local BattleBaseUI = require "app/ui/battle/battle_base_ui"
local BattleUIPVP = class("BattleUIPVP", BattleBaseUI)

local DEFAULT_X = 10000
local ALPHA_COLOR = BF.Color(1, 1, 1, 0)
local BLACK_ALPHA_COLOR = BF.Color(0, 0, 0, 0)
local TIPS_BG = {"battle_bg_5", "battle_bg_6"}

---------------------------------必须重写的方法----------------------------------
function BattleUIPVP:initBaseInfo()
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap
    self.gridNode = uiMap["battle_ui_pvp.board_root_node.board_node_atk.grid_node"]
    self.boardNode = uiMap["battle_ui_pvp.board_root_node"]
    self.boardCenterNode = uiMap["battle_ui_pvp.board_root_node.board_center_node"]
    self.boardNode:setAnchoredPositionX(DEFAULT_X)
    self.boardNode:setVisible(true)
    self.boardMask2D = self.gridNode:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_RECT_MASK_2D)
    self.boardMask = uiMap["battle_ui_pvp.board_root_node.board_mask"]
    self.boardMask:setVisible(false)
    self.boardCacheNode = uiMap["battle_ui_pvp.board_root_node.board_cache_node"]
    self.boardCacheNode:setVisible(false)
    self.boardCacheBox = uiMap["battle_ui_pvp.board_root_node.board_cache_node.skill_box"]
    self.boardCacheBox:setAnchoredPositionX(DEFAULT_X)
    self.battleRoot = uiMap["battle_ui_pvp.battle_root"]
    self.maxLayerNode = uiMap["battle_ui_pvp.battle_root.battle_node.max_layer_show_node"]
    self.aniNode = uiMap["battle_ui_pvp.board_root_node.ani_node"]
    self.touchMask = uiMap["battle_ui_pvp.board_root_node.touch_mask"]
    self.atkBoardImg = self.uiMap["battle_ui_pvp.board_root_node.board_center_node.board_node_atk.board_atk"]
    self.defBoardImg = self.uiMap["battle_ui_pvp.board_root_node.board_center_node.board_node_atk.board_def"]
    self.atkBoard = self.uiMap["battle_ui_pvp.board_root_node.board_center_node.board_node_atk.grid_node.board_atk"]
    self.defBoard = self.uiMap["battle_ui_pvp.board_root_node.board_center_node.board_node_atk.grid_node.board_def"]
    self.boardRootNodeMask = self.uiMap["battle_ui_pvp.board_root_node.mask"]
    self.atkBoardImg:setAnchoredPositionX(0)
    self.defBoardImg:setAnchoredPositionX(0)
    self.actionTipsMask = uiMap["battle_ui_pvp.board_root_node.action_tips_mask"]
    self.actionTipsImg = uiMap["battle_ui_pvp.board_root_node.action_tips_mask.img"]
    self.actionTipsDesc = uiMap["battle_ui_pvp.board_root_node.action_tips_mask.img.desc"]
    self.gridEdgeNode = uiMap["battle_ui_pvp.board_root_node.board_center_node.board_node_atk.grid_edge_node"]
    self.gridEdgeCacheCell = uiMap["battle_ui_pvp.cache_node.grid_edge_cell"]
end

function BattleUIPVP:initBg()
    self.bg = self.uiMap["battle_ui_pvp.battle_root.bg"]
    self.bg:setLocalScale(0, 0, 0)
    local width = self.bg:fastGetSizeDelta()
    self.bg:setAnchoredPositionX(width/4)
end

function BattleUIPVP:initSkill()
    local uiMap = self.root:genAllChildren()
    local atkNode = uiMap["battle_ui_pvp.bottom_node.skill_node"]
    local defNode = uiMap["battle_ui_pvp.top_node.skill_node"]
    local atkCellPrefix = "battle_ui_pvp.bottom_node.skill_node.skill_node_cell_"
    local defCellPrefix = "battle_ui_pvp.top_node.skill_node.skill_node_cell_"
    self:_initSkill(atkNode, atkCellPrefix, defNode, defCellPrefix)
