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

public class Player : MonoBehaviour
{
    [SerializeField] private float defaultUpSpeed = 1.0f;
    [SerializeField] private float defaultHorizontalSpeed = 1.0f;
    [SerializeField] private float defaultColiderRadius = 40f;
    [SerializeField] private float maxSize = 3f;
    [SerializeField] private PlayerImage playerImage;
    [SerializeField] private PlayerVisual playerVisual;
    [SerializeField] private PlayerSound playerSound;

    
    public float currentUpSpeed;
    private float currentHorizontalSpeed;
    private CircleCollider2D circleCollider;

    private void Awake()
    {
        circleCollider = GetComponent<CircleCollider2D>();
        //SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
    }

    private void Start()
    {
        currentUpSpeed = defaultUpSpeed;
        currentHorizontalSpeed = defaultHorizontalSpeed;
        
        circleCollider.radius = defaultColiderRadius;
    }

    private void Update()
    {
        HandleMovement();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision != null)
        {
            //Debug.Log("Destroy");
            GameObject gameObject = collision.gameObject;
            if (gameObject.transform.TryGetComponent(out Creature creature))
            {
                if(gameObject.transform.TryGetComponent(out HaiDan haiDan))
                {
                    playerVisual.PlayDieAni();
                    playerSound.PlayBreakSound();
                    GameApp.Instance.HandlePlayerDestroyedLogic();
                }
                else
                {
                    HandleEatLogic(creature);
                }
            }
        }
    }

    private void HandleEatLogic(Creature creature)
    {
        playerSound.PlayEatSound();
        Destroy(creature.gameObject);
    }

    private void HandleMovement()
    {
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            Vector3 currentPosition = transform.position;
            currentPosition.x -= currentHorizontalSpeed * Time.deltaTime;
            transform.position = currentPosition;
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            Vector3 currentPosition = transform.position;
            currentPosition.x += currentHorizontalSpeed * Time.deltaTime;
            transform.position = currentPosition;
        }

        Vector3 curPosition = transform.position;
        curPosition.y += currentUpSpeed * Time.deltaTime;
        transform.position = curPosition;
    }

    public float GetCurrentUpSpeed()
    {
        Debug.Log(currentUpSpeed);

        return currentUpSpeed;
    }

    public void ChangePlayerSize(float multi)
    {
        float newColiderRadius = defaultColiderRadius * multi;
        circleCollider.radius = newColiderRadius;
        currentUpSpeed *= multi;

        playerImage.ChangePlayerSizeVisual(multi);
    }

    internal void Hide()
    {
        gameObject.SetActive(false);
    }
}
