using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum FoodType
{
    Suger,
    Teeth,
    GoldenTeeth
}

public class FoodObject : MonoBehaviour
{
    // could be the parent class for different types of food if needed

    public Animator foodAnimator;

    public bool isHitted;
    public bool isfrozen;
    public Vector2 movingDir;
    public float hitspeed = 5.0f;
    public float originHitSpeed = 5.0f;
    public FoodType foodType;
    public float moveSpeed = 5.0f;
    public string hittedAnimationName;
    public string moveAnimationName;
    public float sonicAttackPushDistance = 5;

    private Vector3 targetPos;
    private Vector3 wanderTargetPos;
    
    // Start is called before the first frame update
    void Start()
    {
        isHitted = false;
        isfrozen = false;
        targetPos = GameObject.Find("King").transform.position;
        wanderTargetPos = transform.position;
    }

    public void ResetHitSpeed()
    {
        hitspeed = originHitSpeed;
    }

    // Update is called once per frame
    protected void Update()
    {
        if (!isfrozen)
        {
            if (isHitted)
            {
                if (GameController.Instance.IsBounceOpen)
                {
                    var x = movingDir.x;
                    var y = movingDir.y;
                    var position = transform.position;
                    if (Math.Abs(position.x) > 16.5)
                    {
                        transform.position = new Vector3(x < 0 ? -16.49f : 16.49f, position.y, position.z);
                        x = -x;
                    }
                    if (Math.Abs(position.y) > 8.5)
                    {
                        transform.position = new Vector3(position.x , y < 0 ? -8.49f : 8.49f, position.z);
                        y = -y;
                    }

                    hitspeed -= GameController.Instance.Deceleration * Time.deltaTime;
                    
                    movingDir = new Vector2(x, y);
                    if(hitspeed > 0)
                        transform.position += new Vector3(movingDir.x, movingDir.y, 0) * hitspeed * Time.deltaTime;
                    else
                    {
                        isHitted = false;
                        foodAnimator.Play(moveAnimationName);
                    }
                }
                else
                {
                    if (Math.Abs(transform.position.x) > 20 || Math.Abs(transform.position.y) > 10)
                    {
                        Destroy(gameObject);
                        GameController.Instance.DestroyFood(foodType);
                    }
                    else
                    {
                        transform.position += new Vector3(movingDir.x, movingDir.y, 0) * hitspeed * Time.deltaTime;
                    }
                }

                if (foodAnimator != null) foodAnimator.Play(hittedAnimationName);
            }
            else
            {
                //foodAnimator.Play("chef_walk");
                if (foodType == GameController.Instance.TargetFoodType
                    || (foodType == FoodType.GoldenTeeth && GameController.Instance.TargetFoodType == FoodType.Teeth))
                {
                    if (Math.Abs(Vector2.Distance(transform.position, wanderTargetPos)) < 0.01f)
                    {
                        wanderTargetPos = GameController.Instance.GetRandomWanderPoint();
                    }
                    Vector3 dir = (wanderTargetPos - transform.position).normalized;
                    transform.position += new Vector3(dir.x, dir.y, 0) * moveSpeed * Time.deltaTime;
                    if (dir.x < 0)
                    {
                        foodAnimator.transform.localScale = Vector3.one;
                    }
                    else if (dir.x > 0)
                    {
                        foodAnimator.transform.localScale = new Vector3(-1, 1, 1);
                    }
                }
                else
                {
                    Vector3 dir = (targetPos - transform.position).normalized;
                    transform.position += new Vector3(dir.x, dir.y, 0) * moveSpeed * Time.deltaTime;
                    if (dir.x < 0)
                    {
                        foodAnimator.transform.localScale = Vector3.one;
                    }
                    else if (dir.x > 0)
                    {
                        foodAnimator.transform.localScale = new Vector3(-1, 1, 1);
                    }
                }
            }
        }
    }

    public void ReceiveSonicAttack()
    {
        Vector3 dir = (transform.position - targetPos).normalized;
        transform.position += new Vector3(dir.x, dir.y, 0) * sonicAttackPushDistance;
    }
}
