using DG.Tweening;
using ScrambleKit;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AITruck : MonoBehaviour
{
    public FXTrigger scoreFX;
    public FXTrigger replacementPizzaFX;
    public Transform boss;
    public Transform customerPrefab;
    public Transform ghostPrefab;

    private void ScoreCash(Cargo playerCargo)
    {
        scoreFX.Trigger(transform);
        boss.DOLocalMoveY(0.5f, 0.5f).SetEase(Ease.OutCubic).SetLoops(2, LoopType.Yoyo);
        boss.DOPunchRotation(new Vector3(0, 0, 30), 1.2f, 20, 1);

        Score.ScoreCash();

        if (Score.GameFinished())
        {
            playerCargo.type = Cargo.ItemType.None;
        }
        else
        {
            Invoke("NewPizzaFX", 0.6f);
            playerCargo.type = Cargo.ItemType.Pizza;
            Transform ghost = Instantiate(ghostPrefab);
            
            if(AICustomer.newCustomerNeeded)
            {
                Transform customer = Instantiate(customerPrefab);
            }
        }
    }

    void NewPizzaFX()
    {
        replacementPizzaFX.Trigger(transform);
    }

    private void ReplaceStolenPizza(Cargo playerCargo)
    {
        if (Score.GameFinished())
            return;

        replacementPizzaFX.Trigger(transform);
        boss.DOLocalRotate(new Vector3(0, 0, 60), 0.6f).SetEase(Ease.InCubic).SetLoops(2, LoopType.Yoyo);
        playerCargo.type = Cargo.ItemType.Pizza;
        Transform ghost = Instantiate(ghostPrefab);

        if(AICustomer.newCustomerNeeded)
        {
            Transform customer = Instantiate(customerPrefab);
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            Cargo playerCargo = other.gameObject.GetComponent<Cargo>();

            if (playerCargo.type == Cargo.ItemType.Cash)
            {
                ScoreCash(playerCargo);
            }

            if (playerCargo.type == Cargo.ItemType.None)
            {
                ReplaceStolenPizza(playerCargo);
            }
        }
    }
}
