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

public class WinnerTimer : MonoBehaviour
{

    public bool status;
    public float timeRemaining, time;
    public Image selfSpr, bgSpr;
    public Text textTimer, textESC;
    public Sprite spr05, spr04,spr03,spr02,spr01, sprCivic, sprCovid;
    
    public string winner;

    public Controller controller;
    public GameObject people;
    // Start is called before the first frame update
    void Start()
    {
        gameObject.SetActive(false);
        controller = GameObject.Find("Controller").GetComponent<Controller>();
 
    }
    void ResetTimer()
    {
        status = false;
        gameObject.SetActive(false);
        timeRemaining = 5;
        time = 0;
        selfSpr.sprite = spr05;
        transform.localScale = new Vector3(0, 0, 0);
        selfSpr.SetNativeSize();
    }

    // Update is called once per frame
    void Update()
    {
        

        if (status)
        {
            if (controller.immunity > 0 && winner == "covid") {ResetTimer();}

            if (controller.immunity < 100 && winner == "civic") {ResetTimer();}

            if (time < 1)
            {
                if (time == 0 && timeRemaining == 0)
                {textTimer.text = "Время игровой сессии: "+ controller.textTimer.text + "\n("+ controller.time + ")";}

                time += Time.deltaTime;
                if (timeRemaining >= 0)
                {
                    if (timeRemaining > 0) {transform.localScale = new Vector3(time, time, 1f);}
                    else 
                    {
                        bgSpr.color = new Color (1f, 1f, 1f,time);
                        textTimer.color = new Color (1f, 1f, 0f,time);
                        textESC.color = new Color (0.6f, 0.7f, 0.8f,time);
                        
                        transform.localScale = new Vector3(time*1.5f, time*1.5f, 1f);
                    }
                }
            }
            else
            {
                if (timeRemaining > 0) 
                {
                    if (controller.immunity > 0 && winner == "covid") {ResetTimer();}

                    if (controller.immunity < 100 && winner == "civic") {ResetTimer();}

                    timeRemaining -= 1; 
                    time = 0;
                    transform.localScale = new Vector3(0, 0, 1f);
                }
                
                switch( (int)timeRemaining )
                {
                    case 5: selfSpr.sprite = spr05; break;
                    case 4: selfSpr.sprite = spr04; break;
                    case 3: selfSpr.sprite = spr03; break;
                    case 2: selfSpr.sprite = spr02; break;
                    case 1: selfSpr.sprite = spr01; break;
                    default: 
                        if (winner == "civic") {selfSpr.sprite = sprCivic;}
                        else{selfSpr.sprite = sprCovid;}
                        people.GetComponent<CircleCollider2D>().enabled = false;
                        people.SetActive(false);
                        if (Input.GetKeyDown(KeyCode.Escape))
                        {
                            SceneManager.LoadScene("Menu");
                            if (GameObject.Find("MusicPlayer(Clone)"))
                            {
                                Destroy(GameObject.Find("MusicPlayer(Clone)"));
                            }
                        }
                    break;
                }
                selfSpr.SetNativeSize();
            }
        }

    }

}
