using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace AdamSync
{
    public class ContectServer : MonoBehaviour
    {
        public bool isLinkAgain = false;
        public string serverIP;
        public int port;
        // Start is called before the first frame update
        private async void Awake()
        {
            serverIP = ReadFromLocal("confing.txt");
            Debug.Log("Net");
            DontDestroyOnLoad(gameObject);
            SceneManager.LoadScene("SampleScene");
            //try
            //{
            isLinkAgain = true;
            await SyncCreateRoom.StartLinkTCPServer(serverIP, port);
            //}
            //catch (System.Exception e)
            //{
            //    if (isLinkAgain)
            //    {
            //        Awake();
            //    }
            //    Debug.Log(e.ToString());
            //}
        }

        public static string ReadFromLocal(string fileName)
        {
            string path = GetPath(fileName);
            if (!File.Exists(path)) return "无";
            string data = File.ReadAllText(path);
            return data;
        }

        public static string[] ReadAllFromLocal(string fileName)
        {
            string path = GetPath(fileName);
            if (!File.Exists(path)) return null;
            string[] data = File.ReadAllLines(path);
            return data;
        }

        public static string GetPath(string fileName)
        {
            string path = Application.streamingAssetsPath + "/" + fileName;
            return path;
        }

        private void OnDisable()
        {
            SyncCreateRoom.CloseClint();
            isLinkAgain = false;
        }
    }
}