using AX.MessageSystem;
using AX.NetworkSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;



public class SetLiquipdata
{
    /// <summary>
    /// 全液面火Id
    /// </summary>
    public long LiquipId;
    /// <summary>
    /// 时间
    /// </summary>
    public string time;
}
public class FireLiquipPanel : BaseAttributeSet
{

    private InputFieldRecordByAC Minput;
    private InputFieldRecordByAC Sinput;
    private ButtonRecordByAC Sure;
    private ButtonRecordByAC Cancel;
    private Text Error;
    private int Mtime;
    private int Stime;
    private int SetTime;


    private static FireLiquipPanel instance;

    public static FireLiquipPanel Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject obj = Resources.Load<GameObject>("Common/FireLiquipPanel");
                GameObject clone = Instantiate(obj, GameObject.Find("Canvas").transform);
                clone.transform.SetAsLastSibling();
                clone.name = "FireLiquipPanel";
                clone.transform.position = Vector3.zero;
                instance = clone.GetComponent<FireLiquipPanel>();
                instance.Init();
            }
            return instance;
        }
    }

    void Init()
    {
        Minput = transform.Find("MInputField").GetComponent<InputFieldRecordByAC>();
        Sinput = transform.Find("SInputField").GetComponent<InputFieldRecordByAC>();
        Sure = transform.Find("SureButton").GetComponent<ButtonRecordByAC>();
        Cancel = transform.Find("CancelButton").GetComponent<ButtonRecordByAC>();
        Minput.OutInterFaceInPutField = MinputChange;
        Sinput.OutInterFaceInPutField = SinputChange;
        Sure.OutInterFaceButton = SureClick;
        Cancel.OutInterFaceButton = CancelClick;
    }

    public override void OnDisable()
    {
        base.OnDisable();
        Close();
    }

    public override void OpenPanel()
    {
        base.OpenPanel();
        LoadUIDisaster();
        gameObject.SetActive(true);
    }

    private void LoadUIDisaster()
    {
        if (GameSettings.othersSettings.isSelectedDisaster)
        {
            if (GameSettings.othersSettings.mode != Mode.DisasterManagement)
            {//若是练习或演习模式进入导调组界面,则不可以修改
                //Minput.GetComponent<InputField>().interactable = false;
                //Sinput.GetComponent<InputField>().interactable = false;
                //Sure.GetComponent<Button>().interactable = false;



                //Cancel.GetComponent<Button>().interactable = false;
            }
            else
            {
                if (!GameSettings.disasterSetting.isEdit)
                {//若是灾情库设置模式的查看,则不可以修改
                    Minput.GetComponent<InputField>().interactable = false;
                    Sinput.GetComponent<InputField>().interactable = false;
                    Sure.GetComponent<Button>().interactable = false;
                    //Cancel.GetComponent<Button>().interactable = false;
                }
                else
                {
                    //若是灾情库设置模式的编辑,则可以修改
                }
            }
        }
    }

    private void CancelClick()
    {
        MessageDispatcher.SendMessage("CANCEL_OBJ_SELECTED");
        Close();
    }

    private void SureClick()
    {
        if (chooseObj&&chooseObj.GetComponent<FireLiquidLevelCtrl>()) 
        {
            SetTime = Mtime * 60 + Stime;
            string settimestring = SecondToHMS(SetTime);
            chooseObj.GetComponent<FireLiquidLevelCtrl>().showTime = settimestring;

            //发送设置时间
            MessageDispatcher.SendMessage("SetTime", new SetLiquipdata()
            {
                LiquipId = chooseObj.GetComponent<BaseGameObjInfo>().gameObjID,
                time = settimestring,
            });
            MessageDispatcher.SendMessage("CANCEL_OBJ_SELECTED");

            if (GameSettings.othersSettings.mode != Mode.DisasterManagement)
            {
                NetworkManager.Default.SendAsync("SET_LIQUIP_HIDE_SYNC", chooseObj.GetComponent<BaseGameObjInfo>().gameObjID);
            }

            LoadPromptWin.Instance.LoadTextPromptWindow("设置成功", 1f);
        }
       
    }

    public void Close()
    {
        gameObject.SetActive(false);
    }

    private void SinputChange(string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            Sinput.GetComponent<InputField>().text = "0";
        }
        else
        {
            Sinput.GetComponent<InputField>().text=Mathf.Clamp(int.Parse(value),0,59).ToString();
        }
        Stime = int.Parse(Sinput.GetComponent<InputField>().text);
    }

    private void MinputChange(string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            Minput.GetComponent<InputField>().text = "0";
        }
        else
        {
            Minput.GetComponent<InputField>().text = Mathf.Clamp(int.Parse(value), 0, 999).ToString();
        }
        Mtime = int.Parse(Minput.GetComponent<InputField>().text);
    }

    public override void LoadObjData()
    {
        var fireSpreadCtrl = chooseObj.GetComponent<FireLiquidLevelCtrl>();

        string[] str = fireSpreadCtrl.showTime.Split(':');

        int min = int.Parse(str[0])  * 60 + int.Parse(str[1]);
        int second = int.Parse(str[2]);

        Minput.GetComponent<InputField>().text = min.ToString();
        Sinput.GetComponent<InputField>().text = second.ToString();
    }
   
    public string SecondToHMS(int second)
    {
        int h = (int)(second / 3600);
        int m = (int)(second - h * 3600) / 60;
        int s = (int)(second - h * 3600 - m * 60);
        return string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
    }
}