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

public class PlayerSkillHandler: MonoBehaviour
{
    [Header("Dash stats")]
    [SerializeField] private float dashCooldown;

    [SerializeField] private float dashSpeed;
    [SerializeField] private float dashDuration;
    private bool isDashing = false;

    private Dictionary<PlayerSkillTree.Skills, float> cooldowns;
    private Dictionary<PlayerSkillTree.Skills, float> cooldownsActivated;
    private PlayerSkillTree playerSkillTree;
    private HealthManager healthManager;

    private CharacterController controller;

    public bool IsDashing() { return isDashing; }

    public void Start()
    {
        cooldowns = new Dictionary<PlayerSkillTree.Skills, float>();
        cooldownsActivated = new Dictionary<PlayerSkillTree.Skills, float>();
        controller = GetComponent<CharacterController>();
        playerSkillTree = PlayerSkillTree.Instance;
        healthManager = GetComponent<HealthManager>();
        AssignCooldowns();
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && !IsOnCooldown(PlayerSkillTree.Skills.Dash) && playerSkillTree.IsSkillUnlocked(PlayerSkillTree.Skills.Dash))
        {
            /* Get the input for horizontal and vertical movement */
            float moveX = Input.GetAxisRaw("Horizontal");
            float moveZ = Input.GetAxisRaw("Vertical");

            var move = new Vector3(moveX, 0, moveZ).normalized;
            StartCoroutine(DashCoroutine(controller, move));
        }

        if (Input.GetKeyDown(KeyCode.L))
        {
            if (playerSkillTree.TryUsePotion(PotionHandler.PotionType.HealthBig))
            {
                Debug.Log("Potion used!");
                healthManager.ModifyHealth(10);
            }
            else
            {
                Debug.Log("Potion not available!");
            }
        }
    }

    public void FixedUpdate()
    {
        
    }

    public IEnumerator DashCoroutine(CharacterController controller, Vector3 direction)
    {
        Physics.IgnoreLayerCollision(6, 7, true); // disable collisions between player and enemies
        float startTime = Time.time;
        isDashing = true; // just for now. maybe will be removed
        while (Time.time - startTime < dashDuration)
        {
            controller.Move(dashSpeed * Time.deltaTime * direction);
            yield return null; // wait one frame
        }
        handleCooldownTimer(PlayerSkillTree.Skills.Dash);
        isDashing = false; // just for now. maybe will be removed
        Physics.IgnoreLayerCollision(6, 7, false); // enable collisions between player and enemies
    }

    private void handleCooldownTimer(PlayerSkillTree.Skills skill)
    {
        if(!cooldownsActivated.ContainsKey(skill))
        {
            cooldownsActivated.Add(skill, Time.time);
        }
        else
        {
            cooldownsActivated[skill] = Time.time;
        }
            
    }

    private void AssignCooldowns()
    {
        // handle Dash stats
        cooldowns.Add(PlayerSkillTree.Skills.Dash, dashCooldown);
        cooldownsActivated.Add(PlayerSkillTree.Skills.Dash, Time.time - dashCooldown);
    }

    public bool IsOnCooldown(PlayerSkillTree.Skills skill) 
    {
        //Debug.Log("cooldown time: " + (Time.time - cooldownsActivated[skill]));
        //Debug.Log("skill cooldown: " + cooldowns[skill]);
        return !cooldowns.ContainsKey(skill) || !cooldownsActivated.ContainsKey(skill)
            ? false
            : Time.time - cooldownsActivated[skill] < cooldowns[skill];
    }
}
