using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using SmartBowSDK;
using System;
using HyperspaceGame;
using MathNet.Numerics;
public class GeneratingTarget : MonoBehaviour
{
    // 最小时间间隔(秒)
    float _minTimeBetweenEvents = 1.0f;
    public float minTimeBetweenEvents { get { return _minTimeBetweenEvents; } set { _minTimeBetweenEvents = value; } }
    float _maxTimeBetweenEvents = 3.0f;// 最大时间间隔(秒)
    public float maxTimeBetweenEvents { get { return _maxTimeBetweenEvents; } set { _maxTimeBetweenEvents = value; } }
    public GameObject target;
    public Transform UI;
    public TextMeshProUGUI scoreUI;
    public TextMeshProUGUI scoreUI1;
    public Text countDownUI;
    public TextMeshProUGUI clipsizeUI;
    public TextMeshProUGUI hitCountUI;
    public TextMeshProUGUI shootRate;
    public GameObject GameOver;
    public Score scoreCom;
    public Score scoreCom1;
    public GameObject Miss;
    public Image grade;
    public Transform tp;
    public GameObject bulletNull;//子弹不足UI
    public GameObject[] bulletImages;//子弹图片数组
    public GameObject SelectLv;
    public Image imgSlider;
    public Text TopScore;
    public Text CurLvTxt;
    public Text MaxLvTxt;
    public int bulletCount;//子弹数量
    public int hitCount;//击中数量
    private float rate;//命中率
    public Button BtnRestart;
    public Button BtnNext;
    public Button BtnLast;
    int _score;
    public bool stop = false;
    public int score { get { return _score; }
        set {
            _score = value;
            ShowScore();
        }
    }
    /// 
    /// 游戏时间
    /// 
    private float countDownTime;
    int countDownTimeCache;
    /// 
    /// 子弹数量
    /// 
    private int clipsize; 
    const int BulletCount = 15;
    /// 
    /// 靶子存在最长时间
    /// 
    float _targetExistenceTimeMax = 5;
    public float TargetExistenceTimeMax { get { return _targetExistenceTimeMax; } set { _targetExistenceTimeMax = value; } }
    /// 
    /// 靶子存在最短时间
    /// 
    float _targetExistenceTimeMin = 3;
    public float TargetExistenceTimeMin { get { return _targetExistenceTimeMin; } set { _targetExistenceTimeMin = value; } }
    float _scaleMin = 1.3f;
    public float ScaleMin { get { return _scaleMin; } set { _scaleMin = value; } }
    float _scaleMax = 1.8f;
    public float ScaleMax { get { return _scaleMax; } set { _scaleMax = value; } }
    public HyperspaceGame.Font font;
    public AudioSource ShootingSound;
    public AudioSource gameSatrtSound;
    public AudioClip[] gameSatrtClips;
    //public GameObject perfect;
    //public GameObject gaeat;
    //public GameObject good;
    //public Image goodImage;
    //public Image greatImage;
    public static GeneratingTarget gm { get; set; }
    public ShootingEvent shootingEvent;
    /// 
    /// 记录当前射箭是否记录分数
    /// 
    private bool bAddCountScore { get; set; } = false;
    [HideInInspector] public bool getAddCountScore => bAddCountScore;
    //分数统计
    [HideInInspector] public UserGameAnalyse1 userGameAnalyse1;
    public int Index = 0;
    private bool unload = false;
    /// 
    /// 当前难度的配置
    /// 
    public Level LvCfg = null;
    /// 
    /// 当前序列的配置
    /// 
    public Order orderCfg = null;
    float nextOrderWaitTime;
    int curOrderIdx;
    bool canEnterNextOrder = true;
    bool isbegin = false;
    int tragetLeftNum;
    /// 
    /// 当前序列
    /// 
    public int CurOrder = 0;
    public static int MaxLevel = 3;
    public List ScoreLevel;
    public List EnScoreLevel;
    [SerializeField]
    RectTransform _canvasRectTransform;
    private void Awake()
    {
        gm = this;
        var scene = SceneManager.GetActiveScene();
        if(scene.name == "Hyperspace01")
            Index = 0;
        else if (scene.name == "Hyperspace02")
            Index = 1;
        else if(scene.name == "Hyperspace03")
            Index = 2;
        //初始化关卡配置
        LvCfg = Config.levels[Index];
        countDownTime = LvCfg.TotalTime;
        curOrderIdx = 0;
        UpdateCountDown(LvCfg.TotalTime);
        CurLvTxt.text = $"{Index + 1}";
        MaxLvTxt.text = $"/{MaxLevel}";
        shootingEvent = FindObjectOfType();
        scoreCom.gameObject.SetActive(false);
        scoreCom1.gameObject.SetActive(false);
        Miss.SetActive(false);
        stop = false;
        imgSlider.fillAmount = 1;
        ShowScore();
        bulletNull.SetActive(false);
        BtnRestart.onClick.AddListener(OnRestart);
        BtnNext.onClick.AddListener(OnBtnNext);
        BtnNext.gameObject.SetActive(Index + 1 < MaxLevel);
        BtnLast.onClick.AddListener(OnBtnLast);
        BtnLast.gameObject.SetActive(true);
        GameOver.SetActive(false);
		
		CreateUIManager();
        CreateTargetObject2D();
    }
    public void OnRestart()
    {
        Restart(Index);
    }
    public void OnBtnNext()
    {
        Restart(Index + 1);
    }
    public void OnBtnLast()
    {
        //Restart(Index - 1);
        onUploadScore();
        ////结束游戏页面
        //userGameAnalyse1.UploadData(true);
        //userGameAnalyse1.onResetOverlayData();
        //SceneManager.LoadScene("Home", LoadSceneMode.Single);
        //结束游戏页面
        userGameAnalyse1.showResultView(() =>
        {
            gm = null;
            SceneManager.LoadScene("Home", LoadSceneMode.Single);
        });
    }
    void Start()
    {
        if (ShootCheck.ins.bluetoothDeviceStatus == BluetoothDeviceStatus.MagazineLoading)
            OnLoading();
        else
            OnSeparation();
        //Screen.SetResolution(1280, 720, false); // 设置分辨率为1920x1080,并设置为全屏模式
        Invoke("GameStart", 4);
        Game1();
        Invoke("Game2", 1);
        Invoke("Game3", 2);
        var canvases = FindObjectsByType