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= 154},
    {x =-240, y= -2},
    {x =-240, y= -158},
    {x =-240, y= 310},
}

function BattleSkillSelectComp:refresh(skillList, isGodSkill)
    self.skillList = skillList
    self.isGodSkill = isGodSkill
    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.isGodSkill)
    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()
        end)
    end)

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

		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.BATTLE_SKILL_ALL, function()
            ModuleManager.BattleManager:reqSkillRefresh(true)
        end)
    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.all_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_19))
	-- uiMap["battle_select_skill_comp.skill_node.diamond_btn.tx"]:setText(GFunc.getRewardNum(GFunc.getConstReward("refresh_skill_cost")))

	local adBtn = uiMap["battle_select_skill_comp.skill_node.ad_btn"]
	local diamondBtn = uiMap["battle_select_skill_comp.skill_node.diamond_btn"]
	local allBtn = uiMap["battle_select_skill_comp.skill_node.all_btn"]
	diamondBtn:setActive(false)
	if self.isGodSkill then
		adBtn:setActive(false)
		allBtn:setActive(false)
		return
	end

    if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.AD, true) then
		adBtn:setActive(false)
		allBtn:setActive(false)
		return
	end

    local battleController = ModuleManager.BattleManager.battleController
	local getAllAdCount = 0
	if ModuleManager.BattleManager.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.STAGE then
		getAllAdCount = GFunc.getConstIntValue("ads_getall_rogue_limit")
	elseif ModuleManager.BattleManager.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
		getAllAdCount = GFunc.getConstIntValue("daily_challenge_ads_getall_rogue_limit")
	end
	local allCount =  battleController.battleData:getAllSkillCount()
    local showAll = false
    local showAd = false
	if getAllAdCount > allCount then
		allBtn:setActive(true)
        showAll = true
	else
		allBtn:setActive(false)
	end

	local cfgAdCount = 0
	if ModuleManager.BattleManager.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.STAGE then
		cfgAdCount = GFunc.getConstIntValue("ads_refresh_rogue_limit")
	elseif ModuleManager.BattleManager.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
		cfgAdCount = GFunc.getConstIntValue("daily_challenge_ads_refresh_rogue_limit")
	end
    local talentCount = DataManager.TalentData:getSkillRefreshCount()
    cfgAdCount = cfgAdCount + talentCount
	local adCount = battleController.battleData:getADRefreshSkillCount()
	if cfgAdCount > adCount then
		adBtn:setActive(true)
        showAd = true
	else
		adBtn:setActive(false)
	end
    if showAll and showAd then
        allBtn:setAnchoredPositionX(102)
        adBtn:setAnchoredPositionX(-102)
    elseif showAll then
        allBtn:setAnchoredPositionX(0)
    elseif showAd then
        adBtn:setAnchoredPositionX(0)
    end
end

function BattleSkillSelectComp:getNewRandomSkill(parmas)
	parmas = parmas or {}
	if parmas.isAll then
		self:hide()
		for i,v in ipairs(self.skillList) do
			ModuleManager.BattleManager:onSelectSkill(v, nil, SKILL_ICON_POS[i])
		end
		return
	end
	if parmas.deitySkillIdx then
		self:hide()
		ModuleManager.BattleManager:onSelectSkill(self.skillList[parmas.deitySkillIdx], nil, SKILL_ICON_POS[parmas.deitySkillIdx])
		return
	end
    self:refreshBtns()
    local excludeMap = {}
    for _, id in ipairs(self.skillList) do
        excludeMap[id] = true
    end
    self.skillList = ModuleManager.BattleManager.battleController:getRandomSkillList(nil, self.isGodSkill, excludeMap)
    self:refreshRogueSkill()
end

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

	for i = 1, 4 do
		local skillId = self.skillList[i]
		if skillId then
			self.selectSkillCells[i]:getBaseObject():setActive(true)
			self.selectSkillCells[i]:refresh(skillId, function(value)
				if i == 4 then
					SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.BATTLE_SKILL_DEITY, function()
                        self:onClickSkill(skillId, value, SKILL_ICON_POS[i])
					end)
				else
                	self:onClickSkill(skillId, value, SKILL_ICON_POS[i])
				end
            end)
		else
			self.selectSkillCells[i]:getBaseObject():setActive(false)
		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