using UnityEngine;
using System.Collections;

public class WeaponCollider : MonoBehaviour {

    // Attributes
    private LoadPlayer p;
    private PlayerStats stats;
    private AnimationPlayer animationPlayer;
    private bool ins = false;
    private int attack1;
    private int attack2;
    private int strenght;
    private int damage1;
    private int damage2;
    private int attackId;

    // Start.
	void Start () {
        this.p = (LoadPlayer) GameObject.Find("LoadPlayer").GetComponent<LoadPlayer>();
        this.animationPlayer = (AnimationPlayer) this.p.getAnimationPlayer();
        this.stats = this.p.stats;
        this.strenght = this.stats.getStrength();
    }

    // Update stats. Calc the damage values.
    public void updateStats() {
        this.damage1 = (int)(this.attack1 * 1 + (this.strenght / 100));
        this.damage2 = (int)(this.attack2 * 1 + (this.strenght / 100));
        this.printData();
    }

    // Trigger enter. 
    // If the collision object is an enemy and the player
    // is attacking, the combat begins.
    void OnTriggerEnter(Collider other) {
        GameObject coll = other.gameObject;
       
        if (coll.tag == "Enemy") {
            this.attackId = this.getAttackId();
            if (this.attackId != 0) ins = true;
        }
    }

    // Trigger stay. 
    // If the collision object is an enemy and the player is attacking
    // it, the DamageDealer makes its job.
    void OnTriggerStay(Collider other) {
        GameObject coll = other.gameObject;

        if (coll.tag == "Enemy") {
            if (ins) {
                if (this.attackId == 1) {
                    coll.GetComponent<DamageDealer>().setDamage(damage1);
                } else if (this.attackId == 2) {
                    coll.GetComponent<DamageDealer>().setDamage(damage2);
                }
                this.attackId = 0;
                ins = false;
            }
        }
    }


    // Just some debug tools.
    public void printData() {
        Debug.Log(attack1 + " " + attack2 + " " + damage1 + " " + damage2);
    }


    private bool IsPlayerAttacking() { return animationPlayer.getAttacking(); }
    private int getAttackId() { return animationPlayer.atid; }
    
    public void setAttack1 (int a) { this.attack1 = a; }
    public void setAttack2 (int a) { this.attack2 = a; }
    public void setAttacks (int a1, int a2) { this.setAttack1(a1); this.setAttack2(a2); }
    public void setStrength(int a) { this.strenght = a; }
    public void setDamage1 (int a) { this.damage1 = a; }
    public void setDamage2 (int a) { this.damage2 = a; }
    public void setDamages (int a1, int a2) { this.setDamage1(a1); this.setDamage2(a2); } 
}