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

public class UIMain : MonoBehaviour
{
    public GameObject helpPrefab;
    public UITime timeBar;
    public UIPower powerBar;
    public UIMarkPage uiMarkPage;

    public GameObject title;
    bool titleUp = false;

    public GameManager gm;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    public void OnClickHelp()
    {
        helpPrefab.SetActive(true);
    }

    public void OnClickExit()
    {
        Application.Quit();
    }

    internal void ShowTime(bool v, int seconds) 
    {
        this.timeBar.gameObject.SetActive(v);
        timeBar.CountTime(seconds);
    }

    internal void ShowPowerBar(bool v)
    {
        this.powerBar.gameObject.SetActive(v);
    }

    internal void ShowMarkPage(int starCount, bool show)
    {
        uiMarkPage.SetStars(starCount);
        this.uiMarkPage.gameObject.SetActive(show);
    }

    internal void OnFirstWater()
    {
        if (!titleUp)
        {
            StartCoroutine(MoveToTarget(title, title.transform.position + Vector3.up * 1000, 3));
            titleUp = true;
        }
        ShowPowerBar(true);
        ShowTime(true, 60);
        
    }

    IEnumerator MoveToTarget(GameObject go, Vector2 target, float duration)
    {
        RectTransform rect = go.GetComponent<RectTransform>();
        Vector3 startPosition = rect.position;  // 记录起始位置
        float elapsedTime = 0f;  // 经过的时间

        while (elapsedTime < duration)
        {
            rect.position = Vector2.Lerp(startPosition, target, elapsedTime / duration); // 线性插值
            elapsedTime += Time.deltaTime;  // 累加时间
            yield return null;  // 等待下一帧
        }

        // 确保最终位置为目标位置（防止由于精度问题没达到目标）
        rect.position = target;
    }

    public void OnClickNextChapter()
    {
        this.ShowMarkPage(0,false);
        gm.StartChapter2();
    }

    public void OnClickAgain()
    {

    }
}
