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

namespace MyGame.Scenes.marketing.Scripts
{
    public class VipCustomerButtonView : MonoBehaviour
    {
        [SerializeField] private Button vipButton;
        [SerializeField] private Animator buttonAnimator;
        [SerializeField] private Animator vipCustomerAnimator;
        [SerializeField] private CustomerDirection customerDirection;
        [SerializeField] private GameObject vipSpeechBubbleObject;
        [SerializeField] private Transform vipStayPosTransform;
        [SerializeField] private float vipMoveSpeed;
        private readonly BoolReactiveProperty isPause = new BoolReactiveProperty();
        
        public IObservable<Unit> VipButton { get; private set; }
        private IDisposable timerDisposable;
        private IDisposable moveDisposable;
        private Vector3 initialPos;

        private void Awake()
        {
            vipButton.transform.SetLocalScale(0f);
            vipCustomerAnimator.transform.SetLocalScale(0f);
            initialPos = vipCustomerAnimator.transform.localPosition;
            var eventTrigger = vipSpeechBubbleObject.AddComponent<ObservableEventTrigger>();
            VipButton = vipButton.OnClickAsObservable()
                .Merge(eventTrigger.OnPointerClickAsObservable().AsUnitObservable())
                .TakeUntilDestroy(this);
            isPause.AddTo(this);
            isPause.DistinctUntilChanged().Subscribe(x =>
            {
                vipSpeechBubbleObject.transform.SetLocalScale(x ? 0f : 1f);
                buttonAnimator.speed = x ? 0f : 1f;
                vipCustomerAnimator.speed = x ? 0f : 1f;
            }).AddTo(this);
        }

        public void ShowButton(bool animated)
        {
            buttonAnimator.Play("Button_vip", 0, animated ? 0 : 1);
            vipButton.transform.SetLocalScale(1f);
            vipCustomerAnimator.transform.SetLocalScale(1f);
            vipSpeechBubbleObject.SetActive(false);
            customerDirection.SetDefaultSide();
            var vipTransform = vipCustomerAnimator.transform;
            if (animated)
            {
                // 左から歩いてきて立ち止まってmovie_Vip再生
                vipCustomerAnimator.Play("customer_nomal_walk_side");
                moveDisposable?.Dispose();
                moveDisposable = this.UpdateAsObservable()
                    .TakeWhile(_ => Vector2.Distance(vipTransform.localPosition, vipStayPosTransform.localPosition) > .01f)
                    .Subscribe(_ =>
                    {
                        if (isPause.Value)
                        {
                            return;
                        }
                        var localPosition = vipTransform.localPosition;
                        localPosition = Vector2.MoveTowards(localPosition, vipStayPosTransform.localPosition, vipMoveSpeed * Time.deltaTime);
                        vipTransform.localPosition = localPosition.SetZ(initialPos.z);
                    }, () =>
                    {
                        vipCustomerAnimator.Play("customer_nomal_stay_front");
                        vipSpeechBubbleObject.SetActive(true);
                    }).AddTo(this);
            }
            else
            {
                vipTransform.localPosition = vipStayPosTransform.localPosition.SetZ(initialPos.z);
                vipCustomerAnimator.Play("customer_nomal_stay_front");
                vipSpeechBubbleObject.SetActive(true);
            }
        }

        private void CloseButton()
        {
            if (vipSpeechBubbleObject.TryGetComponent<Collider2D>(out var target))
            {
                target.enabled = false;
            }
            // vip左にはける
            vipButton.transform.SetLocalScale(0f);
            vipCustomerAnimator.Play("customer_nomal_walk_side");
            vipSpeechBubbleObject.SetActive(false);
            customerDirection.SetFlipSide();
            var vipTransform = vipCustomerAnimator.transform;
            moveDisposable?.Dispose();
            moveDisposable = this.UpdateAsObservable()
                .TakeWhile(_ => Vector2.Distance(vipTransform.localPosition, initialPos) > .01f)
                .Subscribe(_ =>
                {
                    if (isPause.Value)
                    {
                        return;
                    }
                    var localPosition = vipTransform.localPosition;
                    localPosition = Vector2.MoveTowards(localPosition, initialPos, vipMoveSpeed * Time.deltaTime);
                    vipTransform.localPosition = localPosition.SetZ(initialPos.z);
                }, () =>
                {
                    vipCustomerAnimator.transform.SetLocalScale(0f);
                }).AddTo(this);
        }

        public void ResetTimer(int time)
        {
            // タイマーの更新処理
            timerDisposable?.Dispose();
            timerDisposable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1f))
                .Select(x => (int)(time - x))
                .TakeWhile(x => x > 0)
                .Subscribe(x => { }, () =>
                {
                    CloseButton();
                }).AddTo(this);
        }

        public void SetActive(bool active) => isPause.Value = active;
    }
}