using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class ToggleButton : MonoBehaviour
{
    public bool isOn;

    public float handleOffset;
    public float moveSpeed;
    public float t = 0.0f;

    public GameObject handle;
    public TextMeshProUGUI timeScaleText;

    RectTransform handleTransform;
    public RectTransform toggle;

    public TimeScaleMod timeScaleMod;
    public KeyCode timeSpeedUpKey = KeyCode.Space;

    public Color normalSpeedColor;
    public Color fastSpeedColor;
    public Image toggleHandle;
    public Image speedIcon;

    public Color normalSpeedBGColor;
    public Color fastSpeedBGColor;
    public Image toggleBG;

    float handleSize;
    float onPosX;
    float offPosX;

    bool switching = false;

    public GameObject[] toggleElements;

    public void Awake()
    {
        handleTransform = handle.GetComponent<RectTransform>();

        handleSize = handleTransform.sizeDelta.x;
        float toggleSizeX = toggle.sizeDelta.x;
        onPosX = (toggleSizeX / 2) - (handleSize / 2) - handleOffset;
        offPosX = onPosX * -1;
    }

    void Start()
    {
        speedIcon.color = normalSpeedColor;
        toggleHandle.color = normalSpeedColor;
        toggleBG.color = normalSpeedBGColor;

        if (isOn)
        {
            handleTransform.localPosition = new Vector3(onPosX, 0, 0);
            timeScaleText.text = "x" + timeScaleMod.speedUpTimeScale;
        }
        else
        {
            handleTransform.localPosition = new Vector3(offPosX, 0, 0);
            timeScaleText.text = "x" + 1;
        }
    }

    void Update()
    {
        if (Manager.Instance.win || Manager.Instance.lost || !Manager.Instance.readyToPlay)
        {
            /*if (toggleElements[0].activeSelf) */for (int i = 0; i < toggleElements.Length; i++) toggleElements[i].SetActive(false);

            if (isOn) StartToggleing(isOn);
            if (Time.timeScale > 1) Time.timeScale = 1;
            return;
        }

        if (!toggleElements[0].activeSelf) for (int i = 0; i < toggleElements.Length; i++) toggleElements[i].SetActive(true);

        if (Input.GetKeyDown(timeSpeedUpKey) && Time.timeScale != 0) p_Switch();
        else if (Input.GetKeyUp(timeSpeedUpKey) && Time.timeScale > 1) p_Switch();

        if (switching)
        {
            StartToggleing(isOn);
        }

        if (isOn) timeScaleMod.SpeedUpTime();
        else timeScaleMod.ResetTimeScale();
    }

    void StartToggleing(bool tStatus)
    {
        if (tStatus)
        {
            handleTransform.localPosition = SmoothlyMove(handle, onPosX, offPosX);
            timeScaleText.text = "x" + 1;
            speedIcon.color = normalSpeedColor;
            toggleHandle.color = normalSpeedColor;
            toggleBG.color = normalSpeedBGColor;
        }
        else
        {
            handleTransform.localPosition = SmoothlyMove(handle, offPosX, onPosX);
            timeScaleText.text = "x" + timeScaleMod.speedUpTimeScale;
            speedIcon.color = fastSpeedColor;
            toggleHandle.color = fastSpeedColor;
            toggleBG.color = fastSpeedBGColor;
        }
    }

    Vector3 SmoothlyMove(GameObject handle, float startPosX, float endPosX)
    {
        Vector3 position = new Vector3(Mathf.Lerp(startPosX, endPosX, t += moveSpeed * Time.deltaTime), 0, 0);
        StopSwitching();
        return position;
    }

    void StopSwitching()
    {
        if (t > 1.0f)
        {
            switching = false;
            t = 0.0f;
            switch (isOn)
            {
                case true:
                    isOn = false;
                    break;
                case false:
                    isOn = true;
                    break;
            }
        }
    }
    public void p_Switch()
    {
        switching = true;
    }
}