local DungeonTargetCell = class("DungeonTargetCell", BaseCell)

function DungeonTargetCell:init()
    local uiMap = self:getUIMap()

    self.txDesc = uiMap["dungeon_target_cell.tx_desc"]
    self.btn = uiMap["dungeon_target_cell.right.btn"]
    self.txBtn = uiMap["dungeon_target_cell.right.btn.tx_btn"]
    -- 有扫荡次数
    self.timeObj = uiMap["dungeon_target_cell.time"]
    self.txTime = uiMap["dungeon_target_cell.time.tx_time"]
    self.powerObj = uiMap["dungeon_target_cell.right.power"]
    self.imgPowerIcon = uiMap["dungeon_target_cell.right.power.img_icon"]
    self.txPowerNum = uiMap["dungeon_target_cell.right.power.tx_num"]
end

function DungeonTargetCell:refresh(targetId, targetNum, getWay, id)
    self.getWay = getWay or self.getWay
    self.dungeonId = id or self.dungeonId
    if targetId and targetNum then
        self.target = {id = targetId, value = targetNum}
    end

    if self.getWay == GConst.DungeonConst.TYPE.WEAPON then
        self:showWeapon()
    elseif self.getWay == GConst.DungeonConst.TYPE.ARMOR then
        self:showArmor()
    end
end

-- 显示武器副本
function DungeonTargetCell:showWeapon()
    local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    
    local remainTime = weaponData:getRemianFarmCount()

    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_19, self.dungeonId))

    if weaponData:canFarmChapter(self.dungeonId) and remainTime > 0 then
        -- 扫荡
        self.timeObj:setActive(true)
        self.powerObj:setActive(true)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_3")
        self.txTime:setText(remainTime .."/" .. weaponData:getDialyFarmLimit())
        self.txPowerNum:setText(GFunc.getRewardNum(weaponData:getChallengeHpCost()))
        GFunc.centerImgAndTx(self.imgPowerIcon, self.txPowerNum, 2)
        self.btn:setTouchEnable(true)
    elseif weaponData:canFightChapter(self.dungeonId) then
        -- 前往
        self.timeObj:setActive(false)
        self.powerObj:setActive(false)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_21))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_3")
        self.btn:setTouchEnable(true)
    else
        -- 未开启
        self.timeObj:setActive(false)
        self.powerObj:setActive(false)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_22))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
        self.btn:setTouchEnable(false)
    end
    self.btn:addClickListener(function()
        if weaponData:canFarmChapter(self.dungeonId) and remainTime > 0 then
            ModuleManager.DungeonWeaponManager:reqSweep(self.dungeonId, self.target)
        else
            ModuleManager.DungeonWeaponManager:showMainUI()
            EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI)
        end
    end)
end

-- 显示防具副本
function DungeonTargetCell:showArmor()
    local armorData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    
    local remainTime = armorData:getRemianFarmCount(self.dungeonId)

    local cfg = armorData:getConfig(self.dungeonId)
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_20, cfg.chapter, cfg.stage))

    if armorData:canFarmChapter(self.dungeonId) and remainTime > 0 then
        -- 扫荡
        self.timeObj:setActive(true)
        self.powerObj:setActive(true)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_3")
        self.txTime:setText(remainTime .."/" .. armorData:getDialyFarmLimit(self.dungeonId))
        self.txPowerNum:setText(GFunc.getRewardNum(armorData:getChallengeHpCost()))
        GFunc.centerImgAndTx(self.imgPowerIcon, self.txPowerNum, 2)
        self.btn:setTouchEnable(true)
        
    elseif armorData:canFightChapter(self.dungeonId) then
        -- 前往
        self.timeObj:setActive(false)
        self.powerObj:setActive(false)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_21))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_3")
        self.btn:setTouchEnable(true)
    else
        -- 未开启
        self.timeObj:setActive(false)
        self.powerObj:setActive(false)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_22))
        self.btn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
        self.btn:setTouchEnable(false)
    end
    self.btn:addClickListener(function()
        if armorData:canFarmChapter(self.dungeonId) and remainTime > 0 then
            ModuleManager.DungeonArmorManager:reqSweep(self.dungeonId, self.target)
        else
            ModuleManager.DungeonArmorManager:showMainUI()
            EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI)
        end
    end)
end

return DungeonTargetCell