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

public class UpgradeController : MonoBehaviour
{
    public static UpgradeController instance;
    public GameObject loseScreen;
    public bool winner;

    public float speedMult, dashMult, drugMult,
        speedUpgrade, dashUpgrade, drugUpgrade, copSpeedIncrease;

    public int speedLevel, dashLevel, drugLevel,
        speedBaseCost, speedCostInc,
        dashBaseCost, dashCostInc,
        drugBaseCost, drugCostInc, drugSellPrice, bribePrice;

    private float startSpeed, startDash, startDrug;

    //public bool active;



    void Awake()
    {
        instance = this;
        startSpeed = speedMult;
        startDash = dashMult;
        startDrug = drugMult;
        loseScreen.SetActive(false);
        gameObject.SetActive(false);
    }
    
	void Start ()
    {
	}
	
	void Update ()
    {
        if (loseScreen.active)
        {
            loseScreen.transform.FindChild("Bye").GetComponent<Text>().text = winner ?
                "CRIME DOES PAY!" : "CRIME DOESN'T PAY!";
            loseScreen.transform.FindChild("Score").GetComponent<Text>().text = winner ?
                "Money earned: $" + GameController.instance.money.ToString() :
                "";
            if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Escape))
            {
                Debug.Log("CRIME DOESN'T PAY!");
                GameController.instance.day = 1;
                GameController.instance.points = 0;
                GameController.instance.money = 500;
                reset();
                if (Input.GetKey(KeyCode.Space))
                {
                    GameController.instance.resetLevel();
                    loseScreen.SetActive(false);
                    gameObject.SetActive(false);
                }
                else
                    Application.LoadLevel("Title");
            }
        }
	}


    //0 - speed
    //1 - drug dash
    //2 - drug drop
    public void upgrade(int power)
    {
        if (loseScreen.active)
            return;
        GameController.instance.points -= getCost(power);
        switch (power)
        {
            case (0):
                speedMult += speedUpgrade;
                speedLevel++;
                break;
            case (1):
                dashMult += dashUpgrade;
                dashLevel++;
                break;
            case (2):
                drugMult += drugUpgrade;
                drugLevel++;
                break;
            default:
                break;
        }
    }

    public int getCost(int power)
    {

        switch (power)
        {
            case (0):
                return speedBaseCost + (speedCostInc * (speedLevel - 1));
            case (1):
                return dashBaseCost + (dashCostInc * (dashLevel));
            case (2):
                return drugBaseCost + (drugCostInc * (drugLevel - 1));
            default:
                return int.MaxValue;
        }
    }

    public int getLevel(int power)
    {
        switch (power)
        {
            case (0):
                return speedLevel;
            case (1):
                return dashLevel;
            case (2):
                return drugLevel;
            default:
                return 0;
        }
    }

    public void sellDrug()
    {
        if (loseScreen.active)
            return;
        GameController.instance.money += drugSellPrice;
        GameController.instance.points--;
        PlayerMovement.instance.source.PlayOneShot(PlayerMovement.instance.sellNoise);
    }

    public void sellAllDrugs()
    {
        if (loseScreen.active)
            return;
        GameController.instance.money += drugSellPrice * GameController.instance.points;
        GameController.instance.points = 0;
        PlayerMovement.instance.source.PlayOneShot(PlayerMovement.instance.sellNoise);
    }

    public void nextNight()
    {
        if (loseScreen.active)
            return;
            GameController.instance.day++;
        GameController.instance.resetLevel();
        gameObject.SetActive(false);
    }


    public void reset()
    {
        speedMult = startSpeed;
        dashMult = startDash;
        drugMult = startDrug;

        speedLevel = 1;
        dashLevel = 0;
        drugLevel = 1;
    }


}
