local BaseTips = require "app/ui/tips/base_tips"
local BattleBoardSkillTips = class("BattleBoardSkillTips", BaseTips)

local SELECT_SKILL_CELL = "app/ui/battle/cell/select_skill_cell"
local MIN_HEIGHT = 248
local MAX_HEIGHT = 348
local NO_SKILL_HRIGHT = 136

-- 响应安卓后退事件
function BattleBoardSkillTips:onPressBackspace()
    self:closeUI()
end

function BattleBoardSkillTips:ctor(params)
	self.params = params
    self.heroEntity = params.heroEntity
    self.battleData = params.battleController.battleData
    self.side = params.side
    self.boardSkillEntity = self.battleData:getSkillEntityByElement(self.heroEntity:getMatchType())
    self.battleUnitEntity = self.battleData:getTeamBySide(self.side):getAllMembers()[self.heroEntity:getMatchType()]
	self.tarCornerScreenPos = params.tarCornerScreenPos
    self.location = params.location
end

function BattleBoardSkillTips:getPrefabPath()
	return "assets/prefabs/ui/tips/battle_skill_tips.prefab"
end

function BattleBoardSkillTips:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
    self.bg = uiMap["battle_skill_tips.bg_1"]
    self.mask = uiMap["battle_skill_tips.mask"]
    self.atkDesc = uiMap["battle_skill_tips.bg_1.atk_desc"]
    self.skillDesc = uiMap["battle_skill_tips.bg_1.skill_desc"]
    self.validEffectDesc = uiMap["battle_skill_tips.bg_1.valid_effect_desc"]
    self.gridLayout = uiMap["battle_skill_tips.bg_1.grid_layout"]
    if not self.selectSkillCells then
        self.selectSkillCells = {}
        for i = 1, 7 do
            self.selectSkillCells[i] = CellManager:addCellComp(uiMap["battle_skill_tips.bg_1.grid_layout.skill_select_cell_" .. i], SELECT_SKILL_CELL)
        end
    end

	local tipsBgTransform = self.bg:getTransform()
    self.originSizeDelta = tipsBgTransform.sizeDelta
	self.originPivot = tipsBgTransform.pivot
	self.originAnchoredPosition = tipsBgTransform.anchoredPosition
	self.originLocalPosition = tipsBgTransform.localPosition

    self.bg:addClickListener(function()
        local cfg = ConfigManager:getConfig("skill")[self.boardSkillEntity:getOriginId()]
        if cfg.buff_id and #cfg.buff_id > 0 then
            ModuleManager.TipsManager:showSkillTips(self.bg, cfg.buff_id)
        end
    end)
end

function BattleBoardSkillTips:onRefresh()
	self.mask:addClickListener(function ()
		self:closeUI()
	end)

    local heroId = self.heroEntity:getCfgId()
    local heroNmae = ModuleManager.HeroManager:getHeroName(heroId)
    local atk = self.battleUnitEntity:getAtk()
    self.atkDesc:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_9, heroNmae, atk))
    self.skillDesc:setText(ModuleManager.HeroManager:getSkillDesc(self.heroEntity:getBaseSkill()))
    self.validEffectDesc:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_10))

    local addY = self.skillDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight - self.skillDesc:fastGetSizeDeltaY()
    if addY < 0 then
        addY = 0
    end

    local count = 0
    local rougeSkillList = self.heroEntity:getActiveRogueSkills()
    for index, cell in ipairs(self.selectSkillCells) do
        local rogueSkillId = rougeSkillList[index]
        cell:getBaseObject():setActive(rogueSkillId ~= nil)
        if rogueSkillId then
            local selectedCount = self.battleData:getSkillCount(rogueSkillId, self.side)
            if selectedCount > 0 then
                count = count + 1
                local skillId = rogueSkillId
                local count = selectedCount
                local value = self.battleData:getSelectSkillMap(self.side)[skillId].value or 0
                cell:refresh(skillId, count)
                cell:addClickListener(function()
                    local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
                    ModuleManager.TipsManager:showSkillTips(cell:getBaseObject(), cfg.buff_id, skillId)
                end)
            else
                cell:getBaseObject():setActive(false)
            end
        end
    end

    self.gridLayout:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRID_LAYOUT):RefreshLayout()
    self.gridLayout:setAnchoredPositionY(-148 - addY)
    self.validEffectDesc:setAnchoredPositionY(-118 - addY)
    if count > 5 then
        addY = addY + MAX_HEIGHT
    elseif count >= 1 then
        addY = addY + MIN_HEIGHT
    else
        addY = addY + NO_SKILL_HRIGHT
        self.validEffectDesc:setText(GConst.EMPTY_STRING)
    end

    self.bg:setSizeDeltaY(addY)

	if self.tarCornerScreenPos then
        self:locate(self.location, self.originSizeDelta, self.bg, self.tarCornerScreenPos)
    end
end

function BattleBoardSkillTips:onClose()
	if self.originSizeDelta then
		local tipsBgTransform = self.bg:getTransform()
		tipsBgTransform.sizeDelta = self.originSizeDelta
		tipsBgTransform.pivot = self.originPivot
		tipsBgTransform.anchoredPosition = self.originAnchoredPosition
		tipsBgTransform.localPosition = self.originLocalPosition
	end
end

return BattleBoardSkillTips