using UnityEngine;
using System.Collections;

public class GameHandler : MonoBehaviour {

	public static GameHandler gameHandler;

	public static int index=0;

	/// <summary>
	/// 0=Question
	/// 1=Answer A
	/// 2=Answer B
	/// </summary>
	public UILabel[] qLabels;
	public UILabel context;
	public DataLoader dl;
	public GameObject mainMenu;
	public GameObject gameOverMenu;
	public GameObject winMenu;
	/// <summary>
	/// 0=Context
	/// 1=Question
	/// </summary>
	public GameObject[] mainGameMenus;
	public UITexture[] dude;
	public UITexture gameOverMessage;
	public QuestionController qc;
	public AudioSource sceneAudio;
	public AudioClip[] audios;
	
	/// <summary>
	/// 0=A
	/// 1=B
	/// </summary>
	public static int lvl;
	int count;
	int min;
	int answer;
	int sum;

	void OnEnable(){
		gameHandler=this;
		dl=DataLoader.dataLoader;
		lvl=0;
		count=0;
		dude[1].enabled=true;
		SetLevel();
	}


	
	// Use this for initialization
	
	public void SetLevel(){
		//sum=0;
		sceneAudio.clip=audios[lvl];
		sceneAudio.Play();
		switch(lvl){
			case 0:
				dude[0].mainTexture=Resources.Load("Textures/Scenario-Lemonade-Back") as Texture2D;
				dude[1].mainTexture=Resources.Load("Textures/Scenario-Lemonade-Front") as Texture2D;
			break;
			case 1:
				dude[0].mainTexture=Resources.Load("Textures/Scenario-Whacks-Back") as Texture2D;
				dude[1].mainTexture=Resources.Load("Textures/Scenario-Whacks-Front") as Texture2D;
			break;
			case 2:
				dude[0].mainTexture=Resources.Load("Textures/Scenario-Petro-Back") as Texture2D;
				dude[1].mainTexture=Resources.Load("Textures/Scenario-Petro-Front") as Texture2D;
			break;
			case 3:
				dude[0].mainTexture=Resources.Load("Textures/Scenario-President-Back") as Texture2D;
				dude[1].mainTexture=Resources.Load("Textures/Scenario-President-Front") as Texture2D;
			break;
			case 4:
				dude[0].mainTexture=Resources.Load("Textures/Scenario-Space-FIN") as Texture2D;
			dude[1].enabled=false;
			break;
		}
		string[] lvlData=dl.data[lvl+(lvl*3)].Split("|"[0]);
		context.text=lvlData[0];
		if(lvl<4)
			min=int.Parse(lvlData[1]);
		Resources.UnloadUnusedAssets();
	}

	public void StartLvl(){
		if(lvl<4){
			dude[1].enabled=true;
			mainGameMenus[0].SetActive(false);
			SetQuestion();
			mainGameMenus[1].SetActive(true);
		}
		else{
			qc.ResetQuestion();
			mainGameMenus[1].SetActive(false);
			mainGameMenus[0].SetActive(true);
			sceneAudio.clip=audios[4];
			sceneAudio.Play();
			winMenu.SetActive(true);
			lvl=0;
			count=0;
			sum=0;
			gameObject.SetActive(false);
		}
	}
	
	public void SetQuestion(){
		string[] qData=dl.data[count+lvl+(lvl*3)+1].Split("|"[0]);
		qLabels[0].text=qData[0];
		qLabels[1].text=qData[1];
		qLabels[2].text=qData[2];
		qLabels[3].text="What to do now?";
		answer=int.Parse(qData[3]);
	}

	public void Continue(){
		qc.isQ=true;
		if(count>=3){
			if(sum<min){
				qc.ResetQuestion();
				mainGameMenus[1].SetActive(false);
				mainGameMenus[0].SetActive(true);
				gameOverMenu.SetActive(true);
				dude[1].enabled=false;
				sceneAudio.clip=audios[5];
				sceneAudio.loop=false;
				sceneAudio.Play();
				//dude[1].mainTexture=Resources.Load("Textures/Post1") as Texture2D;
				if(lvl<=1){
					dude[0].mainTexture=Resources.Load("Textures/Fail1") as Texture2D;
					gameOverMessage.mainTexture=Resources.Load("Textures/Post1") as Texture2D;
				}
				else{
					dude[0].mainTexture=Resources.Load("Textures/Fail2") as Texture2D;
					gameOverMessage.mainTexture=Resources.Load("Textures/Post2") as Texture2D;
				}
				lvl=0;
				count=0;
				sum=0;
				//SetLevel();
				gameObject.SetActive(false);
			}
			else{
				count=0;
				lvl++;
				sum=0;
				//if(lvl<4){
					qc.ResetQuestion();
					mainGameMenus[1].SetActive(false);
					mainGameMenus[0].SetActive(true);
					SetLevel();
				/*}
				else{
					qc.ResetQuestion();
					mainGameMenus[1].SetActive(false);
					mainGameMenus[0].SetActive(true);
					sceneAudio.clip=audios[4];
					sceneAudio.Play();
					dude[0].mainTexture=Resources.Load("Textures/Scenario-Space-FIN") as Texture2D;
					dude[1].enabled=false;
					winMenu.SetActive(true);
					//gameOverMenu.SetActive(true);
					lvl=0;
					count=0;
					sum=0;
					//SetLevel();
					gameObject.SetActive(false);
				}*/
				
			}
		}
		else{
			qc.ResetQuestion();
			SetQuestion();
			
		}
	}

	public void AnswerButton(int i){
		if(lvl==1 && count==2){
			if(i==1){
				sum++;
			}
			else
				sum-=100;	
		}
		else{
			if(i==answer){
				sum++;
			}
			
			else
				sum--;
		}

		count++;
		int feedBack=Random.Range(0,15);
		string feedBackText="";
		switch(feedBack){
			case 0:
				feedBackText="People took notice...";
			break;
			case 1:
				feedBackText="They will remember...";
			break;
			case 2:
				feedBackText="They say God is dead...";
			break;
			case 3:
				feedBackText="Oh God...";
			break;
		case 4:
			feedBackText="No you didn't...";
			break;
		case 5:
			feedBackText="Think carefully...";
			break;
		case 6:
			feedBackText="Who hurt you?...";
			break;
		case 7:
			feedBackText="You crazy bastard...";
			break;
		case 8:
			feedBackText="They say the end justifies the means...";
			break;
		case 9:
			feedBackText="Oh God why?...";
			break;
		case 10:
			feedBackText="Please don’t...";
			break;
		case 11:
			feedBackText="Well, that’s certainly an option...";
			break;
		case 12:
			feedBackText="Literally worse than Hitler...";
			break;
		case 13:
			feedBackText="Humanity is watching..";
			break;
		default:
				feedBackText="You Bastard";
			break;
		}
		qLabels[0].text=feedBackText;
		qLabels[3].text="Continue...";
		qc.WhatButton();
		qc.isQ=false;

	}

}