end

function BattleUIPVP:initBuff()
    local atkBuffPrefix = "battle_ui_pvp.bottom_node.buff.tiny_buff_cell_"
    local defBuffPrefix = "battle_ui_pvp.top_node.buff.tiny_buff_cell_"
    local battleBuffTipsRoot = self.uiMap["battle_ui_pvp.battle_buff_tips"]
    local battleBuffTipsMask = self.uiMap["battle_ui_pvp.battle_buff_tips.mask"]
    local battleBuffTipsBg = self.uiMap["battle_ui_pvp.battle_buff_tips.bg"]
    local battleBuffTipsBuff = self.uiMap["battle_ui_pvp.battle_buff_tips.bg.buff"]
    self:_initBuff(atkBuffPrefix, defBuffPrefix, battleBuffTipsRoot, battleBuffTipsMask, battleBuffTipsBg, battleBuffTipsBuff)
end

function BattleUIPVP:initBattlefield()
    self.battleNode = self.uiMap["battle_ui_pvp.battle_root.battle_node"]
end

function BattleUIPVP:initNumberNode()
    self.battleNumberNode = self.uiMap["battle_ui_pvp.battle_root.battle_number_node"]
    self.battleNumberRed = self.uiMap["battle_ui_pvp.cache_node.battle_number_red"]
    self.battleNumberGreen = self.uiMap["battle_ui_pvp.cache_node.battle_number_green"]
    self.battleNumberBlue = self.uiMap["battle_ui_pvp.cache_node.battle_number_blue"]
    self.battleNumberWhite = self.uiMap["battle_ui_pvp.cache_node.battle_number_white"]
    self.battleNumberSpecial = self.uiMap["battle_ui_pvp.cache_node.battle_number_special"]
end

function BattleUIPVP:initComboNode()
    self.comboNode = self.uiMap["battle_ui_pvp.battle_root.combo"]
    self.comboBg = self.uiMap["battle_ui_pvp.battle_root.combo.bg"]
    self.comboBg1 = self.uiMap["battle_ui_pvp.battle_root.combo.bg.bg_1"]
    self.comboTx1 = self.uiMap["battle_ui_pvp.battle_root.combo.number.text.text_1"]
    self.comboTx2 = self.uiMap["battle_ui_pvp.battle_root.combo.number.text.text_2"]
    self.comboFx1 = self.uiMap["battle_ui_pvp.battle_root.combo.bg.vfx_ui_hit_b01"]
    self.comboFx2 = self.uiMap["battle_ui_pvp.battle_root.combo.bg.vfx_ui_hit_b02"]
    self.comboFx3 = self.uiMap["battle_ui_pvp.battle_root.combo.bg.vfx_ui_hit_b03"]
    self:_initComboNode()
end

