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

public class WiFi : MonoBehaviour {

	public float baseDownloadSpeed;
	public float minTimeToLife,maxTimeToLife;
	public float minResetTime, maxResetTime;
	public float minSignalTime, maxSignalTime;
	public float timeCounter;
	public int signalStrength;
	public bool active;
	public float currentActiveTime, currentDeadTime;
	public TextMesh timeTextCounter;

	public GameObject[] wifiIcons;

	public Image sinyalnya;
	public bool connected;
	
	private GameManager gameManager;
	private GameObject player;
	public Sprite[] sinyal;

	void Start () {
		sinyal = Resources.LoadAll<Sprite>("Signal");
		gameManager = FindObjectOfType<GameManager> ();
		timeCounter = 0;
		currentActiveTime = Random.Range (minTimeToLife, maxTimeToLife);
		currentDeadTime = Random.Range (minResetTime, maxResetTime);
		signalStrength = Random.Range (1, 5);
		for(int i = 0; i<wifiIcons.Length; i++){
			wifiIcons[i].SetActive(i<=signalStrength);
		}

		player = GameObject.FindGameObjectWithTag("Player");

		changeColor ();
		StartCoroutine (signalChange ());
	}

	void Update(){
		if (gameManager.state.Equals(GameState.GAMEPLAY)) {
			transform.Rotate(0,30*Time.deltaTime,0);
			timeCounter+=Time.deltaTime;

			if(active){
				System.TimeSpan time = System.TimeSpan.FromSeconds (currentActiveTime - timeCounter);
				timeTextCounter.text = string.Format ("{1:D2}:{2:D2}",time.Hours, time.Minutes, time.Seconds);
				if(timeCounter>currentActiveTime){
					timeCounter = 0;
					active = false;
					currentActiveTime = Random.Range (minTimeToLife, maxTimeToLife);

					changeColor();
				}
			}else {
				System.TimeSpan time = System.TimeSpan.FromSeconds (currentDeadTime - timeCounter);
				timeTextCounter.text = string.Format ("{1:D2}:{2:D2}",time.Hours, time.Minutes, time.Seconds);
				if(timeCounter>currentDeadTime){
					timeCounter = 0;
					active = true;
					currentDeadTime = Random.Range (minResetTime, maxResetTime);
					changeColor();
				}
			}

		
			timeTextCounter.transform.rotation = Quaternion.LookRotation(timeTextCounter.transform.position - player.transform.position);
		}
	}
	
	void OnTriggerEnter(Collider other){
		if (connected) {
			if (active) {
				if (gameManager.state.Equals (GameState.GAMEPLAY)) {
					if (other.CompareTag ("Player")) {
						sinyalnya.GetComponent<Image> ().overrideSprite = sinyal [signalStrength];
					}
				}
			}
		}
	}

	void OnTriggerStay (Collider other){
		if (connected) {
			if (active) {
				if (gameManager.state.Equals (GameState.GAMEPLAY)) {
					if (other.CompareTag ("Player")) {
						gameManager.downloadCounter += baseDownloadSpeed * signalStrength / 5 * Time.deltaTime;
					}
				}
			}
		}
	}

	private void changeColor(){
		for (int i=0; i<wifiIcons.Length; i++) {

			wifiIcons[i].GetComponent<MeshRenderer>().material.color = active ? Color.green : Color.red;
			timeTextCounter.GetComponent<MeshRenderer>().material.color = active ? Color.green : Color.red;
		}
	}

	IEnumerator signalChange(){
		while (true) {
			yield return new WaitForSeconds(Random.Range(minSignalTime,maxSignalTime));
			signalStrength = Random.Range (1, 5);
			for(int i = 0; i<wifiIcons.Length; i++){
				wifiIcons[i].SetActive(i<=signalStrength);
			}
			sinyalnya.GetComponent<Image>().overrideSprite = sinyal[signalStrength];
		}
	}
}
