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

public class SymbolChange : MonoBehaviour {

    public float imageChangeWaitTime = 2.0f;
    public int counterUntilImageChange = 330;

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

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

    public StateType Type {
        get {
            return busyType;
        }
    }

    public Color getColor() {
        return image.color;
    }

    private GameObject dominationType;
    private Image image;
    private StateType busyType = StateType.Mobile;
    private int counter;

	private LevelManager levelManager;

    void Awake() {
        image = GetComponent<Image>();
        dominationType = GameObject.Find("DominationType");
        counter = 0;
        dominationType.GetComponent<RectTransform>().anchoredPosition = new Vector2(410f, -165f);
		levelManager = GameObject.Find("LevelManager").GetComponent<LevelManager>();
    }

    void Start () {
        StartCoroutine(ChangeDominationType());
    }

    private void Update() {
		if (counter == 0 && levelManager.LevelRunning) {
            StartCoroutine(ChangeDominationType());
        }
        counter -= 1;

//		if(!levelManager.LevelRunning) {
//			gameObject.SetActive(false);
//		}
    }

    IEnumerator ChangeDominationType() {
        MoveDominationImage("right");
        yield return new WaitForSeconds(imageChangeWaitTime);
		ChangeSymbol();
        MoveDominationImage("left");
        counter = counterUntilImageChange;
    }

	void ChangeSymbol() {
		StateType randomState = validStates[Random.Range(0, validStates.Length)];
		busyType = randomState;

		switch(randomState) {
		case StateType.Facebook:
			image.sprite = Facebook;
			break;
		case StateType.GooglePlus:
			image.sprite = GooglePlus;
			break;
		case StateType.HeartPlus:
			image.sprite = HeartPlus;
			break;
		case StateType.Instagram:
			image.sprite = Instagram;
			break;
		case StateType.Linkedin:
			image.sprite = Linkedin;
			break;
		case StateType.Pinterest:
			image.sprite = Pinterest;
			break;
		case StateType.RSSA:
			image.sprite = RSSA;
			break;
		case StateType.Tinder:
			image.sprite = Tinder;
			break;
		case StateType.YouTube:
			image.sprite = YouTube;
			break;
		}
	}
    
    void ChangeColor() {
        int color = Random.Range(0,3);
        switch(color) {
            case 0:
                image.color = Color.red;
                break;
            case 1:
                image.color = Color.yellow;
                break;
            case 2:
                image.color = Color.magenta;
                break;
            case 3:
                image.color = Color.cyan;
                break;
        }
    }

    void MoveDominationImage(string position) {
        if (position.Equals("left")) {
            dominationType.GetComponent<Animator>().SetTrigger("floatLeft");
        }
        if (position.Equals("right")) {
            dominationType.GetComponent<Animator>().SetTrigger("floatRight");
        }
    }
}
