using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace MeidoPhotoStudio.Plugin;

public class SettingsWindowPane : BaseMainWindowPane
{
    private static readonly string[] headerTranslationKeys = {
        "controls", "controlsGeneral", "controlsMaids", "controlsCamera", "controlsDragPoint", "controlsScene"
    };

    private static readonly Dictionary<string, string> headers = new();
    private static readonly string[] actionTranslationKeys;
    private static readonly string[] actionLabels;

    private readonly Button reloadTranslationButton;
    private readonly Button reloadAllPresetsButton;
    private readonly KeyRebindButton[] rebindButtons;

    static SettingsWindowPane()
    {
        actionTranslationKeys = Enum.GetNames(typeof(MpsKey))
            .Select(action => char.ToLowerInvariant(action[0]) + action.Substring(1))
            .ToArray();

        actionLabels = new string[actionTranslationKeys.Length];
    }

    public SettingsWindowPane()
    {
        rebindButtons = new KeyRebindButton[actionTranslationKeys.Length];

        for (var i = 0; i < rebindButtons.Length; i++)
        {
            var action = (MpsKey)i;
            var button = new KeyRebindButton(KeyCode.None);

            button.ControlEvent += (s, a) => InputManager.Rebind(action, button.KeyCode);
            rebindButtons[i] = button;

            actionLabels[i] = Translation.Get("controls", actionTranslationKeys[i]);
        }

        for (var i = 0; i < headerTranslationKeys.Length; i++)
            headers[headerTranslationKeys[i]] = Translation.Get("settingsHeaders", headerTranslationKeys[i]);

        reloadTranslationButton = new(Translation.Get("settingsLabels", "reloadTranslation"));
        reloadTranslationButton.ControlEvent += (s, a) => Translation.ReinitializeTranslation();

        reloadAllPresetsButton = new(Translation.Get("settingsLabels", "reloadAllPresets"));
        reloadAllPresetsButton.ControlEvent += (s, a) =>
        {
            Constants.InitializeCustomFaceBlends();
            Constants.InitializeHandPresets();
            Constants.InitializeCustomPoses();
        };
    }

    public override void Draw()
    {
        scrollPos = GUILayout.BeginScrollView(scrollPos);

        MpsGui.Header(headers["controls"]);
        MpsGui.WhiteLine();

        MpsGui.Header(headers["controlsGeneral"]);
        MpsGui.WhiteLine();

        for (var key = MpsKey.Activate; key <= MpsKey.ToggleMessage; key++)
            DrawSetting(key);

        MpsGui.Header(headers["controlsMaids"]);
        MpsGui.WhiteLine();
        DrawSetting(MpsKey.MeidoUndressing);

        MpsGui.Header(headers["controlsCamera"]);
        MpsGui.WhiteLine();

        for (var key = MpsKey.CameraLayer; key <= MpsKey.CameraLoad; key++)
            DrawSetting(key);

        MpsGui.Header(headers["controlsDragPoint"]);
        MpsGui.WhiteLine();

        for (var key = MpsKey.DragSelect; key <= MpsKey.DragFinger; key++)
            DrawSetting(key);

        MpsGui.Header(headers["controlsScene"]);
        MpsGui.WhiteLine();

        for (var key = MpsKey.SaveScene; key <= MpsKey.OpenSceneManager; key++)
            DrawSetting(key);

        GUI.enabled = !InputManager.Listening;

        // Translation settings
        MpsGui.WhiteLine();
        reloadTranslationButton.Draw();

        reloadAllPresetsButton.Draw();

        GUILayout.EndScrollView();

        GUI.enabled = true;
    }

    public override void UpdatePanes()
    {
        for (var i = 0; i < rebindButtons.Length; i++)
            rebindButtons[i].KeyCode = InputManager.GetActionKey((MpsKey)i);
    }

    protected override void ReloadTranslation()
    {
        for (var i = 0; i < rebindButtons.Length; i++)
            actionLabels[i] = Translation.Get("controls", actionTranslationKeys[i]);

        for (var i = 0; i < headerTranslationKeys.Length; i++)
            headers[headerTranslationKeys[i]] = Translation.Get("settingsHeaders", headerTranslationKeys[i]);

        reloadTranslationButton.Label = Translation.Get("settingsLabels", "reloadTranslation");
        reloadAllPresetsButton.Label = Translation.Get("settingsLabels", "reloadAllPresets");
    }

    private void DrawSetting(MpsKey key)
    {
        var keyIndex = (int)key;

        GUILayout.BeginHorizontal();
        GUILayout.Label(actionLabels[keyIndex]);
        GUILayout.FlexibleSpace();
        rebindButtons[keyIndex].Draw(GUILayout.Width(90f));

        if (GUILayout.Button("×", GUILayout.ExpandWidth(false)))
        {
            rebindButtons[keyIndex].KeyCode = KeyCode.None;
            InputManager.Rebind(key, KeyCode.None);
        }

        GUILayout.EndHorizontal();
    }

}
