using UnityEngine;
using System.Collections;

using HamJam.Character;

public class SwordTriggerScript : MonoBehaviour {

    [SerializeField]
    private GameObject _mainCharacterObject;
    [SerializeField]
    private AttackTypes _attackType;

    private CharacterInteractionScript _mainCharController;
    private AttackDataManagerScript _attackDataManager;
    private SpriteRenderer sprite;

    void Awake() {
        this._mainCharController = this._mainCharacterObject.GetComponent<CharacterInteractionScript>();
        this._attackDataManager = GameObject.FindGameObjectWithTag("AttackDataManager").GetComponent<AttackDataManagerScript>();
        sprite = this.GetComponent<SpriteRenderer>();
    }

    void Update() {
        if (_mainCharController.CanAttack) {
            sprite.color = new Vector4(sprite.color.r, sprite.color.g, sprite.color.b,1);
        } else {
            sprite.color = new Vector4(sprite.color.r, sprite.color.g, sprite.color.b, 0.4f);
        }
    }

    void OnTriggerEnter2D(Collider2D coll) {
        if (!this._mainCharController.CanAttack) {
            return;
        }
        var collController = coll.GetComponent<CharacterInteractionScript>();
        if (null != collController) {
            this._mainCharController.PerformAttack(new Vector2(), this._attackType);
            float damageToDeal = this._attackDataManager.GetAttackDamage(this._attackType);
            collController.TakeDamage(damageToDeal);
        }
    }
}
