using System;
using UniRx;
using UnityEngine;

namespace MyGame.Scenes.Cooking.Scripts
{
    public enum PanChangerState
    {
        Init,
        WillFinish,
        Finish
    }
    public class PanColliderChanger : MonoBehaviour
    {
        [SerializeField] private Collider2D topCollider;
        [SerializeField] private Collider2D topWillFinishCollider;
        [SerializeField] private Collider2D topFinishCollider;
        [SerializeField, Range(0f, 1f)] private float willFinishValue = 1f;
        [SerializeField, Range(0f, 1f)] private float finishValue = 1f;
        private ReactiveProperty<PanChangerState> panChangerState = new ReactiveProperty<PanChangerState>();

        private void Start()
        {
            panChangerState.AddTo(this);
            panChangerState.Value = PanChangerState.Init;
            panChangerState.Subscribe(x =>
            {
                switch (x)
                {
                    case PanChangerState.Init:
                        Init();
                        break;
                    case PanChangerState.WillFinish:
                        WillFinish();
                        break;
                    case PanChangerState.Finish:
                        Finish();
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(x), x, null);
                }
            }).AddTo(this);
        }

        public void Reset()
        {
            panChangerState.Value = PanChangerState.Init;
        }

        public void ChangeState(float value)
        {
            if (value >= finishValue)
            {
                panChangerState.Value = PanChangerState.Finish;
            }
            else if (value >= willFinishValue)
            {
                panChangerState.Value = PanChangerState.WillFinish;
            }
            
        }

        private void Init()
        {
            topCollider.enabled = true;
            topWillFinishCollider.enabled = false;
            topFinishCollider.enabled = false;
        }

        private void WillFinish()
        {
            topCollider.enabled = false;
            topWillFinishCollider.enabled = true;
            topFinishCollider.enabled = false;
        }

        private void Finish()
        {
            topCollider.enabled = false;
            topWillFinishCollider.enabled = false;
            topFinishCollider.enabled = true;
        }
    }
}