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

public class Player : MonoBehaviour {
    public static Player instance;

    private GameObject sword, shield;
    private AttackBox attackBox;

    private float clapThreshold = 1f;
    private float lastClapTimer = 10f;

    [SerializeField]
    private int maxHealth = 100;
    private int curHealth = 100;

    private bool _hasSword = false;
    public bool hasSword
    {
        get { return _hasSword; }
        set
        {
            _hasSword = value;
            sword.gameObject.SetActive(true);
            HUD.instance.SetSword(value);
        }
    }

    private bool _hasShield = false;
    public bool hasShield
    {
        get { return _hasShield; }
        set
        {
            _hasShield = value;
            HUD.instance.SetShield(value);
        }
    }

    private int _coins = 0;
    public int coins
    {
        get { return _coins; }
        set
        {
            _coins = value;
            HUD.instance.SetCoinCount(value);
        }
    }

    public int killCount = 0;

    [SerializeField]
    private float attackSpeed = 1;
    private float attackTimer = 0;

    private float _time = 0;
    public float time {  get { return _time; } }


    private void Awake()
    {
        instance = this;
        sword = transform.FindChild("Sword").gameObject;
        shield = transform.FindChild("Shield").gameObject;
        attackBox = sword.GetComponent<AttackBox>();
        curHealth = maxHealth;
    }
    private void Update()
    {
        _time += Time.deltaTime;
    }
    private void LateUpdate()
    {
        lastClapTimer += Time.deltaTime;
        if (lastClapTimer > clapThreshold)
        {
            if (Input.GetKeyDown("e"))
            {
                StartCoroutine(SoundBlastGenerator.instance.PerformClap(transform.position, SoundBlast.Type.Minor));
                lastClapTimer = 0.0f;
            }
            else if (Input.GetKeyDown("q"))
            {
                StartCoroutine(SoundBlastGenerator.instance.PerformMajorClap(transform.position));
                lastClapTimer = 0.0f;
            }
        }
        if (hasShield && Input.GetMouseButtonDown(1))
        {
            shield.SetActive(true);
            sword.SetActive(false);
        }
        else if (hasSword && Input.GetMouseButtonDown(0))
        {
            if (attackTimer <= 0)
            {
                StartCoroutine(attackBox.Attack());
                attackTimer = attackSpeed;
            }
        }
        if (hasShield && Input.GetMouseButtonUp(1))
        {
            shield.gameObject.SetActive(false);
            if (hasSword)
                sword.SetActive(true);
        }
        if (attackTimer > 0)
            attackTimer -= Time.deltaTime;

    }

    public void TakeDamage(int dam)
    {
        if (hasShield && Input.GetMouseButton(1))
            return;
        curHealth -= dam;
        HUD.instance.UpdateHealth(curHealth);
        if (curHealth <= 0)
            Die();
    }  

    public void Die()
    {
        PauseMenu.instance.GameOver(false);
    }
}
