using Krypton.Toolkit;
using qtcnet_client.Factories;
using qtcnet_client.Forms.Games;
using QtCNETAPI.Schema;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace qtcnet_client.Controls
{
    public partial class MainTabControl : UserControl
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListViewItem? SelectedUser { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListViewItem? SelectedStoreItem { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListViewItem? SelectedGameItem { get; set; }

        public event EventHandler? OnContactControlDoubleClicked;
        public event EventHandler? OnUserItemDoubleClicked;
        public event EventHandler? OnRoomControlDoubleClicked;
        public event EventHandler? OnStoreItemDoubleClicked;
        public event EventHandler? OnGameItemDoubleClicked;

        private ImageFactory _imgFactory;
        public MainTabControl(ImageFactory imageFactory)
        {
            _imgFactory = imageFactory;
            InitializeComponent();
        }

        public void AddContacts(List<ContactControl> contactControls)
        {
            if (tlpContactsList.InvokeRequired)
            {
                tlpContactsList.Invoke(() =>
                {
                    tlpContactsList.SuspendLayout();

                    foreach (ContactControl contactControl in contactControls)
                    {
                        contactControl.Width = tlpContactsList.ClientSize.Width;
                        contactControl.OnContactDoubleClicked += ContactControl_OnContactDoubleClicked;
                    }

                    tlpContactsList.Controls.Clear();
                    tlpContactsList.Controls.AddRange([.. contactControls.DistinctBy(ctrl => ctrl.UserId)]);

                    tlpContactsList.ResumeLayout(true);
                });
            }
            else
            {
                tlpContactsList.SuspendLayout();

                foreach (ContactControl contactControl in contactControls)
                {
                    contactControl.Width = tlpContactsList.ClientSize.Width;
                    contactControl.OnContactDoubleClicked += ContactControl_OnContactDoubleClicked;
                }

                tlpContactsList.Controls.Clear();
                tlpContactsList.Controls.AddRange([.. contactControls.DistinctBy(ctrl => ctrl.UserId)]);

                tlpContactsList.ResumeLayout(true);
            }
        }

        private void ContactControl_OnContactDoubleClicked(object? sender, EventArgs e) => OnContactControlDoubleClicked?.Invoke(sender, e);

        public void AddRooms(List<RoomControl> roomControls)
        {
            if (tlpRoomsList.InvokeRequired)
            {
                tlpRoomsList.Invoke(() =>
                {
                    tlpRoomsList.SuspendLayout();

                    foreach (RoomControl roomControl in roomControls)
                    {
                        roomControl.Width = tlpRoomsList.ClientSize.Width;
                        roomControl.OnRoomDoubleClicked += RoomControl_OnRoomDoubleClicked;
                    }

                    tlpRoomsList.Controls.Clear();
                    tlpRoomsList.Controls.AddRange([.. roomControls.DistinctBy(ctrl => ctrl.RoomId)]);

                    tlpRoomsList.ResumeLayout(true);
                });
            }
            else
            {
                tlpRoomsList.SuspendLayout();

                foreach (RoomControl roomControl in roomControls)
                {
                    roomControl.Width = tlpRoomsList.ClientSize.Width;
                    roomControl.OnRoomDoubleClicked += RoomControl_OnRoomDoubleClicked;
                }

                tlpRoomsList.Controls.Clear();
                tlpRoomsList.Controls.AddRange([.. roomControls.DistinctBy(ctrl => ctrl.RoomId)]);

                tlpRoomsList.ResumeLayout(true);
            }
        }

        private void RoomControl_OnRoomDoubleClicked(object? sender, EventArgs e) => OnRoomControlDoubleClicked?.Invoke(sender, e);

        public void AddUsers(List<ListViewItem> users)
        {
            if (lvUserList.InvokeRequired)
            {
                lvUserList.Invoke(() =>
                {
                    lvUserList.SuspendLayout();

                    lvUserList.Items.Clear();
                    lvUserList.Items.AddRange([.. users.DistinctBy(lvi => lvi.Tag)]); // Tag = UserId here

                    lvUserList.ResumeLayout(true);
                });
            }
            else
            {
                lvUserList.SuspendLayout();

                lvUserList.Items.Clear();
                lvUserList.Items.AddRange([.. users.DistinctBy(lvi => lvi.Tag)]); // Tag = UserId here

                lvUserList.ResumeLayout(true);
            }
        }

        private void lvUserList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvUserList.SelectedItems.Count > 0)
            {
                ListViewItem? lvi = lvUserList.SelectedItems[0];
                if (lvi != null)
                {
                    SelectedUser = lvi;
                    OnUserItemDoubleClicked?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public async void AddStoreItems(List<StoreItem> items)
        {
            if (lvStoreItems.InvokeRequired)
            {
                await lvStoreItems.Invoke(async () =>
                {
                    lvStoreItems.SuspendLayout();

                    // iterate through store items
                    foreach (var item in items)
                    {
                        ilStoreIcons.Images.Add(await _imgFactory.GetStoreItemThumb(item.Id));

                        lvStoreItems.Items.Add(new ListViewItem()
                        {
                            Tag = item.Id,
                            Text = item.Name,
                            ImageIndex = item.Id,
                        });
                    }

                    lvStoreItems.ResumeLayout(true);
                });
            }
            else
            {
                lvStoreItems.SuspendLayout();

                // iterate through store items
                foreach (var item in items)
                {
                    var lvi = lvStoreItems.Items.Add(new ListViewItem()
                    {
                        Tag = item.Id,
                        Text = item.Name,
                    });

                    ilStoreIcons.Images.Add(await _imgFactory.GetStoreItemThumb(item.Id));
                    lvi.ImageIndex = item.Id;
                }

                lvStoreItems.ResumeLayout(true);
            }
        }

        private void lvStoreItems_DoubleClick(object sender, EventArgs e)
        {
            if (lvStoreItems.SelectedItems.Count > 0)
            {
                ListViewItem? lvi = lvStoreItems.SelectedItems[0];
                if (lvi != null)
                {
                    SelectedStoreItem = lvi;
                    OnStoreItemDoubleClicked?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        private void lvGames_DoubleClick(object sender, EventArgs e)
        {
            if (lvGames.SelectedItems.Count > 0)
            {
                ListViewItem? lvi = lvGames.SelectedItems[0];
                if (lvi != null)
                {
                    SelectedGameItem = lvi;
                    OnGameItemDoubleClicked?.Invoke(this, EventArgs.Empty);
                }
            }
        }
    }
}
