using NUnit.Framework;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.TextCore.Text;
using TMPro;
using UnityEngine.EventSystems;

public class Manager : MonoBehaviour
{
    InputAction jumpAction;
    Animator animator;
    Rigidbody2D rb;
    //bool isonthefloor = true;
    Player charlie; //charlie is linking to the player code

    public GameObject platform;

    GameObject player;
    GameObject platform1;

    int numberplatform = 3;

    bool hasjumped = false;

    int score = 0;

    public TextMeshProUGUI scouretyper;

    GameObject camera;

    bool cmaerapanning = false;

    Vector3 camerapstion = new Vector3(0, 0, -10);

    // 初始方法,在MonoBehaviour创建后,方法只会被执行一次
    void Awake()
    {
        player = GameObject.Find("Player");
        jumpAction = InputSystem.actions.FindAction("Jump");
        platform1 = GameObject.Find("Platform");
        animator = player.GetComponent<Animator>();
        animator.SetBool("OnFloor", true);
        rb = player.GetComponent<Rigidbody2D>();
        charlie = player.GetComponent<Player>();
        GameObject platforms = GameObject.Instantiate(platform, platform1.transform.position + new Vector3(3.1f, 0, 0), Quaternion.identity);
        platforms.name = "2";
        camera = GameObject.Find("Main Camera");
    }

    //更新管理
    public void Isonthefloor()
    {
        if (charlie.isonthefloor == true && hasjumped == true)
        {
            Debug.Log("true");
            rb.velocity = new Vector2(0, rb.velocity.y);
            score++;
            hasjumped = false;
            Debug.Log("score" + score);
            cmaerapanning = true;
        }

        if (charlie.isonthefloor == false && hasjumped == false)
        {
            Debug.Log("false");
            hasjumped = true;
            camerapstion = camerapstion + new Vector3(3.1f, 0, 0);
        }
    }

    // 帧循环
    void Update()
    {
        Isonthefloor();
        animator.SetBool("OnFloor", charlie.isonthefloor);
        if (jumpAction.IsPressed() && (charlie.isonthefloor == true) && (cmaerapanning == false))
        {
            //animator.SetBool("ButtonPressed", true);
            rb.AddForce(new Vector2(250, 500));
            charlie.isonthefloor = false;

            GameObject previosPlatform = GameObject.Find((numberplatform - 1).ToString());
            GameObject platforms = GameObject.Instantiate(platform, previosPlatform.transform.position + new Vector3(3.1f, 0, 0), Quaternion.identity);
            platforms.name = numberplatform.ToString();
            numberplatform++;

        }

        if (cmaerapanning == true)
        {
            //previosPlatform p[laform we are on 
            camera.transform.Translate(Vector3.right * Time.deltaTime);
        }
        if (camera.transform.position.x > camerapstion.x)
        {
            cmaerapanning = false;
        }

        if (numberplatform > 10)
        {
            Destroy(platform1);
        }

        if (numberplatform > 11)
        {
            GameObject objecttoremove = GameObject.Find((numberplatform - 10).ToString());
            Destroy(objecttoremove);
        }

        /*else{
            animator.SetBool("ButtonPressed", false);
        }*/
        scouretyper.text = "成绩:" + score;
    }
}