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

using HamJam.Character;

public class HealthBarManagerScript : MonoBehaviour {

    [SerializeField]
    private GameObject _raidBossHealthBar;
    [SerializeField]
    private GameObject _heroHealthBar;
    [SerializeField]
    private float _heroBarOffsetVertical;
    [SerializeField]
    private float _heroBarOffsetHorizontal;
    [SerializeField]
    private float _raidBossBarOffsetVertical;
    [SerializeField]
    private float _raidBossBarOffsetHorizontal;

    private Dictionary<GameObject, GameObject> _healthBars;

    void Awake() {

        this._healthBars = new Dictionary<GameObject, GameObject>();
        var raidBoss = GameObject.FindGameObjectWithTag("RaidBoss");
        Vector2 barPos = raidBoss.transform.position;
        barPos.x += this._raidBossBarOffsetHorizontal;
        barPos.y += this._raidBossBarOffsetVertical;
        GameObject newBar = Instantiate(this._raidBossHealthBar, barPos, Quaternion.identity, this.transform) as GameObject;
        this._healthBars[raidBoss] = newBar;
        foreach (var hero in GameObject.FindGameObjectsWithTag("Hero")) {
            barPos = hero.transform.position;
            barPos.x += this._heroBarOffsetHorizontal;
            barPos.y += this._heroBarOffsetVertical;
            newBar = Instantiate(this._heroHealthBar, barPos, Quaternion.identity, this.transform) as GameObject;
            this._healthBars[hero] = newBar;
        }
    }

    void Update() {
        foreach (var key in this._healthBars.Keys) {
            CharacterInteractionScript charInteractScript = key.GetComponent<CharacterInteractionScript>();
            Vector2 newPos = key.transform.position;
            CharacterTypes charType = charInteractScript.CharType;
            if (CharacterTypes.Hero == charType) {
                newPos.x += this._heroBarOffsetHorizontal;
                newPos.y += this._heroBarOffsetVertical;
            } else if (CharacterTypes.RaidBoss == charType) {
                newPos.x += this._raidBossBarOffsetHorizontal;
                newPos.y += this._raidBossBarOffsetVertical;
            } else {
                throw new ArgumentException("The passed in object's char type was not handled!");
            }
            this._healthBars[key].transform.position = newPos;
            // Now Scale the health bar
            float healthPercentage = (charInteractScript.Health / charInteractScript.InitialHealth);
            if (0 > healthPercentage) {
                healthPercentage = 0;
            }
            Vector3 scaled = new Vector3(healthPercentage, 1, 1);
            this._healthBars[key].transform.localScale = scaled;
        }
    }

    public void TrackNewCharacter(GameObject charObj) {
        GameObject barType;
        Vector2 newPos = charObj.transform.position;
        var charType = charObj.GetComponent<CharacterInteractionScript>().CharType;
        if (CharacterTypes.Hero == charType) {
            barType = this._heroHealthBar;
            newPos.x += this._heroBarOffsetHorizontal;
            newPos.y += this._heroBarOffsetVertical;
        } else if (CharacterTypes.RaidBoss == charType) {
            barType = this._raidBossHealthBar;
            newPos.x += this._raidBossBarOffsetHorizontal;
            newPos.y += this._raidBossBarOffsetVertical;
        } else {
            throw new ArgumentException("The passed in object's char type was not handled!");
        }
        GameObject newBar = Instantiate(barType, newPos, Quaternion.identity, this.transform) as GameObject;
        this._healthBars[charObj] = newBar;
    }
}
