local BattleSkillSelectComp = class("BattleSkillSelectComp", LuaComponent)

local SELECT_SKILL_CELL = "app/ui/battle/cell/battle_select_skill_cell"
local BATTLE_COMMON_PATH = "assets/arts/textures/background/battle_common/%s.png"

local SKILL_ICON_POS = {
    {x =-240, y= 165},
    {x =-240, y= 0},
    {x =-240, y= -165}
}

function BattleSkillSelectComp:refresh(skillList, onlyCommonSkill)
    self.skillList = skillList
    self.onlyCommonSkill = onlyCommonSkill
    self:_playPop()
    self:_display()
    self:_addListeners()
end

function BattleSkillSelectComp:_display()
    local uiMap = self:getUIMap()
    local bg2 = uiMap["battle_select_skill_comp.bg_2"]
    bg2:setActive(self.onlyCommonSkill)
    local bg = uiMap["battle_select_skill_comp.bg_1"]
    if ModuleManager.BattleManager.battleController then
        bg:setVisible(false)
        bg:setTexture(string.format(BATTLE_COMMON_PATH, ModuleManager.BattleManager.battleController:getChessBoardBgName() .. "_1"), function()
            bg:setVisible(true)
        end)
    else
        bg:setVisible(true)
    end

    self:refreshBtns()
    self:refreshRogueSkill()
end

function BattleSkillSelectComp:_addListeners()
    local uiMap = self:getUIMap()
    uiMap["battle_select_skill_comp.skill_node.ad_btn"]:addClickListener(function()
        if not ModuleManager.BattleManager.battleController then
            return
        end

        SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.BATTLE_SKILL_REFRESH, function()
            ModuleManager.BattleManager:reqSkillRefresh(true)
        end)
    end)

    uiMap["battle_select_skill_comp.skill_node.diamond_btn"]:addClickListener(function()
        if not ModuleManager.BattleManager.battleController then
            return
        end

        ModuleManager.BattleManager:reqSkillRefresh(false)
    end)

    self.canvasGroup = uiMap["battle_select_skill_comp.skill_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
    self.canvasGroup.alpha = 1
    self.bg = uiMap["battle_select_skill_comp.bg_1"]
    self.bg2 = uiMap["battle_select_skill_comp.bg_2"]
    uiMap["battle_select_skill_comp.look_btn"]:addTouchListener(function(eventType, x, y)
        if eventType == GConst.TOUCH_EVENT.DOWN or eventType == GConst.TOUCH_EVENT.DRAG then
            self.canvasGroup.alpha = 0.3
            self.bg:setVisible(false)
            self.bg2:setVisible(false)
        else
            self.canvasGroup.alpha = 1
            self.bg:setVisible(true)
            self.bg2:setVisible(true)
		end
    end)
end

function BattleSkillSelectComp:refreshBtns()
    local uiMap = self:getUIMap()
    uiMap["battle_select_skill_comp.skill_node.ad_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_3))
    uiMap["battle_select_skill_comp.skill_node.diamond_btn.tx"]:setText(GFunc.getRewardNum(GFunc.getConstReward("refresh_skill_cost")))

    local battleController = ModuleManager.BattleManager.battleController
    local cfgAdCount = GFunc.getConstIntValue("ad_refresh_skill")
    local cfgRefreshCount = GFunc.getConstIntValue("diamond_refresh_skill") + cfgAdCount
    local adCount = battleController.battleData:getADRefreshSkillCount()
    local refreshCount =battleController.battleData:getRefreshSkillCount()
    local showAdBtn = cfgAdCount > adCount
    uiMap["battle_select_skill_comp.skill_node.ad_btn"]:setActive(showAdBtn)
    local showBtn = refreshCount < cfgRefreshCount
    if showAdBtn then
        showBtn = false
    end
    uiMap["battle_select_skill_comp.skill_node.diamond_btn"]:setActive(showBtn)
end

function BattleSkillSelectComp:getNewRandomSkill()
    self:refreshBtns()
    local excludeMap = {}
    for _, id in ipairs(self.skillList) do
        excludeMap[id] = true
    end
    self.skillList = ModuleManager.BattleManager.battleController:getRandomSkillList(nil, self.onlyCommonSkill, excludeMap)
    self:refreshRogueSkill()
end

function BattleSkillSelectComp:refreshRogueSkill()
    local uiMap = self:getUIMap()
    if not self.selectSkillCells then
        self.selectSkillCells = {}
        for i = 1, 3 do
            self.selectSkillCells[i] = CellManager:addCellComp(uiMap["battle_select_skill_comp.skill_node.skill_select_cell_" .. i], SELECT_SKILL_CELL)
        end
    end

    for index, cell in ipairs(self.selectSkillCells) do
        local skillId = self.skillList[index]
        cell:getBaseObject():setActive(skillId ~= nil)
        if skillId then
            cell:refresh(skillId, function(value)
                self:onClickSkill(skillId, value, SKILL_ICON_POS[index])
            end)
        end
    end
end

function BattleSkillSelectComp:onClickSkill(skillId, value, pos)
    if self.isInAni then
        return
    end
    self:hide()
    ModuleManager.BattleManager:onSelectSkill(skillId, value, pos)
end

function BattleSkillSelectComp:hide()
    self.baseObject:setVisible(false)
end

function BattleSkillSelectComp:_playPop()
    self.isInAni = true
	self.popSequence = self.baseObject:createBindTweenSequence()

	local scaleTween1 = self.baseObject:getTransform():DOScale(1.05, 0.15)
	self.popSequence:Append(scaleTween1)

	local scaleTween2 = self.baseObject:getTransform():DOScale(1, 0.2)
	self.popSequence:Append(scaleTween2)

	self.popSequence:AppendCallback(function()
        self.isInAni = false
		self.popSequence = nil
	end)
end

return BattleSkillSelectComp