using System;
using UniRx;
using UnityEngine;

public class CustomerAnimator : MonoBehaviour
{
    public static readonly int WalkFront = Animator.StringToHash("WalkFront");
    public static readonly int WalkSide = Animator.StringToHash("WalkSide");
    public static readonly int WalkBack = Animator.StringToHash("WalkBack");
    public static readonly int StayBack = Animator.StringToHash("StayBack");
    public static readonly int WalkFrontEat = Animator.StringToHash("WalkFrontEat");
    public static readonly int WalkSideEat = Animator.StringToHash("WalkSideEat");
    
    private static readonly int Complain = Animator.StringToHash("ComplainTrigger");
    
    [SerializeField] private Animator animator;
    [SerializeField] private CustomerDirection customerDirection;
    [SerializeField] private Animator orderAnimator;
    [SerializeField] private GameObject tapReaction;
    [SerializeField] private GameObject orderPopup;
    [SerializeField] private Transform leftPopcornTarget;
    [SerializeField] private Transform rightPopcornTarget;
    [SerializeField] private Transform frontPopcornTarget;
    [SerializeField] private Transform wantFlavorSpriteTarget;
    
    private readonly ReactiveProperty<int> triggerName = new ReactiveProperty<int>();

    private void Start()
    {
        triggerName.AddTo(this);
        triggerName.Subscribe(x =>
        {
            animator.SetTrigger(x);
        }).AddTo(this);
    }

    public void SetTrigger(CustomerMovingType movingType)
    {
        switch (movingType)
        {
            case CustomerMovingType.WalkSide:
                triggerName.Value = WalkSide;
                break;
            case CustomerMovingType.WalkSideEat:
                triggerName.Value = WalkSideEat;
                break;
            case CustomerMovingType.WalkCenter:
                triggerName.Value = WalkSide;
                break;
            case CustomerMovingType.StayBackOrder:
                triggerName.Value = StayBack;
                break;
            case CustomerMovingType.StayBack:
                triggerName.Value = StayBack;
                break;
            case CustomerMovingType.WalkBack:
                triggerName.Value = WalkBack;
                break;
            case CustomerMovingType.WalkBackHalf:
                triggerName.Value = WalkBack;
                break;
            case CustomerMovingType.WalkFront:
                triggerName.Value = WalkFront;
                break;
            case CustomerMovingType.WalkFrontEat:
                triggerName.Value = WalkFrontEat;
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(movingType), movingType, null);
        }
        
    }

    public void SetSide(bool isDefaultSide)
    {
        if (isDefaultSide)
        {
            customerDirection.SetDefaultSide();
        }
        else
        {
            customerDirection.SetFlipSide();
        }
    }

    public void SetWantFlavor(MarketPopcornView prefab, ProductRarity rarity)
    {
        leftPopcornTarget.DestroyAllChildrens();
        rightPopcornTarget.DestroyAllChildrens();
        frontPopcornTarget.DestroyAllChildrens();
        wantFlavorSpriteTarget.DestroyAllChildrens();
        Instantiate(prefab, leftPopcornTarget).ChangeRarity(rarity);
        Instantiate(prefab, rightPopcornTarget).ChangeRarity(rarity);
        Instantiate(prefab, frontPopcornTarget).ChangeRarity(rarity);
        Instantiate(prefab, wantFlavorSpriteTarget).ChangeRarity(rarity);
    }
    
    public void ShowWantFlavor()
    {
        orderPopup.SetActive(true);
    }

    public void ShowComplain()
    {
        orderAnimator.SetTrigger(Complain);
        this.CallWaitForSeconds(1.5f, () =>
        {
            orderPopup.SetActive(false);
        });
    }

    public void HideOrderPopup()
    {
        orderPopup.SetActive(false);
    }

    public void ShowTapReaction()
    {
        tapReaction.SetActive(true);
    }
}