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

namespace MyGame.Scenes.marketing.Scripts
{
    public class ShopCustomizeSelector : MonoBehaviour
    {
        [SerializeField] private ButtonOnOff signBoardButton;
        [SerializeField] private ButtonOnOff decoration1Button;
        [SerializeField] private ButtonOnOff cartButton;
        [SerializeField] private ButtonOnOff stockButton;
        [SerializeField] private ButtonOnOff decoration2Button;
        [SerializeField] private ButtonOnOff category6Button;
        [SerializeField] private ShopCustomizeItem itemPrefab;
        [SerializeField] private Transform listPrefab;
        [SerializeField] private ScrollRect scrollRect;

        public IReadOnlyReactiveProperty<ShopCustomizeCategory> SelecedCategory => selectedCategory;
        private readonly ReactiveProperty<ShopCustomizeCategory> selectedCategory = new ReactiveProperty<ShopCustomizeCategory>();
        private readonly Dictionary<ShopCustomizeCategory, Transform> targetDict = new Dictionary<ShopCustomizeCategory, Transform>();
        private readonly Dictionary<ShopCustomizeCategory, ButtonOnOff> buttonDict = new Dictionary<ShopCustomizeCategory, ButtonOnOff>();
        private readonly CompositeDisposable initializeCompositeDisposable = new CompositeDisposable();

        private void Start()
        {
            selectedCategory.AddTo(this);
        }

        public void Initialize(ShopCustomizeCategory firstSelectCategory)
        {
            var viewport = scrollRect.viewport;
            viewport.DestroyAllChildrens();
            var categories = (ShopCustomizeCategory[])Enum.GetValues(typeof(ShopCustomizeCategory));
            foreach (var category in categories)
            {
                targetDict.Add(category, Instantiate(listPrefab, viewport));
            }
            buttonDict.Add(ShopCustomizeCategory.Category1, signBoardButton);
            buttonDict.Add(ShopCustomizeCategory.Category2, decoration1Button);
            buttonDict.Add(ShopCustomizeCategory.Category3, cartButton);
            buttonDict.Add(ShopCustomizeCategory.Category4, stockButton);
            buttonDict.Add(ShopCustomizeCategory.Category5, decoration2Button);
            // buttonDict.Add(ShopCustomizeCategory.Category6, category6Button);
            
            initializeCompositeDisposable.Clear();
            buttonDict.Select(pair => pair.Value.OnClickObservable.Select(_ => pair.Key))
                .Merge()
                .StartWith(firstSelectCategory)
                .ThrottleFirst(TimeSpan.FromSeconds(.2f))
                .Subscribe(x => selectedCategory.Value = x)
                .AddTo(initializeCompositeDisposable);
            selectedCategory.Subscribe(ChangeCategory).AddTo(this);
        }

        public ShopCustomizeItem GenerateItem(ShopCustomizeCategory category)
        {
            return Instantiate(itemPrefab, targetDict[category]);
        }

        public void SetScroll()
        {
            // 描画計算
            // shopInfoObject.SetActive(true);
            // recordObject.SetActive(true);
            Observable.TimerFrame(0, FrameCountType.EndOfFrame).Subscribe(_ =>
            {
                // LayoutRebuilder.ForceRebuildLayoutImmediate(recordObject.transform as RectTransform);
                // LayoutRebuilder.ForceRebuildLayoutImmediate(shopInfoObject.transform as RectTransform);
            }).AddTo(this);
        }

        private void ChangeCategory(ShopCustomizeCategory category)
        {
            foreach (var pair in targetDict)
            {
                pair.Value.gameObject.SetActive(pair.Key == category);
                buttonDict[pair.Key].SetActive(pair.Key == category);
            }
            scrollRect.content = targetDict[category] as RectTransform;
        }
    }
}