using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

/* 
	 * PlayerStats: Stores all numerical stats of player.  For player appearance, see setAppearance.cs
	 * - Any changes to health, ingame stats, currency, etc will use a getting and/or setting from here
	 * - This scripts should always be a component of "Player" (or the root object containing all player parts)
	 */
public class PlayerStats : MonoBehaviour {

	public static float maxHealth;
	public static float curHealth;
	public static string playerName;
	public static int vitality;
    public static int strength;
    public static int defense;
    public static int agility;
    public static int luck;

	private static int money; // to be manipulated by shops
    //below will act as the invetory
    public static int healthPotions;
    public static int strengthPotions;
    public static int defensePotions;
    public static int agilityPotions;
    public static bool offenseBlessing;
    public static bool defenseBlessing;
    public static bool luckBlessing;
	private bool created; // Was the character creation used? (debugging)

	public WeaponStats defaultWeapon; // Incase currentWeapon not set properly.  Set this in inspector!
    public ArmourStats defaultShield;
    private static ArmourStats currentShield;
	private static WeaponStats currentWeapon;
	EnterDoor spawn;

	void Start() {

        if (SceneManager.GetActiveScene().name == "arena") { // apply blessings
            heal(); // CHANGE THIS -- WILL AUTO HEAL THE CHARACTER ON ENTRY OF THE ARENA
            if (getOffenseBlessing() == true) {
                agility += 1;
                strength += 1;
            }
            if (getDefenseBlessing() == true) {
                defense += 1;
                vitality += 1;
            }
            if (getLuckBlessing() == true) {
                luck += 2;
            }
			if (!created) {
				setDefaultStats();
			}
        }

        //Debug.Log("Player Stats:\n\nname: " + playerName + "\nvit: " + vitality + "\nstr: " + strength + "\ndef: " + defense + "\nagi: " + agility + "\nluck: " + luck);
        Debug.Log("Bliessings:\noffense: " + offenseBlessing + "\ndefense: " + defenseBlessing + "\nluck: " + luckBlessing);

        updateWeapon();
        updateShield();
    }

	public void Damage(int amount) {
		curHealth -= amount/defense;
		if (curHealth <= 0) {
			StartCoroutine("waitandload");
		}
	}

	public int getDamageOutput(){
		int final = Random.Range (currentWeapon.damageMin, currentWeapon.damageMax);
		final += strength;
		return final;
	}

	IEnumerator waitandload() {
        if (SceneManager.GetActiveScene().name == "arena") { // deactivate blessings
            if (getOffenseBlessing() == true) {
                agility -= 1;
                strength -= 1;
                setOffenseBlessing(false);
            }
            if (getDefenseBlessing() == true) {
                defense -= 1;
                vitality -= 1;
                setDefenseBlessing(false);
            }
            if (getLuckBlessing() == true) {
                luck -= 2;
                setLuckBlessing(false);
            }
        }
        //leave time for a death animation and a defeat message, then load the main scene
        yield return new WaitForSeconds(2.0f);
		SceneManager.LoadScene("Town01"); // ** will have to change this with more towns
	}

	public void ChangeHealthBarp(int damage) {
		GameObject healthbar = GameObject.Find("healthbarFullp");
		float newsize = healthbar.transform.localScale.x - ((damage / maxHealth) * 0.3f);
		healthbar.transform.localScale = new Vector3(newsize, 0.3f, 1f);
		if (curHealth <= 0) {
			healthbar.GetComponent<SpriteRenderer>().enabled = false;
		}
	}

	private void updateWeapon() {
		GameObject weapon = GameObject.FindWithTag("currentWeapon"); // Get player weapon from scene
		SpriteRenderer spriteRenderer = weapon.GetComponent<SpriteRenderer> ();
		if (currentWeapon != null) {
			spriteRenderer.sprite = currentWeapon.image; // Set correct image of current weapon
		}
		else {
			currentWeapon = defaultWeapon;
			spriteRenderer.sprite = defaultWeapon.image; // Incase somethings wrong with currentWeapon
		}

	}

    private void updateShield() {
        GameObject armour = GameObject.FindWithTag("currentShield"); // Get player shield
        SpriteRenderer spriteRenderer = armour.GetComponent<SpriteRenderer>();
        if (currentShield != null) {
            spriteRenderer.sprite = currentShield.image; // Set correct image of current shield
        }
        else {
            currentShield = defaultShield;
            spriteRenderer.sprite = defaultShield.image; // Incase somethings wrong with currentShield
        }

    }

    // needed for shop buttons
    public WeaponStats getWeapon() {
        return currentWeapon;
    }

    public ArmourStats getShield() {
        return currentShield;
    }

    public static float getHealth() {
		return curHealth;
	}

	public void setWeapon(WeaponStats newWeapon) {
		currentWeapon = newWeapon;
		updateWeapon();
	}

    public void setShield(ArmourStats newShield) {
        currentShield = newShield;
        updateShield();
    }

	public void setName(string newName) {
		playerName = newName;
	}

	public void setVitality(int newVitality) {
		vitality = newVitality;
		setMaxHealth(); // Vitality affects HP amount
	}

	// Calculated from vitality
	public void setMaxHealth() { // public to set portrait
		maxHealth = 10+vitality * 10; // base health of 10
	}

	public void setStrength(int newStrength) {
		strength = newStrength;
	}

	public void setDefense(int newDefense) {
		defense = newDefense;
	}

	public void setAgility(int newAgility) {
		agility = newAgility;
	}

	public void setLuck(int newLuck) {
		luck = newLuck;
	}

    // money methods
	public void changeMoney(int incr){
		money += incr;
	}

	public int getMoney(){
		return money;
	}

    public void heal() {
        curHealth = maxHealth;
    }

    // inventory methods - for buying and consuming potions, blessing stuff
    public void addHealthPot(int amount) {
        healthPotions += amount;
    }
    public void addStrengthPot(int amount) {
        strengthPotions += amount;
    }
    public void addDefensePot(int amount) {
        defensePotions += amount;
    }
    public void addAgilityPot(int amount) {
        agilityPotions += amount;
    }
    public void subtractHealthPot(int amount) {
        healthPotions -= amount;
    }
    public void subtractStrengthPot(int amount) {
        strengthPotions -= amount;
    }
    public void subtractDefensePot(int amount) {
        defensePotions -= amount;
    }
    public void subtractAgilityPot(int amount) {
        agilityPotions -= amount;
    }
    // blessings
    public void setOffenseBlessing(bool option) {
        offenseBlessing = option;
    }
    public void setDefenseBlessing(bool option) {
        defenseBlessing = option;
    }
    public void setLuckBlessing(bool option) {
        luckBlessing = option;
    }

    public int getHealthPot() { // maybe to be used by A.I. decisions based on how many potions the player has?
        return healthPotions;
    }
    public int getStrengthPot() {
        return strengthPotions;
    }
    public int getDefensePot() {
        return defensePotions;
    }
    public int getAgilityPot() {
        return agilityPotions;
    }
    public bool getOffenseBlessing() {
        return offenseBlessing;
    }
    public bool getDefenseBlessing() {
        return defenseBlessing;
    }
    public bool getLuckBlessing() {
        return luckBlessing;
    }

    public string getName() {
        return playerName;
    }

	public void wasCreated(){
		created = true;
	}

	private void setDefaultStats(){
		setName ("No Name");
		setVitality (5);
		curHealth = maxHealth;
		setStrength (5);
		setDefense (5);
		setAgility (5);
		setLuck (5);
	}
}