local DungeonWeaponMainUI = class("DungeonWeaponMainUI", BaseUI)

local CHAPTER_LAYER_CELL = "app/ui/dungeon_weapon/cell/chapter_layer_cell"
local GIFT_CELL = "app/ui/main_city/cell/side_bar_weapon_gift_cell"

function DungeonWeaponMainUI:getCurrencyParams()
    if self.currencyParams == nil then
        self.currencyParams = {
            itemIds = {
                GConst.ItemConst.ITEM_ID_GOLD,
                GConst.ItemConst.ITEM_ID_VIT
            },
            showType = GConst.CURRENCY_TYPE.HORIZONTAL
        }
    end
 	return self.currencyParams
end

function DungeonWeaponMainUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_weapon/dungeon_weapon_mian_ui.prefab"
end

function DungeonWeaponMainUI:ctor()
    self.weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    self.chapterList = {}
    self.targetId = self.weaponData:getPassedMaxId()
    for id, info in pairs(ConfigManager:getConfig("chapter_dungeon_equip")) do
        table.insert(self.chapterList, id)
        if not self.targetId then
            self.targetId = id
        end

        if id == self.weaponData:getPassedMaxId() + 1 and self.weaponData:canFightChapter(id) then -- 最大可挑战章节
            self.targetId = id
        end
    end
end

function DungeonWeaponMainUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()
    DataManager.ShopData:checkPopGift(PayManager.PURCHARSE_ACT_TYPE.WEAPON_GIFT)

    self:refreshTime()
	self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

function DungeonWeaponMainUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_weapon_mian_ui.banner.btn_formation.tx_ok"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_4))

    self:initRightBtns()
    self:refreshScrollrect()
    self:refreshFormation()
    self:refreshRemianNode()
end

function DungeonWeaponMainUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_weapon_mian_ui.banner.btn_back"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["dungeon_weapon_mian_ui.banner.btn_formation"]:addClickListener(function()
        ModuleManager.CommonManager:showFormationUI(GConst.BattleConst.FORMATION_TYPE.DUNGEON_WEAPON)
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.GO_SHOP, function()
        self:closeUI()
    end)
end

function DungeonWeaponMainUI:_bind()
    self:bind(self.weaponData, "isDirty", function()
        self:refreshFormation()
        self:refreshRemianNode()
        self:refreshScrollrect()
    end)

    self:bind(DataManager.FormationData, "dirty", function()
        self:refreshFormation()
    end)

    self:bind(DataManager.HeroData, "isDirty", function()
        self:refreshFormation()
    end)
end

function DungeonWeaponMainUI:refreshScrollrect()
    if not self.scrollRect then
        local uiMap = self.root:genAllChildren()
        self.scrollRect = uiMap["dungeon_weapon_mian_ui.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
        self.scrollRect:addInitCallback(function()
            return CHAPTER_LAYER_CELL
        end)
        self.scrollRect:addRefreshCallback(function(index, cell)
            cell:refresh(self.chapterList[index])
        end)
        self.scrollRect:refillCells(#self.chapterList, nil, self.targetId)
    else
        self.scrollRect:refreshAll()
    end
end

function DungeonWeaponMainUI:refreshFormation()
    if not self.formationComp then
        local uiMap = self.root:genAllChildren()
        self.formationComp = uiMap["dungeon_weapon_mian_ui.hero_formation_comp"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_FORMATION_COMP)
    end
    self.formationComp:refreshByFormation(DataManager.FormationData:getDungeonWeaponFormation())
end

function DungeonWeaponMainUI:refreshRemianNode()
    local uiMap = self.root:genAllChildren()
    local node = uiMap["dungeon_weapon_mian_ui.remian_node"]
    node:setAnchoredPositionX(-330)
    local txt = uiMap["dungeon_weapon_mian_ui.remian_node.desc"]
    txt:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_5, self.weaponData:getRemianFarmCount() .. "/" .. self.weaponData:getDialyFarmLimit()))
    GFunc.expandImgToFitTx(node, txt, 16.5)
end

function DungeonWeaponMainUI:refreshTime()
    self:refreshRightBtns()
end

function DungeonWeaponMainUI:initRightBtns()
    if self.rightBtnCells then
        return
    end
    local uiMap = self.root:genAllChildren()
    self.rightNode = uiMap["dungeon_weapon_mian_ui.right_node"]
    self.rightArrow = uiMap["dungeon_weapon_mian_ui.right_node.arrow_node"]
    self.rightBtnCells = {}
    self.rightBtnCells.giftCell = CellManager:addCellComp(uiMap["dungeon_weapon_mian_ui.right_node.side_bar.side_bar_cell"], GIFT_CELL)

    self.rightArrow:setVisible(false)

    self:refreshRightBtns()
end

function DungeonWeaponMainUI:refreshRightBtns()
    local y = 0
    if self.rightBtnCells.giftCell:checkIsOpen() then
        y = y - 60
        self.rightBtnCells.giftCell:setActive(true)
        self.rightBtnCells.giftCell:setVisible(true)
        self.rightBtnCells.giftCell:getBaseObject():setAnchoredPositionY(y)
        self.rightBtnCells.giftCell:refresh()
    else
        self.rightBtnCells.giftCell:setVisible(false)
    end

    -- 暂不处理箭头的问题
    self.rightNode:setVisible(y < 0)
end

return DungeonWeaponMainUI