using Newtonsoft.Json;
using NPOI.HPSF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
/// 
/// 重连
/// 
public class Reconnection : MonoBehaviour
{
    public DisconnectedData DisconnectedData;
    string fileName;
    public Transform Player;
    public Vector3 PlayerPos;
    public Vector3 PlayerRot;
    [SerializeField] List tempGame;
    char[] TempChar = new char[7] { '(', 'C', 'l', 'o', 'n', 'e', ')' };
    public bool ison = false;
    DuanDian isonDian;
    [SerializeField] GameObject im;
    public GameObject HandGameObject;
    public bool isDuanDianok = false;
    public Text te;
    // Start is called before the first frame update
    void Awake()
    {
        try
        {
            //Player = GameManager.Instance.Player.transform;
            //DisconnectedData = new DisconnectedData();
            //DisconnectedData.PlayerPosition = new pos();
            //DisconnectedData.PlayerRotions = new pos();
            //DisconnectedData.toolsName = new List();
            //DateTime currentTime = DateTime.Now;
            //Debug.Log("当前系统时间:" + currentTime);
            ////fileName = Application.streamingAssetsPath + "/" + currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() + ".json";
            //fileName = Application.streamingAssetsPath + "/" +"2023922" + ".json";
            //Debug.Log(fileName);
            //StartCoroutine(LoadJSON(fileName));
            
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.Message);
            throw;
        }
    }
    /// 
    /// 读取是否有数据
    /// 
    /// 
    /// 
    /// 
    public async Task GetSceneLoad(string name, string id)
    {
        ExaminationLoad examinationLoad = new ExaminationLoad();
        isonDian = await examinationLoad.GetLoad(name, id);
        return isonDian.state;
    }
    async Task load1234(string name, string tips, string obj)
    {
        ExaminationLoad examinationLoad = new ExaminationLoad();
        StartCoroutine(examinationLoad.LoadAsyncPost(name, tips, obj));
        //await examinationLoad.LoadAsyncPost(name,tips,obj);
        //Debug.Log(tempbool.data);
    }
    public void init(string data)
    {
        //ExaminationLoad examinationLoad = new ExaminationLoad();
        //await examinationLoad.GetLoad(UIManager.Instance.loginController.userName, UIManager.Instance.loginController.PassWord);
        LoadJSON(data);
        string name = "";
        
        PlayerPos = new Vector3(DisconnectedData.PlayerPosition.x, DisconnectedData.PlayerPosition.y, DisconnectedData.PlayerPosition.z);
        PlayerRot = new Vector3(DisconnectedData.PlayerRotions.x, DisconnectedData.PlayerRotions.y, DisconnectedData.PlayerRotions.z);
        //DisconnectedData.userBianHao = 
        if (!string.IsNullOrEmpty(DisconnectedData.HandGameObject))
        {
            name = Filter(DisconnectedData.HandGameObject, TempChar);
        }
        if (!string.IsNullOrEmpty(DisconnectedData.HandGameObject))
        {
            for (int i = 0; i < tempGame.Count; i++)
            {
                if (tempGame[i].name.Equals(name))
                {
                    HandGameObject = tempGame[i];
                    //GameManager.Instance.ToolsShow(tempGame[i]);
                }
            }
        }
        for (int i = 0; i < DisconnectedData.toolsName.Count; i++)
        {
            for (int j = 0; j < tempGame.Count; j++)
            {
                if (DisconnectedData.toolsName[i].Equals(tempGame[j].name))
                {
                    Vector3 temppos = new Vector3(DisconnectedData.toolsPos[i].x, DisconnectedData.toolsPos[i].y, DisconnectedData.toolsPos[i].z);
                    Vector3 temprot = new Vector3(DisconnectedData.toolsRot[i].x, DisconnectedData.toolsRot[i].y, DisconnectedData.toolsRot[i].z);
                    ToolsItemManager.Instance.CreatToolItem(tempGame[j], temppos, temprot);
                    //Debug.Log(tempGame[j].name + "  ======");
                }
            }
        }
        //SceneLoad.Instance.SceneChange(DisconnectedData.SceneName);
        UIManager.Instance.bottomCotroller.buttomPanel.SetActive(true);
        UIManager.Instance.time.countTime = DisconnectedData.Timer;
        UIManager.Instance.loginController.gameObject.SetActive(false);
        UIManager.Instance.jobCardController.show(DisconnectedData.timuAnd);
        im.gameObject.SetActive(false);
        //UIManager.Instance.goToScene.OnGo(DisconnectedData.userBianHao);
        UIManager.Instance.goToScene.userID.text = DisconnectedData.userBianHao;
        UIManager.Instance.exitBtn.gameObject.SetActive(true);
        te.text = "考生姓名:"+DisconnectedData.UserName;
        //isDuanDianok = true;
    }
    void LoadJSON(string json)
    {
        //yield return new WaitForSeconds(1f);
        //string sPath = fileName;
        //using (UnityWebRequest oReq = UnityWebRequest.Get(sPath))
        //{
        //    yield return oReq.SendWebRequest();
        //    if (!string.IsNullOrEmpty(oReq.error))
        //    {
        //        Debug.Log("获取配置信息失败!");
        //        yield break;
        //    }
        //    string json = oReq.downloadHandler.text;
        DisconnectedData = new DisconnectedData();
        DisconnectedData.PlayerPosition = new pos();
        DisconnectedData.PlayerRotions = new pos();
        DisconnectedData.toolsName = new List();
        DisconnectedData.keyValuePairs = new Dictionary();
        DisconnectedData.timuAnd = new timuAndOptoin();
        DisconnectedData.timuAnd.userID = new List();
        DisconnectedData.timuAnd.userName = new List();
        DisconnectedData.timuAnd.workSpace = new List();
        DisconnectedData.timuAnd.aqcss = new List();
        DisconnectedData.timuAnd.fxdfx = new List();
        DisconnectedData.timuAnd.bools = new List();
        DisconnectedData.toolsPos = new List();
        DisconnectedData.toolsRot = new List();
        DisconnectedData = JsonConvert.DeserializeObject(json);
        //load1234("12345", "12345", json);
        //}
        //init();
    }
    public string Filter(string str, char[] charsToRemove)
    {
        return String.Concat(str.Split(charsToRemove.ToArray()));
    }
}