local SUIPanelUpdate = SUIPanelUpdate or {}

SUIPanelUpdate.textUpdateStateInfo = nil
SUIPanelUpdate.imageProgress = nil
SUIPanelUpdate.textTipsTitle = nil
SUIPanelUpdate.textTipsInfo = nil

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

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

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

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

function SUIPanelUpdate.SetUpdateInfo(information)
    SUIPanelUpdate.textUpdateStateInfo:GetComponent("Text").text = information
end

function SUIPanelUpdate.SetTips(title, information)
    SUIPanelUpdate.panelTips:SetActive(true)
    SUIPanelUpdate.textTipsTitle:GetComponent("Text").text = title
    SUIPanelUpdate.textTipsInfo:GetComponent("Text").text = information
end

function SUIPanelUpdate.Awake()
    print("SUIPanelUpdate 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)
    SUIPanelUpdate.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 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)

    SUIPanelUpdate.panelTips = panelTips
    SUIPanelUpdate.textUpdateStateInfo = textInfo
    SUIPanelUpdate.imageProgress = imageProgress
    SUIPanelUpdate.textTipsTitle = textTitle
    SUIPanelUpdate.textTipsInfo = tipsTextInfo
end

function SUIPanelUpdate.Start()
    print("SUIPanelUpdate Start")

    local canvasUpdate = SUIPanelUpdate.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)

    SUIPanelUpdate.RefreshInfo()

    local eventDispatchCenter = require("Base/SEventDispatchCenter.lua")
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, SUIPanelUpdate.SetUpdateInfo)
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, SUIPanelUpdate.SetProgressPercent)
    eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_SHOW_TIPS, SUIPanelUpdate.SetTips)
end

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

function SUIPanelUpdate.RefreshInfo()
    SUIPanelUpdate.textTipsTitle:GetComponent("Text").text = "Update Tips"
    SUIPanelUpdate.textTipsInfo:GetComponent("Text").text = "Prepare For Update."
    SUIPanelUpdate.textUpdateStateInfo:GetComponent("Text").text = "Wait For Update."
end

return SUIPanelUpdate