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

public enum StateType {
	Mobile, //Red
	TV, //Yellow
	Earphones, //Magenta
	Work, // Orange
	Idle, // Blue
	Thinking, // Green
	Facebook,
	GooglePlus,
	HeartPlus,
	Instagram,
	Linkedin,
	Pinterest,
	RSSA,
	Tinder,
	Twitter,
	YouTube
}

public class StateChange : MonoBehaviour {

	public float minTimeState = 1.0f;
	public float maxTimeState = 2.0f;
	public float probabilityBusy = 0.85f;
	public int thinkingDuration = 3;
	public int thinkingExtraClick = 1;

    public Sprite Facebook,
    GooglePlus,
    HeartPlus,
    Instagram,
    Linkedin,
    Pinterest,
    RSSA,
    Tinder,
    Twitter,
    YouTube;

    public AudioClip wrongSelection;
    public AudioClip keepThinking;

	public StateType State {
		get {
			return state;
		}
	}

	private Image image;
    private Image thought;
    private Text buttonText;
	private StateType state = StateType.Mobile;
	private int thinkingCounter;
	private LevelManager levelManager;
    private SymbolChange symbol;
    private Color[] cores = new Color[]{Color.red, Color.yellow, Color.magenta, Color.cyan};

    private StateType[] validStates = {
        StateType.Facebook,
        StateType.GooglePlus,
        StateType.HeartPlus,
        StateType.Instagram,
        StateType.Linkedin,
        StateType.Pinterest,
        StateType.RSSA,
        StateType.Tinder,
        StateType.Twitter,
        StateType.YouTube
    };

    private AudioSource audioSource;

    void Awake() {
		image = GetComponent<Image>();
		buttonText = transform.Find("Text").GetComponent<Text>();
		levelManager = GameObject.Find("LevelManager").GetComponent<LevelManager>();
        symbol = GameObject.Find("DominationType").GetComponent<SymbolChange>();
        audioSource = gameObject.GetComponent<AudioSource>();
        thought = transform.Find("Thought").GetComponent<Image>();
    }

	// Use this for initialization
	void Start () {
		InvokeRepeating ("ChangeState", 0.00001f, Random.Range(minTimeState, maxTimeState));
	}

	void ChangeState() {
        if (!transform.Find("Avatar").gameObject.activeInHierarchy) {
            transform.Find("Avatar").gameObject.SetActive(true);
        }
		if(Random.value <= probabilityBusy) {
            state = validStates[(Random.Range(0,10))];
            ChangeThought();
            image.color = Color.gray;// cores[(Random.Range(0, 3))];
            buttonText.text = "";// "Busy with\nMobile"; // Choose a proper description from the state
		}
	}

    void ChangeThought() {

        switch (state) {
            case StateType.Facebook:
                thought.sprite = Facebook;
                break;
            case StateType.GooglePlus:
                thought.sprite = GooglePlus;
                break;
            case StateType.HeartPlus:
                thought.sprite = HeartPlus;
                break;
            case StateType.Instagram:
                thought.sprite = Instagram;
                break;
            case StateType.Linkedin:
                thought.sprite = Linkedin;
                break;
            case StateType.Pinterest:
                thought.sprite = Pinterest;
                break;
            case StateType.RSSA:
                thought.sprite = RSSA;
                break;
            case StateType.Tinder:
                thought.sprite = Tinder;
                break;
            case StateType.YouTube:
                thought.sprite = YouTube;
                break;
        }
    }

    void Thinking() {
		if(thinkingCounter == 0) {
			CancelInvoke("Thinking");
			InvokeRepeating ("ChangeState", 0.00001f, Random.Range(minTimeState, maxTimeState));
		} else {
			buttonText.text = thinkingCounter.ToString();
			--thinkingCounter;
		}
	}

	public void Clicked() {
		if(state.Equals(symbol.Type)) {
			CancelInvoke("ChangeState");

			state = StateType.Thinking;
			image.color = Color.green;
			thinkingCounter = thinkingDuration;
			buttonText.text = thinkingCounter.ToString();
			InvokeRepeating("Thinking", 0.00001f, 1f);

			if(levelManager != null) {
				levelManager.CharIsThinking();
			}

		} else if (state == StateType.Thinking) {
            audioSource.clip = keepThinking;
            audioSource.Play();
            thinkingCounter = thinkingDuration;
			buttonText.text = "Thinking: " + thinkingCounter;
		} else {
            audioSource.clip = wrongSelection;
            audioSource.Play();
            transform.Find("Avatar").gameObject.SetActive(false);
            gameObject.GetComponent<Animator>().SetTrigger("static");
        }
	}
}
