local RateUI = class("RateUI", BaseUI)

local COLOR = BF.Color(1, 1, 1, 1)

local BTN_ICON = {
    "common_btn_green_2",
    "common_btn_grey_2"
}

function RateUI:isFullScreen()
    return false
end

function RateUI:showCommonBG()
    return false
end

function RateUI:getPrefabPath()
    return "assets/prefabs/ui/setting/rate_ui.prefab"
end

function RateUI:ctor()
    self.curStar = 0
end

function RateUI:onClose()
    BIReport:postRateClose(self.curStar)
    if self.starSeq then
        self.starSeq:Kill()
        self.starSeq = nil
    end
end

function RateUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
end

function RateUI:_display()
    local uiMap = self.root:genAllChildren()

    uiMap["rate_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.RATE_DESC_1))
    uiMap["rate_ui.later_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.RATE_DESC_2))

    if not self.stars then
        self.stars = {}
        for i = 1, 5 do
            self.stars[i] = {
                touchNode = uiMap["rate_ui.bg.bg_" .. i],
                star = uiMap["rate_ui.bg.bg_" .. i .. ".star"]
            }
            self.stars[i].star:setVisible(true)
        end
    end

    self:onClickStar(0)
end

function RateUI:_addListeners()
    local uiMap = self.root:genAllChildren()

    uiMap["rate_ui.bg.bg_1"]:addClickListener(function()
        self:onClickStar(1)
    end)

    uiMap["rate_ui.bg.bg_2"]:addClickListener(function()
        self:onClickStar(2)
    end)

    uiMap["rate_ui.bg.bg_3"]:addClickListener(function()
        self:onClickStar(3)
    end)

    uiMap["rate_ui.bg.bg_4"]:addClickListener(function()
        self:onClickStar(4)
    end)

    uiMap["rate_ui.bg.bg_5"]:addClickListener(function()
        self:onClickStar(5)
    end)

    uiMap["rate_ui.bg.btn"]:addClickListener(function()
        if self.curStar <= 0 then
            return
        end

        self:closeUI()

        if self.curStar > 3 then
            local url = "https://play.google.com/store/apps/details?id=com.combo.heroes.puzzle.rpg"
            if Platform:isIosPlatform() then
                url = "https://itunes.apple.com/app/6450101181"
            end
            GFunc.openUrl(url)
        end
    end)

    uiMap["rate_ui.later_tx"]:addClickListener(function()
        self:closeUI()
    end)
    BIReport:postRateOpen()
end

function RateUI:onClickStar(star)
    self.curStar = star
    for index, starInfo in ipairs(self.stars) do
        starInfo.star:setVisible(index <= star)
    end

    local uiMap = self.root:genAllChildren()
    local icon = BTN_ICON[1]

    local btnTx = uiMap["rate_ui.bg.btn.ok_tx"]
    local btnTxGray = uiMap["rate_ui.bg.btn.ok_tx_gray"]
    if self.curStar <= 0 then
        icon = BTN_ICON[2]
        btnTx:setText(GConst.EMPTY_STRING)
        btnTxGray:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
    else
        btnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
        btnTxGray:setText(GConst.EMPTY_STRING)
    end
    local btn = uiMap["rate_ui.bg.btn"]
    btn:setSprite(GConst.ATLAS_PATH.COMMON, icon)
    btn:setTouchEnable(self.curStar > 0)

    if self.starSeq then
        self.starSeq:Kill()
        self.starSeq = nil
    end

    self.stars[1].star:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE).color = COLOR
    if self.curStar > 0 then
        return
    else
        self.starSeq = self.root:createBindTweenSequence()
        self.stars[1].star:setVisible(true)
        self.starSeq:Append(self.stars[1].star:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):DOFade(0, 1))
        self.starSeq:Append(self.stars[1].star:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):DOFade(1, 1))
        self.starSeq:SetLoops(-1)
    end
end

return RateUI