local PaySelectUI = class("PaySelectUI", BaseUI)

function PaySelectUI:isFullScreen()
    return false
end

function PaySelectUI:getPrefabPath()
    return "assets/prefabs/ui/shop/pay_select_ui.prefab"
end

function PaySelectUI:ctor(params)
    self.rechrageId = params.rechargeId
    self.config = ConfigManager:getConfig("recharge")[self.rechrageId]
    self.vouchers = self.config and self.config.vouchers
    self.callback = params.callback
end

function PaySelectUI:onLoadRootComplete()
    if not self.config then
        self:closeUI()
        return
    end
    self.selectTag = DataManager.PaymentData:getDefaultPayWay() ~= nil
    self:_display()
end

function PaySelectUI:_display()
    local uiMap = self.root:genAllChildren()
    local selectObj = uiMap["pay_select_ui.content.content.today.check.select"]
    selectObj:setVisible(self.selectTag)

    local txContent = uiMap["pay_select_ui.content.content.tx_content"]
    txContent:setText(I18N:getGlobalText(I18N.GlobalConst.DAI_JIN_JUAN_DESC_2, self.vouchers.num))

    local iconToday = uiMap["pay_select_ui.content.content.today.check"]
    local txToday = uiMap["pay_select_ui.content.content.today.text"]

    uiMap["pay_select_ui.content.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.MESSAGE_BOX_TITLE))
    txToday:setText(I18N:getGlobalText(I18N.GlobalConst.CONFIRM_IGNORE))
    uiMap["pay_select_ui.content.content.btn_ok.tx_ok"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAI_JIN_JUAN_DESC_4))
    uiMap["pay_select_ui.content.content.btn_cancel.tx_cancel"]:setText(GFunc.getFormatPrice(self.rechrageId))
    uiMap["pay_select_ui.content.content.btn_cancel.tx_cancel_tips"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAI_JIN_JUAN_DESC_3))
    GFunc.centerImgAndTx(iconToday, txToday, 3)

    local num = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_ACT_DAIJINJUAN)
    local str = num .. "/" .. self.vouchers.num
    local canUseDaiJinJuan = true
    if num < self.vouchers.num then
        str = "<color=#ff3939>" .. str .. "</color>"
        canUseDaiJinJuan = false
    end
    local txNum = uiMap["pay_select_ui.content.content.btn_ok.cost.tx_cost"]
    txNum:setText(str)
    local imgIcon = uiMap["pay_select_ui.content.content.btn_ok.cost.img_cost"]
    imgIcon:setSprite(GConst.ATLAS_PATH.ICON_ITEM, ModuleManager.ItemManager:getItemIcon(GConst.ItemConst.ITEM_ID_ACT_DAIJINJUAN))

    uiMap["pay_select_ui.content.btn_close"]:addClickListener(function()
        self:closeUI()
    end)

    iconToday:addClickListener(function()
        if DataManager.PaymentData:getDefaultPayWay() ~= nil then
            DataManager.PaymentData:setDefaultPayWay(nil)
        else
            DataManager.PaymentData:setDefaultPayWay(DataManager.PaymentData.PAY_WAY.DIRECTE)
        end
        self.selectTag = DataManager.PaymentData:getDefaultPayWay() ~= nil
        selectObj:setVisible(self.selectTag)
    end)

    uiMap["pay_select_ui.content.content.btn_cancel"]:addClickListener(function()
        self:closeUI()
        if self.selectTag then
            DataManager.PaymentData:setDefaultPayWay(DataManager.PaymentData.PAY_WAY.DIRECTE)
        end
        local callback = self.callback
        self.callback = nil
        if callback then
            callback(false)
        end
    end)

    local daiJinJuanBtn = uiMap["pay_select_ui.content.content.btn_ok"]
    if canUseDaiJinJuan then
        daiJinJuanBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_3")
        daiJinJuanBtn:addClickListener(function()
            self:closeUI()
            if self.selectTag then
                DataManager.PaymentData:setDefaultPayWay(DataManager.PaymentData.PAY_WAY.DAI_JIN_JUAN)
            end
            local callback = self.callback
            self.callback = nil
            if callback then
                callback(true)
            end
        end)
    else
        daiJinJuanBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_gray_3")
        daiJinJuanBtn:addClickListener(function()
            GFunc.checkCost(GConst.ItemConst.ITEM_ID_ACT_DAIJINJUAN, self.vouchers.num, true)
        end)
    end
end

return PaySelectUI