using UnityEngine;

namespace COM3D2.MeidoPhotoStudio.Plugin
{
    internal class MaidSelectorPane : BasePane
    {
        private readonly MeidoManager meidoManager;
        private Vector2 maidListScrollPos;
        private readonly Button clearMaidsButton;
        private readonly Button callMaidsButton;
        public MaidSelectorPane(MeidoManager meidoManager)
        {
            this.meidoManager = meidoManager;
            clearMaidsButton = new Button(Translation.Get("maidCallWindow", "clearButton"));
            clearMaidsButton.ControlEvent += (s, a) => this.meidoManager.SelectMeidoList.Clear();

            callMaidsButton = new Button(Translation.Get("maidCallWindow", "callButton"));
            callMaidsButton.ControlEvent += (s, a) => this.meidoManager.CallMeidos();
        }

        protected override void ReloadTranslation()
        {
            clearMaidsButton.Label = Translation.Get("maidCallWindow", "clearButton");
            callMaidsButton.Label = Translation.Get("maidCallWindow", "callButton");
        }

        public override void Draw()
        {
            clearMaidsButton.Draw();
            callMaidsButton.Draw();

            GUIStyle labelStyle = new GUIStyle(GUI.skin.label) { fontSize = 14 };
            GUIStyle selectLabelStyle = new GUIStyle(labelStyle);
            selectLabelStyle.normal.textColor = Color.black;
            selectLabelStyle.alignment = TextAnchor.UpperRight;
            GUIStyle labelSelectedStyle = new GUIStyle(labelStyle);
            labelSelectedStyle.normal.textColor = Color.black;

            Rect windowRect = parent.WindowRect;
            float windowHeight = windowRect.height;
            float buttonWidth = windowRect.width - 30f;
            const float buttonHeight = 85f;

            Rect positionRect = new Rect(5, 115f, windowRect.width - 10f, windowHeight - 150f);
            Rect viewRect = new Rect(0, 0, buttonWidth, (buttonHeight * meidoManager.Meidos.Length) + 5f);
            maidListScrollPos = GUI.BeginScrollView(positionRect, maidListScrollPos, viewRect);

            for (int i = 0; i < meidoManager.Meidos.Length; i++)
            {
                Meido meido = meidoManager.Meidos[i];
                float y = i * buttonHeight;
                bool selectedMaid = meidoManager.SelectMeidoList.Contains(i);

                if (GUI.Button(new Rect(0f, y, buttonWidth, buttonHeight), ""))
                {
                    if (selectedMaid) meidoManager.SelectMeidoList.Remove(i);
                    else meidoManager.SelectMeidoList.Add(i);
                }

                if (selectedMaid)
                {
                    int selectedIndex = meidoManager.SelectMeidoList.IndexOf(i) + 1;
                    GUI.DrawTexture(
                        new Rect(5f, y + 5f, buttonWidth - 10f, buttonHeight - 10f), Texture2D.whiteTexture
                    );
                    GUI.Label(
                        new Rect(0f, y + 5f, buttonWidth - 10f, buttonHeight),
                        selectedIndex.ToString(), selectLabelStyle
                    );
                }

                GUI.DrawTexture(new Rect(5f, y, buttonHeight, buttonHeight), meido.Portrait);
                GUI.Label(
                    new Rect(95f, y + 30f, buttonWidth - 80f, buttonHeight),
                    $"{meido.LastName}\n{meido.FirstName}", selectedMaid ? labelSelectedStyle : labelStyle
                );
            }
            GUI.EndScrollView();
        }
    }
}