//
// MainWindowController.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Ntp.Process;
using Ntp.Monitor;
using Ntp.Monitor.Client;
using Ntp.Analyzer.Gui.Model;
using Ntp.Analyzer.Gui.Window;
using Ntp.Analyzer.Gui.Data;

namespace Ntp.Analyzer.Gui.Control
{
    public sealed class MainWindowController
    {
        #region Initialization
        public MainWindowController (MasterController master)
        {
            this.master = master;
            main = new MainWindow (this);
        }

        private readonly MasterController master;
        private readonly MainWindow main;
        #endregion
        #region UI Handleling
        public void Start ()
        {
            main.Show ();
        }

        internal void HomeSelected ()
        {
            ExecuteCommand ("help");
        }

        internal void NetworkSelected ()
        {
            NetworkSetupController controller = new NetworkSetupController (master);
            controller.Start ();
        }

        public void SelectCommand ()
        {
            CommandController controller = new CommandController (this);
            controller.Show ();
        }
        #endregion
        #region Controller Logic
        public void ExecuteCommand (string command)
        {
            ExecuteCommand (command, HostDataStore.Instance.Hosts.ToArray ());
        }

        public void ExecuteCommand (string command, Host[] hosts)
        {
            main.Clear ();

            foreach (Host host in hosts) {
                TextRequest req = new TextRequest (host.EndPoint);
                string result = req.Send (command);
                main.ShowResult (host.ToString (), result);
            }
        }
        #endregion
    }
}
