//
// NetworkDialog.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 dotreeon 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 CONTRAtreeOR OTHERWISE, ARISING FROM,
// OUT OF ORtreeCTION WITH THE SOFTWARE OR treeR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Gtk;
using Ntp.Analyzer.Gui.Control;
using System.Globalization;
using Ntp.Analyzer.Gui.Model;
using System.Runtime.InteropServices;

namespace Ntp.Analyzer.Gui.Window
{
    public partial class NetworkDialog : Gtk.Dialog
    {
        #region Initialization and Properties
        public NetworkDialog (NetworkSetupController controller)
        {
            this.controller = controller;
            this.Build ();
            Initialize ();
        }

        private readonly NetworkSetupController controller;

        private ListStore listStore {
            get { return tree.Model as ListStore; }
        }

        private void Initialize ()
        {
            ListStore list = new ListStore (
                typeof(Int32),
                typeof(String),
                typeof(String),
                typeof(String)
            );

            // ID column
            TreeViewColumn colId = new TreeViewColumn ();
            colId.Title = "ID";
            colId.Visible = false;
            tree.AppendColumn (colId);

            // First column
            TreeViewColumn col1 = new TreeViewColumn ();
            col1.Title = "Daemon name";

            CellRendererText col1r = new CellRendererText ();
            col1r.Editable = true;
            col1r.Edited += new EditedHandler (CellEdited);
            col1r.Data.Add ("ColIndex", 1);

            col1.PackStart (col1r, true);
            col1.AddAttribute (col1r, "text", 1);
            tree.AppendColumn (col1);

            // Second column
            TreeViewColumn col2 = new TreeViewColumn ();
            col2.Title = "IP address";

            CellRendererText col2r = new CellRendererText ();
            col2r.Editable = true;
            col2r.Edited += new EditedHandler (CellEdited);
            col2r.Data.Add ("ColIndex", 2);

            col2.PackStart (col2r, true);
            col2.AddAttribute (col2r, "text", 2);
            tree.AppendColumn (col2);

            // Third column
            TreeViewColumn col3 = new TreeViewColumn ();
            col3.Title = "Port";

            CellRendererText col3r = new CellRendererText ();
            col3r.Editable = true;
            col3r.Edited += new EditedHandler (CellEdited);
            col3r.Data.Add ("ColIndex", 3);

            col3.PackStart (col3r, true);
            col3.AddAttribute (col3r, "text", 3);
            tree.AppendColumn (col3);

            tree.Model = list;
        }
        #endregion
        #region UI Logic
        public void AppendLine (int id, string name, string address, int port)
        {
            listStore.AppendValues (
                id, name, address, port.ToString (CultureInfo.InvariantCulture));
        }
        #endregion
        #region Event Handlers
        protected void AddClicked (object sender, EventArgs e)
        {
            controller.Add ();
        }

        protected void RemoveClicked (object sender, EventArgs e)
        {
            TreeIter iter;

            if (tree.Selection.GetSelected (out iter)) {
                int id = Convert.ToInt32 (listStore.GetValue (iter, 0));
                listStore.Remove (ref iter);
                controller.DeleteRow (id);
            }
        }

        protected void OkClicked (object sender, EventArgs e)
        {
            controller.ApplySelected ();
        }

        protected void CancelClicked (object sender, EventArgs e)
        {
            controller.CancelSelected ();
        }

        protected void CellEdited (object o, EditedArgs args)
        {
            CellRenderer rend = o as CellRenderer;

            string value = args.NewText;
            int column = Convert.ToInt32 (rend.Data ["ColIndex"]);

            TreeIter iter;
            listStore.GetIterFromString (out iter, args.Path);
            listStore.SetValue (iter, column, value);

            int id = Convert.ToInt32 (listStore.GetValue (iter, 0));
            controller.CellUpdated (id, column, value);
        }
        #endregion
    }
}
