using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class StartCode : MonoBehaviour
{

    public bool fast;
    
    public Scrollbar scroll;

    public float effectTime = 2;

    public GameObject[] showPanels;

    public TimeController timeController;

    public GameController gameController;

    private bool line;

    public TextMeshProUGUI lineText;

    public GameObject lineGO;

    public GameObject goodText;

    private bool executed;

    public Camera myCamera;
    public float endSize;
    public Vector3 endPosition;
    
    private void Start()
    {
        InvokeRepeating("Change",0.5f,0.5f);
    }

    private void Change()
    {
        if (line)
        {
            lineText.text = "_";
        }
        else
        {
            lineText.text = "";
        }
        
        line = !line;
            
    }

    private void Update()
    {
        if (executed)
            return;

        if (Input.GetMouseButtonDown(0))
        {
            executed = true;
            Process();
        }
    }

    private void Process()
    {
        CancelInvoke("Change");
        lineGO.gameObject.SetActive(false);
        goodText.gameObject.SetActive(true);
        
        float val = 1;
        DOTween.To(() => val, x => val = x, 0, effectTime).OnUpdate(() => { scroll.value = val; }).OnComplete(() =>
        {
            gameObject.SetActive(false);
            foreach (GameObject showPanel in showPanels)
            {
                showPanel.SetActive(true);
            }
            timeController.Activate();
            
            myCamera.DOOrthoSize(endSize, 5).SetDelay(1);
            myCamera.transform.DOMove(endPosition, 5).SetDelay(1).OnComplete(() => gameController.started = true);
        });

        
    }
}
