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

public class UpdatePanel : BasePanel
{
    private HotUpdater mHotUpdater;
    private Action hideCb;

    private Text mVersionText;
    private Slider mProgressSlider;
    private Text mProgressText;
    private Text mTipsText;
    private GameObject mAppUpdateDlg;
    private Button mNextBtn;
    private Button mFullAppUpdateBtn;
    private GameObject mErrorTipsDlg;
    private Text mErrorText;
    private Button mRetryBtn;

    public static void Create(Action hideCb)
    {
        var panel = PanelMgr.instance.ShowPanel<UpdatePanel>("UpdatePanel", "BaseRes/UpdatePanel.prefab");
        if (null != hideCb)
            panel.hideCb = hideCb;
    }

    void Awake()
    {
        var uiBinder = gameObject.GetComponent<PrefabObjBinder>();
        mVersionText = uiBinder.GetObj<Text>("versionText");
        mProgressSlider = uiBinder.GetObj<Slider>("progressSlider");
        mProgressText = uiBinder.GetObj<Text>("progressText");
        mTipsText = uiBinder.GetObj<Text>("tipsText");
        mAppUpdateDlg = uiBinder.GetObj<GameObject>("appUpdateDlg");
        mNextBtn = uiBinder.GetObj<Button>("nextBtn");
        mFullAppUpdateBtn = uiBinder.GetObj<Button>("updateBtn");
        mErrorTipsDlg = uiBinder.GetObj<GameObject>("errorTipsDlg");
        mErrorText = uiBinder.GetObj<Text>("errorText");
        mRetryBtn = uiBinder.GetObj<Button>("retryBtn");

        mNextBtn.onClick.AddListener(OnNextBtnClick);
        mFullAppUpdateBtn.onClick.AddListener(OnFullAppUpdateBtnClick);
        mRetryBtn.onClick.AddListener(OnRetryBtnClick);
    }

    void Start()
    {
        // 初始化
        mVersionText.text = string.Format("app: {0} res: {0}", VersionMgr.instance.appVersion, VersionMgr.instance.resVersion);
        mProgressSlider.value = 0;
        mProgressText.text = "0%";
        mTipsText.text = "正在请求更新，请稍等...";

        // 请求热更新
        mHotUpdater = new HotUpdater();
        mHotUpdater.actionForceFullAppUpdate = ShowForceFullAppUpdateDlg;
        mHotUpdater.actionWeakFullAppUpdate = ShowWeakFullAppUpdateDlg;
        mHotUpdater.actionShowErrorTips = ShowErrorTips;
        mHotUpdater.actionUpdateTipsText = UpdateTipsText;
        mHotUpdater.actionNothingToUpdate = NothingToUpdate;
        mHotUpdater.actionUpdateProgress = UpdateProgress;
        mHotUpdater.actionAllDownloadDone = AllDownloadDone;
        mHotUpdater.Init();
        mHotUpdater.Start();
    }

    private void OnNextBtnClick()
    {
        mAppUpdateDlg.SetActive(false);
        mHotUpdater.DoNextTime();
    }

    private void OnFullAppUpdateBtnClick()
    {
        mHotUpdater.DoFullAppUpdate();
    }

    private void OnRetryBtnClick()
    {
        mErrorTipsDlg.SetActive(false);
        mHotUpdater.Start();
    }

    private void ShowForceFullAppUpdateDlg()
    {
        mAppUpdateDlg.SetActive(true);
        mNextBtn.gameObject.SetActive(false);
    }

    private void ShowWeakFullAppUpdateDlg()
    {
        mAppUpdateDlg.SetActive(true);
        mNextBtn.gameObject.SetActive(true);
    }

    private void ShowErrorTips(UnityWebRequest.Result error)
    {
        mErrorTipsDlg.SetActive(true);
        switch (error)
        {
            case UnityWebRequest.Result.ConnectionError:
                {
                    mErrorText.text = "连击更新服务器失败，请重试！";
                }
                break;
            case UnityWebRequest.Result.DataProcessingError:
                {
                    mErrorText.text = "下载异常，请检查网络并重试！";
                }
                break;
            default:
                {
                    mErrorText.text = "请求更新服务器失败，请重试！";
                }
                break;
        }
    }

    private void UpdateTipsText(string tipsStr)
    {
        mTipsText.text = tipsStr;
    }

    private void NothingToUpdate()
    {
        Finish();
    }

    private void AllDownloadDone()
    {
        Finish();
    }

    private void Update()
    {
        mHotUpdater.Update();
    }

    /// <summary>
    /// 更新进度条
    /// </summary>
    /// <param name="value">值</param>
    private void UpdateProgress(float value)
    {
        mProgressSlider.value = value < 0.05f ? 0.05f : value;
        mProgressText.text = (100 * value).ToString("0.00") + "%";
    }

    private void Finish()
    {
        Destroy(gameObject);
        hideCb?.Invoke();
    }

    private void OnApplicationQuit()
    {
        mHotUpdater.Dispose();
    }
}
