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

public class Monster {

    private List<Elements> elements;
    private Dictionary<Attributes, int> attributes;
    private string titel;
    private Player owner;

    private int health;
    private int attack;
    private int dexterity;
    private int luck;
    private bool isDead;

    private string configData1;
 

    public Monster(Player owner) {
        this.elements = new List<Elements>(3);
        this.isDead = false;

        Attributes[] attributes = (Attributes[])System.Enum.GetValues(typeof(Attributes));
        this.attributes = new Dictionary<Attributes, int>(attributes.Length);
        for(int i = 0; i < this.attributes.Count; i++) {
            this.attributes.Add(attributes[i], 0);
        }

    }
	

    public void AddElement(Elements element) {
        elements.Add(element);

        if(GetElementsCount() == 3){
            SetAttributes();
        }
    }


    private void SetAttributes() {

        configData1 = GameMaster.configData;
        var jsonData = JSON.Parse(configData1);

        for (int i = 0; i < this.elements.Count; i++)
        {
            switch (elements[i])
            {
                case Elements.FIRE:
                    
                    Helper.replaceKey(this.attributes, Attributes.ATTACK, jsonData["elements"]["fire"]["attack"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEFENSE, jsonData["elements"]["fire"]["defense"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEXTERITY, jsonData["elements"]["fire"]["dexterity"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.LUCK, jsonData["elements"]["fire"]["luck"].AsInt);
                    break;

                case Elements.EARTH:

                    Helper.replaceKey(this.attributes, Attributes.ATTACK, jsonData["elements"]["earth"]["attack"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEFENSE, jsonData["elements"]["earth"]["defense"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEXTERITY, jsonData["elements"]["earth"]["dexterity"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.LUCK, jsonData["elements"]["earth"]["luck"].AsInt);
                    break;

                case Elements.AIR:

                    Helper.replaceKey(this.attributes, Attributes.ATTACK, jsonData["elements"]["air"]["attack"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEFENSE, jsonData["elements"]["air"]["defense"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEXTERITY, jsonData["elements"]["air"]["dexterity"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.LUCK, jsonData["elements"]["air"]["luck"].AsInt);
                    break;

                case Elements.WATER:

                    Helper.replaceKey(this.attributes, Attributes.ATTACK, jsonData["elements"]["water"]["attack"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEFENSE, jsonData["elements"]["water"]["defense"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.DEXTERITY, jsonData["elements"]["water"]["dexterity"].AsInt);
                    Helper.replaceKey(this.attributes, Attributes.LUCK, jsonData["elements"]["water"]["luck"].AsInt);
                    break;

            }
        }

        SetAttack(attributes[Attributes.ATTACK], jsonData["basics"]["attack"].AsInt);
        SetHealth(attributes[Attributes.DEFENSE], jsonData["basics"]["defense"].AsInt);
        SetDexterity(attributes[Attributes.DEXTERITY], jsonData["basics"]["dexterity"].AsInt);
        SetLuck(attributes[Attributes.LUCK], jsonData["basics"]["luck"].AsInt);

    }


    private void SetTitel() {

    }

    private void SetHealth(int defenseAttribute, int basicValue) {
        this.health = basicValue + 3 * defenseAttribute;
    }

    private void SetAttack(int attackAttribute, int basicValue) {
        this.attack = basicValue + 2 * attackAttribute;
    }

    private void SetDexterity(int dexterityAttribute, int basicValue) {
        this.dexterity = basicValue + 2 * dexterityAttribute;
    }

    private void SetLuck(int luckAttribute, int basicValue) {
        this.luck = basicValue + 2 * luckAttribute;
    }


    public int GetHealth(){
        return health;
    }

    public int GetAttack(){
        return attack;
    }

    public int GetDexterity(){
        return dexterity;
    }

    public int GetLuck(){
        return luck;
    }

    public bool GetIsDead(){
        return isDead;
    }

    public void TakeDamage(int damage) {
        health = health - damage;

        if(health <= 0) {
            isDead = true;
        }
    }

    public int GetElementsCount() {
        return elements.Count;
    }

	public List<Elements> GetElements() {
		return elements;
	}
}