function BattleUIPVP:initHpNode()
    self.hpProgressLeft = self.uiMap["battle_ui_pvp.bottom_node.hp_node.hp_progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpProgressRight = self.uiMap["battle_ui_pvp.top_node.hp_node.hp_progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpTextLeft = self.uiMap["battle_ui_pvp.bottom_node.hp_node.hp"]
    self.hpTextRight = self.uiMap["battle_ui_pvp.top_node.hp_node.hp"]
    self.hpProgressYellowLeft = self.uiMap["battle_ui_pvp.bottom_node.hp_node.hp_progress_yellow"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpProgressYellowRight = self.uiMap["battle_ui_pvp.top_node.hp_node.hp_progress_yellow"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self:_initHpNode()
end

function BattleUIPVP:initFxNode()
    self.fxNode = self.uiMap["battle_ui_pvp.battle_root.batttle_fx_node"]
end

function BattleUIPVP:hideGenerateSkillGridCells()
    local generateSkillCellPrefix = "battle_ui_pvp.board_root_node.ani_node.grid_cell_"
    self:_hideGenerateSkillGridCells(generateSkillCellPrefix)
end

function BattleUIPVP:initSkillLineSfx()
    if not self.skillLineSfxs then
        self.skillLineSfxs = {}
        self.skillLineSfxs[13] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_2_1h"]
        self.skillLineSfxs[11] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_2_1v"]
        self.skillLineSfxs[15] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_2_1l"]
        self.skillLineSfxs[17] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_2_1r"]

        self.skillLineSfxs[23] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_1_2h"]
        self.skillLineSfxs[21] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_1_2v"]
        self.skillLineSfxs[25] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_1_2l"]
        self.skillLineSfxs[27] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_1_2r"]

        self.skillLineSfxs[33] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_3h"]
        self.skillLineSfxs[31] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_3v"]
        self.skillLineSfxs[35] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_3l"]
        self.skillLineSfxs[37] = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b01_3r"]
    end

    if not self.root.skillLightSfxs then
        self.root.skillLightSfxs = {
            point = {
                isLoaded = true,
                obj = self.uiMap["battle_ui_pvp.board_root_node.ani_node.sfx_skill_b03"],
            }
        }
    end

    self:_initSkillLineSfx()
end

function BattleUIPVP:initGenerateSkillEffect()
    local generateSkillEffecPrefix = "battle_ui_pvp.board_root_node.ani_node.sfx_piece_skill_b01_"
    self:_initGenerateSkillEffect(generateSkillEffecPrefix)
end

function BattleUIPVP:initCounterAttack()
    self.counterAttackNode = self.uiMap["battle_ui_pvp.battle_root.counter_attack"]
    self.counterTx = self.uiMap["battle_ui_pvp.battle_root.counter_attack.text_number"]
    self.counterTxbgComp = self.uiMap["battle_ui_pvp.battle_root.counter_attack.bg"]
    self.counterTxTmp = self.counterTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
    self.counterAttackNode:setVisible(false)
end

--------------------------------end必须重写的方法--------------------------------

function BattleUIPVP:getBGMId()
    if self.battleController then
        if self.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.ACT_PVP then
            return AudioManager.BGM_ID.ACT_PVP_FIGHT
        end
    end
    return AudioManager.BGM_ID.BATTLE
end

function BattleUIPVP:getPrefabPath()
    return "assets/prefabs/ui/battle/battle_ui_pvp.prefab"
end

function BattleUIPVP:_display()
    BattleBaseUI._display(self)
    self:refreshAvatar()

    ---- 适配
    local w, h = GFunc.getUIExpandScreenSize()
    local scale = math.min(1, h / 1280 * 0.84)
    self.boardCenterNode:setVisible(true, scale)
end

function BattleUIPVP:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["battle_ui_pvp.top_node.close_btn"]:addClickListener(function()
        ModuleManager.BattleManager:showPauseUI(self.battleController.battleType, self.battleController)
    end)
end

function BattleUIPVP:refreshAvatar()
    -- 等数据
    local defInfo = self.battleController:getControllerParams().defInfo
    if not defInfo then
        return
    end

    if not self.atkPlayerHeadCell then
        self.atkPlayerHeadCell = CellManager:addCellComp(self.uiMap["battle_ui_pvp.bottom_node.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
        self.defPlayerHeadCell = CellManager:addCellComp(self.uiMap["battle_ui_pvp.top_node.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
    end

    self.atkPlayerHeadCell:refresh()
    self.defPlayerHeadCell:refresh(defInfo.avatar, defInfo.avatar_frame)
end

function BattleUIPVP:showLeftBuffTips(buffList, autoClose)
    local addY = self:showBuffTips(buffList, autoClose)
    self.battleBuffTipsBg:setAnchoredPosition(-175, -1050 + addY)
end

function BattleUIPVP:showRightBuffTips(buffList, autoClose)
    self:showBuffTips(buffList, autoClose)
    self.battleBuffTipsBg:setAnchoredPosition(175, -200)
end

function BattleUIPVP:refreshWave(wave, iconAtlas, iconName)
    local uiMap = self.root:genAllChildren()
    local icon = uiMap["battle_ui_pvp.bottom_node.round_icon"]
    local desc = uiMap["battle_ui_pvp.bottom_node.round_text"]
    desc:setText(wave .. "/" .. self.battleController:getMaxRoundCount())
    -- GFunc.centerImgAndTx(icon, desc, 10)

    iconAtlas = iconAtlas or GConst.ATLAS_PATH.COMMON
    iconName = iconName or "common_dec_15"
    icon:setSprite(iconAtlas, iconName)
end

function BattleUIPVP:setGenerateSkillSfxsParent(posId, obj)
    local isAtkAction = self.battleController.curActionSide == GConst.BattleConst.SIDE_ATK
    if isAtkAction then
        obj:setParent(self.atkBoard, true)
    else
        obj:setParent(self.defBoard, true)
    end
end

function BattleUIPVP:dealBoardMaskOnShowAni(isOver)
    local targetX = isOver and 0 or DEFAULT_X
    self.boardMask:setAnchoredPositionX(targetX)
end

function BattleUIPVP:enterShowBoardAni(callback, skipInterval)
    self:showMask(false)
    local isAtkAction = self.battleController.curActionSide == GConst.BattleConst.SIDE_ATK
    if self.touchMask then
        self.touchMask:setVisible(not isAtkAction)
    end

    self:clearEnterShowBoardSeq()
    local w, h = GFunc.getUIExpandScreenSize()
    self.enterShowBoardSeq = self.root:createBindTweenSequence()
    self.enterShowBoardSeq:AppendCallback(function()
        local str
        local sprite
        if isAtkAction then
            str = I18N:getGlobalText(I18N.GlobalConst.ARENA_BATTLE_DESC_1)
            sprite = TIPS_BG[1]
        else
            str = I18N:getGlobalText(I18N.GlobalConst.ARENA_BATTLE_DESC_2)
            sprite = TIPS_BG[2]
        end
        self.actionTipsImg:setSprite(GConst.ATLAS_PATH.BATTLE, sprite)
        self.actionTipsImg:setAnchoredPositionX(-w - 360)
        self.actionTipsDesc:setText(str)
        self.actionTipsMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE).color = BLACK_ALPHA_COLOR
        self.boardNode:setVisible(true)
        self.boardNode:setAnchoredPositionX(0)
    end)
    if not skipInterval then
        self.enterShowBoardSeq:AppendInterval(0.5)
    end
    self.enterShowBoardSeq:AppendCallback(function()
        for posId, entity in pairs(self.battleController.battleData:getGridEnties()) do
            if entity:getCell() then
                local inCurRange = self.battleController:getPosIdInCurActionBoardRowRange(posId)
                local parent
                if isAtkAction then
                    parent = inCurRange and self.atkBoard or self.defBoard
                else
                    parent = inCurRange and self.defBoard or self.atkBoard
                end
                if parent then
                    entity:getCell():getBaseObject():setParent(parent, true)
                end
            end
        end

        for posId, info in pairs(self.battleController.battleData:getGridEdgeEntities()) do
            for direction, entity in pairs(info) do
                if entity:getCell() then
                    local inCurRange = self.battleController:getPosIdInCurActionBoardRowRange(posId)
                    local parent
                    if isAtkAction then
                        parent = inCurRange and self.atkBoard or self.defBoard
                    else
                        parent = inCurRange and self.defBoard or self.atkBoard
                    end
                    if parent then
                        entity:getCell():getBaseObject():setParent(parent, true)
                    end
                end
            end
        end
    end)

    self.enterShowBoardSeq:Join(self.defBoardImg:getTransform():DOAnchorPosX(0, 0.5))
    self.enterShowBoardSeq:Join(self.atkBoardImg:getTransform():DOAnchorPosX(0, 0.5))
    self.enterShowBoardSeq:Join(self.boardRootNodeMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):DOFade(0.9, 0.5))
    self.enterShowBoardSeq:Append(self.actionTipsMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):DOFade(0.9, 0.5))
    self.enterShowBoardSeq:Join(self.actionTipsImg:getTransform():DOAnchorPosX(0, 0.3))
    self.enterShowBoardSeq:AppendInterval(0.5)
    self.enterShowBoardSeq:Append(self.actionTipsImg:getTransform():DOAnchorPosX(w + 360, 0.3))
    self.enterShowBoardSeq:AppendCallback(function()
        self.actionTipsMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE).color = BLACK_ALPHA_COLOR
        for posId, entity in pairs(self.battleController.battleData:getGridEnties()) do
            if entity:getCell() then
                entity:getCell():getBaseObject():setParent(self.gridNode, true)
            end
        end
        for posId, info in pairs(self.battleController.battleData:getGridEdgeEntities()) do
            for direction, entity in pairs(info) do
                if entity:getCell() then
                    entity:getCell():getBaseObject():setParent(self.gridEdgeNode, true)
                end
            end
        end
        if callback then
            callback()
        end
    end)
end

function BattleUIPVP:enterHideBoardAni(callback)
    self:dealBoardMaskOnShowAni()
    self:clearEnterShowBoardSeq()
    local w, h = GFunc.getUIExpandScreenSize()
    self.enterShowBoardSeq = self.root:createBindTweenSequence()
    self.enterShowBoardSeq:AppendCallback(function()
        self.actionTipsMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE).color = BLACK_ALPHA_COLOR
        self.actionTipsImg:setAnchoredPositionX(-w - 360)
    end)
    self.enterShowBoardSeq:AppendInterval(0.5)
    self.enterShowBoardSeq:AppendCallback(function()
        local isAtkAction = self.battleController.curActionSide == GConst.BattleConst.SIDE_ATK
        for posId, entity in pairs(self.battleController.battleData:getGridEnties()) do
            if entity:getCell() then
                local inCurRange = self.battleController:getPosIdInCurActionBoardRowRange(posId)
                local parent
                if isAtkAction then
                    parent = inCurRange and self.atkBoard or self.defBoard
                else
                    parent = inCurRange and self.defBoard or self.atkBoard
                end
                if parent then
                    entity:getCell():getBaseObject():setParent(parent, true)
                end
            end
        end

        for posId, info in pairs(self.battleController.battleData:getGridEdgeEntities()) do
            for direction, entity in pairs(info) do
                if entity:getCell() then
                    local inCurRange = self.battleController:getPosIdInCurActionBoardRowRange(posId)
                    local parent
                    if isAtkAction then
                        parent = inCurRange and self.atkBoard or self.defBoard
                    else
                        parent = inCurRange and self.defBoard or self.atkBoard
                    end
                    if parent then
                        entity:getCell():getBaseObject():setParent(parent, true)
                    end
                end
            end
        end
    end)
    self.enterShowBoardSeq:Join(self.defBoardImg:getTransform():DOAnchorPosX(w + 360, 0.5))
    self.enterShowBoardSeq:Join(self.atkBoardImg:getTransform():DOAnchorPosX(-(w + 360), 0.5))
    self.enterShowBoardSeq:Join(self.boardRootNodeMask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):DOFade(0, 0.5))
    self.enterShowBoardSeq:AppendCallback(function()
        self:dealBoardMaskOnShowAni(true)
        self.boardNode:setVisible(false)
        if callback then
            callback()
        end
    end)
end

return BattleUIPVP