using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net;

public class IPsettingPanel : MonoBehaviour
{
    public static IPsettingPanel instance;

    public InputField syncidInput;
    public InputField interfanceInput;
    public Button quedingBtn;
    public Button quxiaoBtn;

    private void Awake()
    {
        instance = this;
        quedingBtn.onClick.AddListener(()=> 
        {
            if (Check())
            {
                PlayerPrefs.SetString("协同交互IP", syncidInput.text);
                PlayerPrefs.SetString("接口服务IP", interfanceInput.text);
                MyNetMQClient.CallIP = interfanceInput.text;
                gameObject.SetActive(false);
            }
            else
            {
                Debug.Log("请检查IP格式");
                MessagePanel.ShowMessage("请检查IP格式", LoginPanel.instance.canvas.transform);
            }
        });

        quxiaoBtn.onClick.AddListener(()=> 
        {
            gameObject.SetActive(false);
        });

        gameObject.SetActive(false);
    }

    public void Show()
    {
        syncidInput.text= PlayerPrefs.GetString("协同交互IP", "");
        interfanceInput.text= PlayerPrefs.GetString("接口服务IP", "");
        MyNetMQClient.CallIP = interfanceInput.text;
        gameObject.SetActive(true);
    }

    private bool Check()
    {
        if(string.IsNullOrEmpty(syncidInput.text) || string.IsNullOrEmpty(interfanceInput.text))
        {
            return false;
        }

        if(!syncidInput.text.Contains(":") || !interfanceInput.text.Contains(":"))
        {
            return false;
        }

        string[] tmp1=syncidInput.text.Split(':');
        string[] tmp2 = interfanceInput.text.Split(':');

        if(tmp1.Length!=2 || tmp2.Length!=2)
        {
            return false;
        }

        IPAddress iPAddress1;
        IPAddress iPAddress2;
        if (!IPAddress.TryParse(tmp1[0], out iPAddress1) || !IPAddress.TryParse(tmp2[0], out iPAddress2))
        {
            return false;
        }

        int port1;
        int port2;
        if(!int.TryParse(tmp1[1],out port1)|| !int.TryParse(tmp2[1],out port2))
        {
            return false;
        }

        if(port2<=0 || port2<=0)
        {
            return false;
        }

        //成功
        return true;
    }

    
}