using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GFATask
{
    [Browsable(false), ToolboxItem(false)]
    public partial class ucAdminAppClickCounter : UserControl
    {
        public ContextMenuStrip CMS
        {
            get
            {
                return new ContextMenuStrip();
            }
        }


        public ucAdminAppClickCounter()
        {
            InitializeComponent();
            this.SetDefaultDesign();
            lvAppClickCounter.SortByColumn(1, SortOrder.Ascending);
            lvAppClickCounter.ContextMenuStrip = CMS;

            lvAppClickCounter.MouseClick += LvAppClickCounter_MouseClick;
            Config.AppClickCounter.GetType().GetProperties().ToList().ForEach(applist =>
            {
                ListViewGroup group = new ListViewGroup(applist.Name, applist.Name);
                lvAppClickCounter.Groups.Add(group);
                var apps = ((List<Models.ClickCounter.AppCounter>)applist.GetValue(Config.AppClickCounter));
                lvAppClickCounter.Items.AddRange(apps.Select(app => new ListViewItem(new string[] { app.ID.ToString(), app.Name, app.Counts.ToString(), app.LastClicked.ToEuropeDateCulture(), app.FirstClicked.ToEuropeDateCulture() }, group)).ToArray());

            });

          
        }

        private void LvAppClickCounter_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                var item = lvAppClickCounter.HitTest(e.Location).Item;
                if(item != null && item.GetType() != typeof(ColumnHeader) && item.GetType() != typeof(ListViewGroup))
                {
                    if (lvAppClickCounter.ContextMenuStrip != null)
                    {
                        lvAppClickCounter.ContextMenuStrip.Items.Clear();
                        ToolStripMenuItem item_reset = new ToolStripMenuItem("Zurücksetzen", null, (s, evt) =>
                        {
                            AppType apptype = (AppType)Enum.Parse(typeof(AppType), item.Group.Header, true);
                            int id = int.Parse(item.Text);
                            ClickCounter clickcounter = Accessor.GetAppCounter(apptype, id).ToClickCounter(apptype);
                            ResetAppCounter(clickcounter);
                            JSON.Save(JSONConfig.AppClickCounter);
                            XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.AppCounter_Reset, AppType = apptype, ClickCounter = clickcounter });
                        });
                        lvAppClickCounter.ContextMenuStrip.Items.Add(item_reset);
                        lvAppClickCounter.ContextMenuStrip.Show(this, new Point(e.X, lvAppClickCounter.GetItemRect(item.Index).Bottom));
                    }
                }
            }
        }


        public void ResetAppCounter(ClickCounter clickcounter)
        {
            Models.ClickCounter.AppCounter appcounter = ((List<Models.ClickCounter.AppCounter>)Config.AppClickCounter.GetType().GetProperty(clickcounter.AppType.ToString()).GetValue(Config.AppClickCounter)).Find(appc => appc.ID == clickcounter.ID);
            appcounter.Counts = 0;
            appcounter.FirstClicked = DateTime.Now;
            appcounter.LastClicked = DateTime.Now;
            clickcounter.Counts = 0;
            clickcounter.FirstClicked = DateTime.Now;
            clickcounter.LastClicked = DateTime.Now;
            ListViewItem replacing_item = clickcounter.AsLvItem();
            replacing_item.Group = lvAppClickCounter.Groups[clickcounter.AppType.ToString()];
            lvAppClickCounter.Items[lvAppClickCounter.Items.Cast<ListViewItem>().ToList().FindIndex(lvitem => lvitem.Text == clickcounter.ID.ToString())] = replacing_item;
        }
        

        private void abtnReset_Click(object sender, EventArgs e)
        {
            if(XObjects.MessageBox("Setzt die gezählten Klicks pro App zurück", NotifyButtons.OkCancel, NotifyType.Message, new Size(350,150), "ClickCounter zurücksetzen").Result == DialogResult.OK)
            {
                Config.AppClickCounter = new Models.ClickCounter()
                {
                    Admin = new List<Models.ClickCounter.AppCounter>(),
                    Menu = new List<Models.ClickCounter.AppCounter>(),
                    User = new List<Models.ClickCounter.AppCounter>()
                };
                JSON.Save(JSONConfig.AppClickCounter);
                ClearAppClickCounterList();
                XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.ClickCounter_Clear });
            }
        }


        public void ClearAppClickCounterList()
        {
            lvAppClickCounter.Items.Clear();
        }


        public void RefreshList(ClickCounter clickcounter)
        {
            ListViewItem item = clickcounter.AsLvItem();
            item.Group = lvAppClickCounter.Groups.Cast<ListViewGroup>().First(group => group.Header == clickcounter.AppType.ToString());

            if (lvAppClickCounter.Items.Cast<ListViewItem>().ToList().Exists(lvitem => lvitem.Text == clickcounter.ID.ToString()))
            {
                lvAppClickCounter.Items[lvAppClickCounter.Items.Cast<ListViewItem>().ToList().FindIndex(lvitem => lvitem.Text == clickcounter.ID.ToString())] = item;
            }
            else
            {
               
                lvAppClickCounter.Items.Add(item);
            }
        }

    }

    internal static class Extensions
    {
        public static ListViewItem AsLvItem(this ClickCounter clickcounter)
        {
            ListViewItem item = new ListViewItem(clickcounter.ID.ToString());
            ListViewItem.ListViewSubItem subName = new ListViewItem.ListViewSubItem(item, clickcounter.Name) { Name = "colAppName" };
            ListViewItem.ListViewSubItem subCounts = new ListViewItem.ListViewSubItem(item, clickcounter.Counts.ToString()) { Name = "colCounts" };
            ListViewItem.ListViewSubItem subLastClicked = new ListViewItem.ListViewSubItem(item, clickcounter.LastClicked.ToEuropeDateCulture()) { Name = "colDateLast" };
            ListViewItem.ListViewSubItem subFirstClicked = new ListViewItem.ListViewSubItem(item, clickcounter.FirstClicked.ToEuropeDateCulture()) { Name = "colDateFirst" };
            item.SubItems.AddRange(new ListViewItem.ListViewSubItem [] { subName, subCounts, subLastClicked, subFirstClicked }.ToArray());
            return item;
        }
    }
}
