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

public class GameController : MonoBehaviour
{
    public Animator car_animator;
    public GameObject car;
    public Canvas MainCanvas;
    public GameObject playSpace;
    public Material[] materials;
    public GameObject EnginePlaySpace, Tire, Screw;
    public Image Bar, TireBar;
    public Text score_text;
    public Text objectives;
    //private string[] list;

    public AudioClip car_leaving, car_arriving;
    private AudioSource source;

    // Start is called before the first frame update
    public void updateTimer()
    {
        

    }
    
    void Start()
    {
        car_animator = car.GetComponent<Animator>();
        source = GetComponent<AudioSource>();
        source.PlayOneShot(car_arriving);
        //list = objectives.text.Split("\n"[0]);
    }

    // Update is called once per frame
    void Update()
    {
       
        if (car_animator.GetCurrentAnimatorStateInfo(0).IsName("GamePHase"))
        {
            MainCanvas.enabled = true;
        }

        else
            MainCanvas.enabled = false;
    }

    public void StartEngineMiniGame()
    {
        playSpace.SetActive(true);


    }

    public void submitButton()
    {
        car_animator.SetBool("Setup",true);
        //if (!source.isPlaying)
            source.PlayOneShot(car_leaving);
        checkOilGame();
        checkTireGame();
        checkEngineGame();
        CheckScrewGame();
        //FindObjectOfType<DragTire>().transform.position = FindObjectOfType<DragTire>().initPos;
        Tire.transform.position = Tire.GetComponent<DragTire>().initPos;
        StartCoroutine(resetSetupState());
    }

    IEnumerator resetSetupState()
    {
        //car_animator.SetBool("Setup", false);
        yield return new WaitForSeconds(1);

        EnginePlaySpace.GetComponent<EngineMinigameController>().resetEngineMiniGame();
        car_animator.SetBool("Setup", false);
        if (!source.isPlaying)
            source.PlayOneShot(car_arriving);
        Bar.fillAmount = 0;
        TireBar.fillAmount = 0;
        MainCanvas.enabled = false;
        Screw.GetComponent<ScrewMiniGame>().resetScrewGame();

        int random = Random.Range(0, 10);
        car.transform.GetChild(0).GetComponent<MeshRenderer>().material = materials[random];

    }

    void checkEngineGame()
    {
        if (EnginePlaySpace.GetComponent<EngineMinigameController>().checkEngineStatus())
        {
            int score = int.Parse(score_text.text);
            score += 30;
            score_text.text = score.ToString();
        }
         
    }

    void CheckScrewGame()
    {
        if (Screw.GetComponent<ScrewMiniGame>().isFixed)
        {
            int score = int.Parse(score_text.text);
            score += 50;
            score_text.text = score.ToString();
        }

    }


    void checkOilGame()
    {
        int score;
        if (Bar.fillAmount.Equals(1))
        {
            score = int.Parse(score_text.text);
            score += 20;
            score_text.text = score.ToString();
        }
      
    }

    void checkTireGame()
    {
        int score;
        if (TireBar.fillAmount > 0.94f)
        {
            score = int.Parse(score_text.text);
            score += 40;
            score_text.text = score.ToString();
        }
    }


    public void revealEnginePlaySpace()
    {
        hideExcept("EngineMinigame");
    }

    public void revealTirePlaySpace()
    {

        hideExcept("TireMiniGame");
    }

    public void revealOilPlaySpace()
    {

        hideExcept("OilMiniGame");

    }

    public void revealScrewPlaySpace()
    {

        hideExcept("ScrewMiniGame");
    }

    public void hideExcept(string spaceName)
    {
        for (int i = 0; i < playSpace.transform.childCount; i++)
        {
            if (playSpace.transform.GetChild(i).name == spaceName)
            {
                playSpace.transform.GetChild(i).gameObject.SetActive(true);

            }
            else
                playSpace.transform.GetChild(i).gameObject.SetActive(false);
        }
    }

    public void hideAllPlaySpaces()
    {
        for (int i = 0; i < playSpace.transform.childCount; i++)
        {
            {
                playSpace.transform.GetChild(i).gameObject.SetActive(false);
            }
        }
    }
}
