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

public enum LevelStatus
{
    Playing,
    Waiting,
}

public class GameManager : MonoBehaviour
{
    public LevelStatus levelStatus = LevelStatus.Waiting;
    float score = 0;


    public float forceStrength = 10.0f;
    public Pipe pipe;

    public UIScore uiScore;

    public Cup currentCup;//用杯子来标识关卡
    public Transform cupRoot;
    public Transform pipeRoot;

    public GameObject cup1;
    public GameObject cup2;

    public bool allowMouseDrag = false;
    //public bool firstWater = false;

    public float timer = 0;

    public UIMain uiMain;
    public SoundManager sm;

    public GameObject bgMan;
    Vector2 manPos;
    public GameObject bgManMouth;
    public GameObject cupTag;

    int levelIndex = 1;

    // Start is called before the first frame update
    void Start()
    {
        StartChapter1 ();
        manPos = bgMan.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            sm.PlaySound("click 1");
        }

        if (levelStatus == LevelStatus.Playing && Time.time > timer + 60)
        {
            StopChapter();
        }

        if (levelStatus == LevelStatus.Playing && currentCup.waterLevel.IsTooLow())
        {
            StopChapter ();
        }
    }


    //对每个珍珠产生一个吸力
    public void DragBubble(float power)
    {
        currentCup.DragBubble(power);

    }

    internal void RemoveBubble(Bubble bb)
    {
        float addscore = 0 ;
        switch (bb.type)
        {
            case BubbleType.Normal:
                addscore = 10;
                break;
            case BubbleType.Coconut:
                addscore = 10;
                break;
            case BubbleType.Sago:
                addscore = 4;
                break;
            case BubbleType.Ranbow:
                addscore = 20;
                break;
            case BubbleType.Taro:
                break;
            default:
                break;
        }
        currentCup.RemoveBubble(bb.gameObject);
        score += addscore;

        uiScore.SetScore(score);
    }

    public void StartChapter1()
    {
        
        pipe.transform.position = pipeRoot.transform.position;
        pipe.transform.rotation = pipeRoot.transform.rotation;
        GameObject go = Instantiate(cup1, cupRoot);
        currentCup = go.GetComponent<Cup>();
        currentCup.sipTimes = 30;
        currentCup.gm = this;
        currentCup.SpawnBubble(Config.sequence1);

    }

    //在每个关卡第一次吸管入水时候触发小动画
    public void PipeFirstInWater()
    {
        timer = Time.time;
        levelStatus = LevelStatus.Playing;
        //人物挪到上面去
        StartCoroutine(MoveToTarget(bgMan, bgMan.transform.position + Vector3.up * 11, 5));
        bgManMouth.gameObject.SetActive(false);
        pipe.ShowMouth(true);

        //让吸管的嘴物体显示

        //显示蓄力条
        uiMain.OnFirstWater();
    }

    //当用时结束或者水位下降到一定程度结束关卡
    public void StopChapter()
    {
        levelStatus = LevelStatus.Waiting;
        Destroy(currentCup.gameObject);

        int maxScore = currentCup.maxScore;

        float scorePercent = score / maxScore;
        int star = 0;
        if (scorePercent > 0.8f)
        {
            star = 3;
            sm.PlaySound("star 3");
        }
        else if (scorePercent > 0.6f)
        {
            star = 2;
            sm.PlaySound("star 2");
        }
        else if (scorePercent > 0.4f)
        {
            star = 1;
            sm.PlaySound("star 1");
        }
        else
            sm.PlaySound("star 0");
        score = 0;
        pipe.firstTime = true;
        pipe.isInWater = false;
        bgMan.transform.position = manPos;
        //弹出计分界面
        uiMain.ShowMarkPage(star, true);
    }

    public void StartChapter2()
    {
        
        pipe.transform.position = pipeRoot.transform.position;
        pipe.transform.rotation = pipeRoot.transform.rotation;
        GameObject go = Instantiate(cup2, cupRoot);
        currentCup = go.GetComponent<Cup>();
        currentCup.sipTimes = 30;
        currentCup.gm = this;
        currentCup.SpawnBubble(Config.sequence2);

    }


    // 协程：平滑移动到目标位置
    public IEnumerator MoveToTarget(GameObject go, Vector2 target, float duration)
    {
        Vector3 startPosition = transform.position;  // 记录起始位置
        float elapsedTime = 0f;  // 经过的时间

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

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