using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using GCGame.Table;
using Module.Log;

public class ImperialWaitTimePanel : MonoBehaviour {

    public Text descText;
    public Text timeText;


    public GameObject _huishiDescObj;
    public GameObject _dianshiDescObj;

    public static ImperialWaitTimePanel Instance;
    private void Awake()
    {
        Instance = this;
    }

    private void OnEnable()
    {
        remainTime = 0;
    }

    private void OnDisable()
    {
        Instance = null;
    }

    public int remainTime;
	public void SetTime()
    {
        if (_curType == ImpreialExamPanelCtr.Question_Type.QUESTION_HUISHI)
            CalculateOpenHuishiRemainTime();
        else
            CalculateDianshiRemainTime();
        GetAndSetRemainTime();
    }

    public void CalculateOpenHuishiRemainTime()
    {
        var otherTab = TableManager.GetQuestionOtherByID(1, 0);  //获取会试需要进行的时间
        if (otherTab == null)
        {
            LogModule.ErrorLog("OtherTab is null, id : " + 1);
            return;
        }

        DateTime curTime = GCGame.Utils.GetServerDateTime();
        DateTime openTime = GetOpenTime().AddSeconds(otherTab.WaitTime);  //当天19.01
        remainTime = (int)(openTime - curTime).TotalSeconds > 0 ? (int)(openTime - curTime).TotalSeconds : 0;
    }

    public void CalculateDianshiRemainTime()
    {
        if (remainTime != 0)
            return;

        var otherTab = TableManager.GetQuestionOtherByID(1, 0);  //获取会试需要进行的时间
        if (otherTab == null)
        {
            LogModule.ErrorLog("OtherTab is null, id : " + 1);
            return;
        }

        var dianshiTab = TableManager.GetQuestionOtherByID(2, 0);  //获取会试需要进行的时间
        if (dianshiTab == null)
        {
            LogModule.ErrorLog("dianshiTab is null, id : " + 1);
            return;
        }

        //7.01开始
        var _overSeconds = otherTab.DayQuestionCount * otherTab.AutoFlush + otherTab.WaitTime;

        DateTime curTime = GCGame.Utils.GetServerDateTime();  //24的时候才开始竞猜
        DateTime openTime = GetOpenTime().AddSeconds(_overSeconds + dianshiTab.WaitTime);
        remainTime = (int)(openTime - curTime).TotalSeconds > 0 ? (int)(openTime - curTime).TotalSeconds : 0;
    }

    public void GetAndSetRemainTime()
    {
        if(remainTime > 0)
        {
            timeText.text = remainTime.ToString();
        }else
        {
            timeText.text = "0";
        }
    }


    float countTime = 0.0f;
    private void Update()
    {
        if (remainTime > 0)
        {
            countTime += Time.deltaTime;
            if(countTime >= 1.0f)
            {
                remainTime--;
                GetAndSetRemainTime();
                countTime = 1.0f - countTime;
            }
        }
    }

    private void GetCurQuestionType()
    {
        //根据当前时间判断
        var huishiOtherTab = TableManager.GetQuestionOtherByID(1, 0);  //获取会试需要进行的时间
        
        DateTime huishiOpenTime = GetOpenTime().AddSeconds(huishiOtherTab.WaitTime);  //当天19.01

        if (huishiOtherTab == null)
        {
            LogModule.ErrorLog("OtherTab is null, id : " + 1);
            return;
        }

        var _huishiTotalTime = huishiOtherTab.DayQuestionCount * huishiOtherTab.AutoFlush;

        DateTime huishiOverTime = huishiOpenTime.AddSeconds(_huishiTotalTime);

        var dianshiOtherTab = TableManager.GetQuestionOtherByID(2, 0);  //获取殿试等待时间
        if (dianshiOtherTab == null)
        {
            LogModule.ErrorLog("OtherTab is null, id : " + 1);
            return;
        }
        
        DateTime dianshiOpenTime = huishiOverTime.AddSeconds(dianshiOtherTab.WaitTime);
        DateTime curTime = GCGame.Utils.GetServerDateTime();
        //会试结束之后的竞猜时间是殿试阶段
        _curType = curTime <= huishiOverTime ? ImpreialExamPanelCtr.Question_Type.QUESTION_HUISHI : ImpreialExamPanelCtr.Question_Type.QUESTION_DIANSHI;

        if (ImpreialExamPanelCtr.Instance)
        {
            ImpreialExamPanelCtr.Instance.SetMyQuestionType(_curType);
        }
    }

    //会试1 殿试2
    private ImpreialExamPanelCtr.Question_Type _curType;
    public void SetWaitType()
    {
        GetCurQuestionType();

        _huishiDescObj.SetActive(_curType == ImpreialExamPanelCtr.Question_Type.QUESTION_HUISHI);
        _dianshiDescObj.SetActive(_curType == ImpreialExamPanelCtr.Question_Type.QUESTION_DIANSHI);
        timeText.gameObject.SetActive(true);

        switch (_curType)
        {
            case ImpreialExamPanelCtr.Question_Type.QUESTION_HUISHI:
                descText.text = StrDictionary.GetClientDictionaryString("#{2565}");
                break;
            case ImpreialExamPanelCtr.Question_Type.QUESTION_DIANSHI:
                descText.text = StrDictionary.GetClientDictionaryString("#{2566}");
                break;
        }

        //计算并且设置剩余时间
        SetTime();
    }

    public DateTime GetOpenTime()
    {
        DateTime curTime = GCGame.Utils.GetServerDateTime();

        var sysParamTab = TableManager.GetSystemParamByID(71, 0);
        if(sysParamTab == null)
        {
            Debug.LogError("SystemPatam tab is null, id : " + 71);
            return new DateTime(curTime.Year, curTime.Month, curTime.Day, 19, 15, 0);
        }
        return new DateTime(curTime.Year, curTime.Month, curTime.Day, 
            int.Parse(sysParamTab.StringValue.Split('|')[0]), int.Parse(sysParamTab.StringValue.Split('|')[1]), int.Parse(sysParamTab.StringValue.Split('|')[2]));
    }
}