local ZUIPanelUpdate = ZUIPanelUpdate or {}

ZUIPanelUpdate.textUpdateStateInfo = nil
ZUIPanelUpdate.imageProgress = nil
ZUIPanelUpdate.textTipsTitle = nil
ZUIPanelUpdate.textTipsInfo = nil

local function OnClickButtonYes()
    print("--> Click Yes ~")
    ZUIPanelUpdate.panelTips:SetActive(false)
    local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
    eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_RETRY)
end

local function OnClickButtonNo()
    print("--> Click No  ~")
    ZUIPanelUpdate.panelTips:SetActive(false)
    CS.UnityEngine.Application.Quit()
end

function ZUIPanelUpdate.SetProgressPercent(percent)
    if percent <= 0 then percent = 0 end
    if percent >= 1 then percent = 1 end

    ZUIPanelUpdate.imageProgress.transform:GetComponent("Image").fillAmount = percent;
end

function ZUIPanelUpdate.SetUpdateInfo(information)
    ZUIPanelUpdate.panelUpdateInfo:SetActive(false)
    ZUIPanelUpdate.textUpdateStateInfo:SetActive(true)
    ZUIPanelUpdate.textUpdateStateInfo:GetComponent("Text").text = information
end

function ZUIPanelUpdate.SetTips(title, information)
    ZUIPanelUpdate.panelUpdateInfo:SetActive(false)

    ZUIPanelUpdate.panelTips:SetActive(true)
    ZUIPanelUpdate.textTipsTitle:GetComponent("Text").text = title
    ZUIPanelUpdate.textTipsInfo:GetComponent("Text").text = information
end

function ZUIPanelUpdate.SetUpdateDetail(szPercent, szUpdateDetail, szUpdateSpeed)
    ZUIPanelUpdate.panelTips:SetActive(false)
    ZUIPanelUpdate.textUpdateStateInfo:SetActive(false)
    ZUIPanelUpdate.panelUpdateInfo:SetActive(true)
    ZUIPanelUpdate.textUpdatePercent:GetComponent("Text").text = szPercent
    ZUIPanelUpdate.textUpdateDetail:GetComponent("Text").text = szUpdateDetail
    ZUIPanelUpdate.textUpdateSpeed:GetComponent("Text").text = szUpdateSpeed
end

function ZUIPanelUpdate.Awake()
    print("ZUIPanelUpdate Awake")
    --local prefabCanvasUpdate = CS.SFramework.SResourceManagerR.LoadResource("Prefabs/Canvas_Update.prefab")
    local prefabCanvasUpdate = LoadResource("Prefabs/Canvas_Update.prefab")
    local canvasUpdate = CS.UnityEngine.GameObject.Instantiate(prefabCanvasUpdate)
    ZUIPanelUpdate.canvasUpdate = canvasUpdate

    local panelUpdate = canvasUpdate.transform:Find("Panel_Update").gameObject
    local textInfo  = panelUpdate.transform:Find("Text_Info").gameObject
    local imageProgress = panelUpdate.transform:Find("Image_Progress").gameObject

    local panelUpdateInfo = panelUpdate.transform:Find("Panel_UpdateInfo").gameObject
    local textUpdatePercent = panelUpdateInfo.transform:Find("Text_UpdatePercent").gameObject
    local textUpdateDetail = panelUpdateInfo.transform:Find("Text_UpdateDetail").gameObject
    local textUpdateSpeed = panelUpdateInfo.transform:Find("Text_UpdateSpeed").gameObject

    local panelTips = canvasUpdate.transform:Find("Panel_Tips").gameObject
    local textTitle = panelTips.transform:Find("Text_Title").gameObject
    local tipsTextInfo = panelTips.transform:Find("Text_Info").gameObject

    panelTips:SetActive(false)
    panelUpdateInfo:SetActive(false)

    ZUIPanelUpdate.panelTips = panelTips
    ZUIPanelUpdate.textUpdateStateInfo = textInfo
    ZUIPanelUpdate.imageProgress = imageProgress
    ZUIPanelUpdate.panelUpdateInfo = panelUpdateInfo
    ZUIPanelUpdate.textUpdatePercent = textUpdatePercent
    ZUIPanelUpdate.textUpdateDetail = textUpdateDetail
    ZUIPanelUpdate.textUpdateSpeed = textUpdateSpeed
    ZUIPanelUpdate.textTipsTitle = textTitle
    ZUIPanelUpdate.textTipsInfo = tipsTextInfo
end

function ZUIPanelUpdate.Start()
    print("ZUIPanelUpdate Start")

    local canvasUpdate = ZUIPanelUpdate.canvasUpdate
    local buttonYes = canvasUpdate.transform:Find("Panel_Tips/Button_Yes").gameObject
    buttonYes:GetComponent("Button").onClick:AddListener(OnClickButtonYes)
    local buttonNo = canvasUpdate.transform:Find("Panel_Tips/Button_No").gameObject
    buttonNo:GetComponent("Button").onClick:AddListener(OnClickButtonNo)

    ZUIPanelUpdate.RefreshInfo()

    local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, ZUIPanelUpdate.SetUpdateInfo)
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, ZUIPanelUpdate.SetProgressPercent)
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_SHOW_TIPS, ZUIPanelUpdate.SetTips)
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_SHOW_UPDATE_DETAIL, ZUIPanelUpdate.SetUpdateDetail)
end

function ZUIPanelUpdate.OnDestroy()
    print("ZUIPanelUpdate.OnDestroy")
    local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
    eventDispatchCenter:UnregisterEvent(eventDispatchCenter.EventType.UPDATE_SHOW_UPDATE_DETAIL, ZUIPanelUpdate.SetUpdateDetail)
    eventDispatchCenter:UnregisterEvent(eventDispatchCenter.EventType.UPDATE_SHOW_TIPS, ZUIPanelUpdate.SetTips)
    eventDispatchCenter:UnregisterEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, ZUIPanelUpdate.SetProgressPercent)
    eventDispatchCenter:UnregisterEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, ZUIPanelUpdate.SetUpdateInfo)
end

function ZUIPanelUpdate.RefreshInfo()
    ZUIPanelUpdate.textTipsTitle:GetComponent("Text").text = "更新提示"
    ZUIPanelUpdate.textTipsInfo:GetComponent("Text").text = "准备更新..."
    ZUIPanelUpdate.textUpdateStateInfo:GetComponent("Text").text = "等待更新..."
end

return ZUIPanelUpdate