using System;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;

namespace MyGame.Scenes.marketing.Scripts
{
    public enum ShopCustomizeItemState
    {
        Lock,
        Unlock,
        Purchased,
        Selected,
    }
    public class ShopCustomizeItem : MonoBehaviour
    {
        [SerializeField] private Button button;
        [SerializeField] private GameObject lockObject;
        [SerializeField] private GameObject unlockObject;
        [SerializeField] private GameObject purchasedObject;
        [SerializeField] private GameObject selectedObject;
        [SerializeField] private GameObject selectedFrameObject;
        [SerializeField] private GameObject unselectedObject;
        [SerializeField] private Text unlockLevelText;
        [SerializeField] private Text priceText;
        [SerializeField] private Transform iconTarget;
        float pressTime = .5f;

        public IObservable<Unit> PurchaseObservable => purchaseSubject;
        private readonly Subject<Unit> purchaseSubject = new Subject<Unit>();
        public IObservable<Unit> SelectObservable => selectSubject;
        private readonly Subject<Unit> selectSubject = new Subject<Unit>();
        public IObservable<Unit> DetailObservable => detailSubject;
        private readonly Subject<Unit> detailSubject = new Subject<Unit>();
        private readonly ReactiveProperty<ShopCustomizeItemState> itemState = new ReactiveProperty<ShopCustomizeItemState>();
        private void Start()
        {
            itemState.AddTo(this);
            purchaseSubject.AddTo(this);
            selectSubject.AddTo(this);
            detailSubject.AddTo(this);
            itemState.Subscribe(state =>
            {
                lockObject.SetActive(state == ShopCustomizeItemState.Lock);
                iconTarget.gameObject.SetActive(state != ShopCustomizeItemState.Lock);
                unlockObject.SetActive(state == ShopCustomizeItemState.Unlock);
                purchasedObject.SetActive(state == ShopCustomizeItemState.Purchased || state == ShopCustomizeItemState.Selected);
                selectedObject.SetActive(state == ShopCustomizeItemState.Selected);
                selectedFrameObject.SetActive(state == ShopCustomizeItemState.Selected);
                unselectedObject.SetActive(state == ShopCustomizeItemState.Purchased);
            }).AddTo(this);
            
            button.OnPointerDownAsObservable()
                .Select(_ => 
                    Observable.Amb(
                        Observable.Timer(TimeSpan.FromSeconds(pressTime)).Select(x => true),
                        button.OnPointerUpAsObservable().Select(x => false))
                    )
                .Switch()
                .Subscribe(longPress =>
                {
                    if (longPress)
                    {
                        if (itemState.Value == ShopCustomizeItemState.Purchased)
                        {
                            detailSubject.OnNext(Unit.Default);
                        }
                    }
                    else
                    {
                        switch (itemState.Value)
                        {
                            case ShopCustomizeItemState.Lock:
                                break;
                            case ShopCustomizeItemState.Unlock:
                                purchaseSubject.OnNext(Unit.Default);
                                break;
                            case ShopCustomizeItemState.Purchased:
                                selectSubject.OnNext(Unit.Default);
                                break;
                            case ShopCustomizeItemState.Selected:
                                detailSubject.OnNext(Unit.Default);
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }
                    }
                }).AddTo(this);
        }

        public void SetState(ShopCustomizeItemState state)
        {
            itemState.Value = state;
        }

        public void SetData(ShopCustomizeData customizeData)
        {
            unlockLevelText.text = customizeData.level.ToString();
            priceText.text = customizeData.price.ToString();
            iconTarget.DestroyAllChildrens();
            Instantiate(customizeData.GetIconPrefab(), iconTarget);
        }
    }
}