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

public class Gameplay : MonoBehaviour
{
    public GameObject[] containers;
    public Transform pos1,pos2,posdown,posup,conpos1,conpos2;
    public GameObject Coin;
    public int peremesh,con1,con2,i,coinpos;
    public float speed = 0.1f;
    public bool Move = false;
    public bool Stop = false;
    public bool hited = false;
    public Text Itog;
    public GameObject restart;
    void Awake()
    {
 Coin.transform.position = new Vector3 (containers[coinpos].transform.position.x,-0.4f,0f);
    }
    void Start()
    {
      coinpos = Random.Range(0,containers.Length);

    }
    void Update()
    {
     Coin.transform.position = new Vector3 (containers[coinpos].transform.position.x,-0.4f,0f);
      if (Move == true)
      {
        StartCoroutine(Mesh());
      }
      if (Stop == true)
      {
        StopAllCoroutines();
        if(i < peremesh)
        {
          Count();
        }
        if(i == peremesh)
        {
          Coin.SetActive(true);
          Itog.text = "Выберите стакан";
          if(Input.GetMouseButtonDown(0))
          {
          Ray ray= Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
           RaycastHit[] hits = Physics.RaycastAll(ray,Mathf.Infinity);
           foreach (var hit in hits)
           {
             if(hit.transform.tag == "Stakan")
             {
               hit.transform.Translate(new Vector3(0f,-0.5f,0f));   
             }
              if(hit.transform.tag == "Coin")
               {
                 Itog.text = "Вы победили!";
                hited = true;
                 restart.SetActive(true);
                 i = i+1;
               }
        
           }
             if(hited == false)
               {
                  Itog.text = "Вы проиграли";
                   restart.SetActive(true);
                     i = i+1;
               }
           }
          }
        }
      }
    
   public void Count()
   {
           Stop = false;
           con1 = Random.Range(0,containers.Length);
           con2 = Random.Range(0,containers.Length);
          if(con1 == con2)
          {
              while(con1== con2)
              {
             con2 = Random.Range(0,containers.Length);
              }
          }
          pos1.position = posup.transform.position;
          pos2.position = posdown.transform.position;
           conpos1.transform.position= containers[con1].transform.position;
          conpos2.transform.position = containers[con2].transform.position;
          Move = true;
          
      
   }
   IEnumerator Mesh()
   {
              containers[con1].transform.position = Vector3.MoveTowards(containers[con1].transform.position,pos1.position,speed * Time.deltaTime);
              containers[con2].transform.position = Vector3.MoveTowards(containers[con2].transform.position,pos2.position,speed * Time.deltaTime);        
      if ((Vector3.Distance(containers[con2].transform.position,pos2.position) == 0f))
      {
        pos1.position = conpos2.position;
        pos2.position = conpos1.position;
      }
    yield return new WaitForSeconds(0.5f);
         i = i+1;
      Stop = true;
    yield break;
   }
    public void StartGame()
    {
 foreach (GameObject el in containers)
         {
             el.transform.Translate(new Vector3(0f,-0.5f,0f));
         }
        Coin.SetActive(false);
         Count();
         GameObject.Find("Start").SetActive(false);    
    }
       public void Restart()
       {
         SceneManager.LoadScene(0);
       } 
        public void Exit()
       {
        Application.Quit();
       } 
}