using UnityEngine;
using UnityEngine.UI;
using System.Collections;

// This is used for character creation only
public class ChooseCharStats : MonoBehaviour {

	public Text vitText;
	public Text strText;
	public Text defText;
	public Text agiText;
	public Text lucText;
	public Text unalocText;

	public static string playerName;
	private int vitality;
	private int strength;
	private int defense;
	private int agility;
	private int luck;
	public static int unallocated;

	private PlayerStats stats;
	// Use this for initialization
	void Start () {
		vitality = 5;
		strength = 5;
		defense = 5;
		agility = 5;
		luck = 5;
		unallocated = 10;
		stats = GameObject.Find("CharacterDisplay").GetComponent<PlayerStats>();
		updateScore ();
		setStats ();
	}
		
	public void setStats(){
		stats.setName (playerName);
		stats.setVitality (vitality);
		stats.setStrength (strength);
		stats.setDefense (defense);
		stats.setAgility (agility);
		stats.setLuck (luck);
	}
		
	private void updateScore(){
		unalocText.text = "Unallocated: " + unallocated.ToString ();
		vitText.text = "Vitality: " + vitality.ToString ();
		strText.text = "Strength: " + strength.ToString ();
		defText.text = "Defense: " + defense.ToString ();
		agiText.text = "Agility: " + agility.ToString ();
		lucText.text = "Luck: " + luck.ToString ();
	}

    public void randomStats()
    {
        //a tally of all 'potential' stat points... each individual stat must be at least 1, so I'm going to set the 'default' randoms to that.
        int tally = 30;
        int randomVit = 1;
        int randomStrength = 1;
        int randomDefense = 1;
        int randomAgility = 1;
        int randomLuck = 1;
        int statChoice = Random.Range(1, 6);
        int numberChangedBy;

        //(hopefully) adding all the numbers correctly and randomly...
        while (tally != 0)
        {
            //if vitality is chosen...
            if (statChoice == 1)
            {
                if (tally > 15) { numberChangedBy = Random.Range(1, (tally + 1) / 2); }
                else { numberChangedBy = Random.Range(1, tally + 1); }
                randomVit += numberChangedBy;
                tally -= numberChangedBy;
            }
            //if strength is chosen...
            if (statChoice == 2)
            {
                if (tally > 15) { numberChangedBy = Random.Range(1, (tally + 1) / 2); }
                else { numberChangedBy = Random.Range(1, tally + 1); }
                randomStrength += numberChangedBy;
                tally -= numberChangedBy;
            }
            //if defense is chosen...
            if (statChoice == 3)
            {
                if (tally > 15) { numberChangedBy = Random.Range(1, (tally + 1) / 2); }
                else { numberChangedBy = Random.Range(1, tally + 1); }
                randomDefense += numberChangedBy;
                tally -= numberChangedBy;
            }
            //if agility is chosen...
            if (statChoice == 4)
            {
                if (tally > 15) { numberChangedBy = Random.Range(1, (tally + 1) / 2); }
                else { numberChangedBy = Random.Range(1, tally + 1); }
                randomAgility += numberChangedBy;
                tally -= numberChangedBy;
            }
            //if luck is chosen...
            if (statChoice == 5)
            {
                if (tally > 15) { numberChangedBy = Random.Range(1, (tally + 1) / 2); }
                else { numberChangedBy = Random.Range(1, tally + 1); }
                randomLuck += numberChangedBy;
                tally -= numberChangedBy;
            }
            //setting the stat choice for the next iteration of the while loop...
            statChoice = Random.Range(1, 6);
        }
        vitality = randomVit;
        strength = randomStrength;
        defense = randomDefense;
        agility = randomAgility;
        luck = randomLuck;
        unallocated = tally;
        updateScore();
        setStats();
    }
    

	public void change_name(string newName){
		playerName = newName;
		playerName = "Testing"; // Delete me later
	}
	// Incrementing each field, used by + buttons
	public void inc_vit(){
		if (unallocated > 0) {
			unallocated--;
			vitality++;
			updateScore ();
			setStats ();
		}
	}
	public void inc_str(){
		if (unallocated > 0) {
			unallocated--;
			strength++;
			updateScore ();
			setStats ();
		}
	}
	public void inc_def(){
		if (unallocated > 0) {
			unallocated--;
			defense++;
			updateScore ();
			setStats ();
		}
	}
	public void inc_agi(){
		if (unallocated > 0) {
			unallocated--;
			agility++;
			updateScore ();
			setStats ();
		}
	}
	public void inc_luc(){
		if (unallocated > 0) {
			unallocated--;
			luck++;
			updateScore ();
			setStats ();
		}
	}

	//Decrementing each field, used by - buttons
	public void dec_str(){
		if (strength > 1) {
			strength--;
			unallocated++;
			updateScore ();
			setStats ();
		}
	}

	public void dec_vit(){
		if (vitality > 1) {
			vitality--;
			unallocated++;
			updateScore ();
			setStats ();
		}
	}

	public void dec_def(){
		if (defense > 1) {
			defense--;
			unallocated++;
			updateScore ();
			setStats ();
		}
	}

	public void dec_agi(){
		if (agility > 1) {
			agility--;
			unallocated++;
			updateScore ();
			setStats ();
		}
	}

	public void dec_luc(){
		if (luck > 1) {
			luck--;
			unallocated++;
			updateScore ();
			setStats ();
		}
	}
}