//
// CommandDialog.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// 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 Gtk;
using Ntp.Analyzer.Gui.Control;
using Ntp.Analyzer.Gui.Model;

namespace Ntp.Analyzer.Gui.Window
{
    public partial class CommandDialog : Dialog
    {
        #region Initialization
        public CommandDialog (CommandController controller)
        {
            this.controller = controller;
            this.Build ();
        }

        private readonly CommandController controller;
        private ListStore model;
        private Host[] hostList;
        private const int indexStart = 2;
        private int indexLast;

        public void InitializeTree (Host[] hosts)
        {
            hostList = hosts;

            List<Type> columnType = new List<Type> ();
            columnType.Add (typeof(String));

            treeview1.AppendColumn ("Command", new CellRendererText (), "text", 0);

            CellRendererToggle allRend = new CellRendererToggle ();
            allRend.Mode = CellRendererMode.Activatable;
            allRend.Toggled += AllToggled;
            allRend.Active = false;
            TreeViewColumn allCol = new TreeViewColumn ("All hosts", allRend, "active", 1);
            treeview1.AppendColumn (allCol);

            columnType.Add (typeof(Boolean));

            int index = indexStart;

            foreach (Host host in hosts) {
                CellRendererToggle rend = new CellRendererToggle ();
                rend.Mode = CellRendererMode.Activatable;
                rend.Toggled += CellToggled;
                rend.Active = false;
                rend.Data.Add ("ID", host);
                rend.Data.Add ("Index", index);
                TreeViewColumn col = new TreeViewColumn (host.Name, rend, "active", index);
                treeview1.AppendColumn (col);

                columnType.Add (typeof(Boolean));

                index++;
            }

            indexLast = index;

            treeview1.AppendColumn (String.Empty, new CellRendererText (), "text", index);
            columnType.Add (typeof(String));

            model = new ListStore (columnType.ToArray ());
            treeview1.Model = model;
        }
        #endregion
        #region UI Logic
        public void AddRow (string command)
        {
            model.AppendValues (command);
        }
        #endregion
        #region Event handlers
        protected void CancelClicked (object sender, EventArgs e)
        {
            HideAll ();
        }

        protected void OkClicked (object sender, EventArgs e)
        {
            TreeIter iter;
            treeview1.Selection.GetSelected (out iter);

            object value = model.GetValue (iter, 0);
            string command = value != null ? value.ToString () : null;

            if (command == null)
                return;

            List<Host> hosts = new List<Host> ();

            for (int i = 0; i < hostList.Length; i++) {
                if (Convert.ToBoolean (model.GetValue (iter, i + indexStart))) {
                    hosts.Add (hostList [i]);
                }
            }

            HideAll ();
            controller.CommandSelected (command, hosts.ToArray ());
        }

        protected void CellToggled (object o, ToggledArgs args)
        {
            CellRendererToggle rend = o as CellRendererToggle;

            TreeIter iter;
            model.GetIterFromString (out iter, args.Path);

            int column = Convert.ToInt32 (rend.Data ["Index"]);

            bool value = Convert.ToBoolean (model.GetValue (iter, column));
            model.SetValue (iter, column, !value);

            // Reset toggle all column
            model.SetValue (iter, 1, false);
        }

        protected void AllToggled (object o, ToggledArgs args)
        {
            TreeIter iter;
            model.GetIterFromString (out iter, args.Path);

            bool value = Convert.ToBoolean (model.GetValue (iter, 1));

            for (int i = indexStart - 1; i < indexLast; i++) {
                model.SetValue (iter, i, !value);
            }
        }
        #endregion
    }
}
