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

public class CleaverWeapon : BasicWeapon
{
    [SerializeField] private AudioSource cleave;
    private const string CLEAVE = "Cleave";
    private const string IDLE_CLEAVER = "IdleCleaver";
    // public Ownership owner;
    [SerializeField] private CleaveZone cleaveZone;
    private Animator cleaverAnim;
    private CleaverAI enemyPsyche;

    protected override void Awake()
    {
        base.Awake();
        this.cleaverAnim = GetComponent<Animator>();
        enemyPsyche = GetComponent<CleaverAI>();
    }

    protected override void Update() {
        base.Update();
        this.cleaveZone.owner = this.owner;
        if(this.owner == Ownership.ENEMY) {
            enemyPsyche.enabled = true;
            this.health.owner = Ownership.ENEMY;
        }
        else if(this.owner == Ownership.PLAYER) {
            // if(enemyPsyche != null)
            //     Destroy(enemyPsyche);
            // enemyPsyche.enabled = false;
            PlayerManager.I.unit = GetComponent<CleaverUnitController>();
            this.health.owner = Ownership.PLAYER;
        }
    }

    void FixedUpdate() {
        if(this.cleaverAnim.GetCurrentAnimatorStateInfo(0).IsName(IDLE_CLEAVER)) {
            CleaveZone(false);
        }
    }

    public void Cleave() {
        if(!this.cleaverAnim.GetCurrentAnimatorStateInfo(0).IsName(CLEAVE)) {
            cleave.Play();
            this.cleaverAnim.SetTrigger(CLEAVE);
            CleaveZone(true);
        }
    }

    private void CleaveZone(bool isOn) {
        this.cleaveZone.Hazard(isOn);
    }
}
