using BarRaider.SdTools;
using ClipTrimDotNet.Client;
using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClipTrimDotNet.Keys
{
    [PluginActionId("com.michal-courson.cliptrim.page-navigator")]
    public class PageNavigator : KeypadBase
    {
        static List<PageNavigator> instances = new();
        private KeyCoordinates coordinates;
        public PageNavigator(SDConnection connection, InitialPayload payload) : base(connection, payload)
        {
            coordinates = payload.Coordinates;
            GlobalSettingsManager.Instance.RequestGlobalSettings();
            instances.Add(this);
            OnTick();
        }
        public void Instance_OnReceivedGlobalSettings(object sender, ReceivedGlobalSettingsPayload e)
        {
            Tools.AutoPopulateSettings(GlobalSettings.Instance, e.Settings);
        }
        public int GetIndex()
        {
            int index = Math.Min(Math.Max(coordinates.Column - 1, 0), 2);
            return index;
        }
        public override void Dispose()
        {
        }
        public override void KeyPressed(KeyPayload payload)
        {
            switch (GetIndex())
            {
                case 0:
                    ClipTrimClient.Instance.PageDown();
                    break;
                case 1:
                    ClipTrimClient.Instance.PageMode = !ClipTrimClient.Instance.PageMode;
                    break;
                case 2:
                    ClipTrimClient.Instance.PageUp();
                    break;
            }
            Player.TickAll();
            TickAll();
        }

        public static void TickAll()
        {
            foreach (var instance in instances)
            {
                instance.OnTick();
            }
            instances.RemoveAll(i => i == null);
        }   

        private async void SetTitle()
        {
            switch (GetIndex())
            {
                case 0:
                    await Connection.SetTitleAsync(ClipTrimClient.Instance.CanPageDown ? "<" : "");
                    break;
                case 1:
                    await Connection.SetTitleAsync(ClipTrimClient.Instance.GetCurrentCollectionName() + "\n" + (ClipTrimClient.Instance.PageIndex + 1).ToString());
                    break;
                case 2:
                    await Connection.SetTitleAsync(ClipTrimClient.Instance.CanPageUp ? ">" : "");
                    break;
            }
        }

        public override void OnTick()
        {
            SetTitle();
        }

        public override void KeyReleased(KeyPayload payload)
        {

        }

        public override void ReceivedSettings(ReceivedSettingsPayload payload)
        {

        }
        public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload)
        {
            Tools.AutoPopulateSettings(GlobalSettings.Instance, payload.Settings);
            //if (payload.Settings == null || payload.Settings.Count == 0)
            //{
            //    var inst = GlobalSettings.Instance;
            //    //GlobalSettingsManager.Instance.SetGlobalSettings(JObject.FromObject(inst));
            //}
            //else
            //{ 
            //    GlobalSettings.Instance = payload.Settings.ToObject<GlobalSettings>();
            //}
        }
    }
}
