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


public class detectWifi : MonoBehaviour {
	
	public Transform[] wifi;
	public WiFi[] waifu;
	public GameObject alert;
	public GameObject buttonsetting;
	public GameObject connected;
	public Text status;
	
	public bool kematian;
	public bool bisa = true;
	public bool wifi1, wifi2, wifi3;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (!kematian)
		{
			//wifi 1
			if (Vector3.Distance(transform.position, wifi[0].transform.position) < 3 && waifu[0].active)
			{
				wifi1 = true;
				alert.SetActive(true);
				buttonsetting.SetActive(true);
			}
			
			//wifi 2
			else if (Vector3.Distance(transform.position, wifi[1].transform.position) < 3 && waifu[1].active)
			{
				wifi2 = true;
				alert.SetActive(true);
				buttonsetting.SetActive(true);
			}
			
			//wifi 3
			else if (Vector3.Distance(transform.position, wifi[2].transform.position) < 3 && waifu[2].active)
			{
				wifi3 = true;
				alert.SetActive(true);
				buttonsetting.SetActive(true);
			}
			else if ((Vector3.Distance(transform.position, wifi[0].transform.position) > 3 || !waifu[0].active)
			         || (Vector3.Distance(transform.position, wifi[1].transform.position) > 3 || !waifu[1].active)
				|| (Vector3.Distance(transform.position, wifi[2].transform.position) > 3 || !waifu[2].active))
			{
				for (int i = 0; i < waifu.Length; i++)
				{
					//waifu[i].enabled = false;
					waifu[i].connected = false;
				}
				wifi1 = false;
				wifi2 = false;
				wifi3 = false;
				alert.SetActive(false);
				buttonsetting.SetActive(false);
				status.text = "Disconnected";
				bisa = true;
			}
		}
		else
		{
			for (int i = 0; i < waifu.Length; i++)
			{
				//waifu[i].enabled = false;
				waifu[i].connected = false;
			}
			wifi1 = false;
			wifi2 = false;
			wifi3 = false;
			bisa = true;
			alert.SetActive(false);
			buttonsetting.SetActive(false);
			status.text = "Disconnected";
		}
	}
	
	
	public void ConnectWifi()
	{
		if (bisa)
		{bisa = false;
			StartCoroutine(connecting());
		}
	}
	
	IEnumerator connecting() {
		
		yield return new WaitForSeconds(.3f);
		connected.SetActive(true);
		if (wifi1)
		{
			//waifu[0].enabled = true;
			waifu[0].connected = true;
		}
		else if (wifi2)
		{
			//waifu[1].enabled = true;
			waifu[1].connected = true;
		}
		else if (wifi3)
		{
			//waifu[2].enabled = true;
			waifu[2].connected = true;
		}
		
		status.text = "Connected";
		yield return new WaitForSeconds(1.2f);
		connected.SetActive(false);

	}
	
}
