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

public class KitchenView : MonoBehaviour
{
    // blue
    private static readonly int Normal = Animator.StringToHash("Normal");
    private static readonly int Tired = Animator.StringToHash("Tired");
    // pink
    private static readonly int Walk = Animator.StringToHash("Walk");
    private static readonly int Stay = Animator.StringToHash("Stay");
    private static readonly int Sleep = Animator.StringToHash("Sleep");
    [SerializeField] private Animator blueBrotherAnimator;
    [SerializeField] private Animator pinkBrotherAnimator;
    [SerializeField] private Transform pinkTransform;
    [SerializeField] private float pinkWalkspeed = .8f;
    private readonly (int trigger, Vector2 wayPoint, float wait)[] pinkMoves =
    {
        (Walk, new Vector2(3.28f,0.726f), 0f),
        // (Stay, new Vector2(3.28f,0.726f), 2f),
        (Walk, new Vector2(0f,0.726f), 0f),
        (Stay, new Vector2(0f,0.726f), 3f),
        (Walk, new Vector2(-3.28f,0.726f), 0f),
        // (Stay, new Vector2(-3.28f,0.726f), 2f),
        (Walk, new Vector2(0f,0.726f), 0f),
        (Stay, new Vector2(0f,0.726f), 3f),
    };

    private int moveIndex;
    private Vector2 wayPoint;
    private float durationDelta;
    private float completedDuration;
    private bool isTired;
    private void Start()
    {
        this.UpdateAsObservable().Subscribe(_ =>
        {
            if (isTired)
            {
                return;
            }
            var localPosition = pinkTransform.localPosition;
            localPosition = Vector2.MoveTowards(localPosition, wayPoint, pinkWalkspeed * Time.deltaTime);
            pinkTransform.localPosition = localPosition;
            if (Vector2.Distance(localPosition, wayPoint) < .01f)
            {
                if (durationDelta < completedDuration)
                {
                    durationDelta += Time.deltaTime;
                    return;
                }
                durationDelta = 0;
                moveIndex++;
                if (moveIndex == pinkMoves.Length)
                {
                    moveIndex = 0;
                }
                SetPinkMove();
            }
        }).AddTo(this);
    }

    public void Initialize()
    {
        moveIndex = 0;
    }

    private void SetPinkMove()
    {
        var move = pinkMoves[moveIndex];
        wayPoint = move.wayPoint;
        completedDuration = move.wait;
        pinkBrotherAnimator.SetTrigger(move.trigger);
        if (Vector2.Distance(wayPoint, pinkTransform.localPosition.ToVector2()) < .01f)
        {
            // nop
        } else if ((wayPoint - pinkTransform.localPosition.ToVector2()).x > 0)
        {
            pinkTransform.localRotation = Quaternion.Euler(Vector3.up * 180);
        }
        else
        {
            pinkTransform.localRotation = Quaternion.Euler(Vector3.zero);
        }
    }

    public void SetNormalAnimation()
    {
        isTired = false;
        SetPinkMove();
        blueBrotherAnimator.SetTrigger(Normal);
    }

    public void SetTiredAnimation()
    {
        isTired = true;
        blueBrotherAnimator.SetTrigger(Tired);
        pinkBrotherAnimator.SetTrigger(Sleep);
    }
}
