local HeroDetailUI = class("HeroDetailUI", BaseUI)
local COMP_HERO = "app/ui/hero/hero_info_comp"
local COMP_WEAPON = "app/ui/hero/weapon_info_comp"
local COMP_ARMOR = "app/ui/hero/armor_info_comp"
local COMP_RUNES = "app/ui/hero/runes_info_comp"
local SIZE_DELTA_Y_HERO = 942
local SIZE_DELTA_Y_LOOK_HERO = 802

function HeroDetailUI:isFullScreen()
    return false
end

function HeroDetailUI:getPrefabPath()
    return "assets/prefabs/ui/hero/hero_detail_ui.prefab"
end

function HeroDetailUI:onPressBackspace()
	self:closeUI()
end

function HeroDetailUI:onClose()
    if self.compRunes then
        self.compRunes:onClose()
    end
end

function HeroDetailUI:ctor(parmas)
    self.panelType = parmas.panelType or GConst.HeroConst.PANEL_TYPE.HERO
    self.formationType = parmas.formationType
    self.onlyLook = parmas.onlyLook
    if parmas.heroEntity then
        self.heroEntity = parmas.heroEntity
    else
        local heroId = parmas.heroId
        self.heroEntity = DataManager.HeroData:getHeroById(heroId)
    end
end

function HeroDetailUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()

    self.commonInfo = uiMap["hero_detail_ui.common"]
    self.heroInfo = uiMap["hero_detail_ui.hero_info"]
    self.weaponInfo = uiMap["hero_detail_ui.weapon_info"]
    self.armorInfo = uiMap["hero_detail_ui.armor_info"]
    self.runesInfo = uiMap["hero_detail_ui.runes_info"]
    self.txTitle = uiMap["hero_detail_ui.common.img_title.tx_title"]
    self.btnClose = uiMap["hero_detail_ui.common.btn_close"]
    self.bg = uiMap["hero_detail_ui.common.bg.bg"]
    self.btnHero = uiMap["hero_detail_ui.common.btns.btn_hero"]
    self.txHero1 = uiMap["hero_detail_ui.common.btns.btn_hero.tx_btn"]
    self.selectHero = uiMap["hero_detail_ui.common.btns.btn_hero.select"]
    self.txHero2 = uiMap["hero_detail_ui.common.btns.btn_hero.select.tx_select"]
    self.btnWeapon = uiMap["hero_detail_ui.common.btns.btn_weapon"]
    self.lockWeapon = uiMap["hero_detail_ui.common.btns.btn_weapon.img_lock"]
    self.txWeapon1 = uiMap["hero_detail_ui.common.btns.btn_weapon.tx_btn"]
    self.selectWeapon = uiMap["hero_detail_ui.common.btns.btn_weapon.select"]
    self.txWeapon2 = uiMap["hero_detail_ui.common.btns.btn_weapon.select.tx_select"]
    self.btnArmor = uiMap["hero_detail_ui.common.btns.btn_armor"]
    self.lockArmor = uiMap["hero_detail_ui.common.btns.btn_armor.img_lock"]
    self.txArmor1 = uiMap["hero_detail_ui.common.btns.btn_armor.tx_btn"]
    self.selectArmor = uiMap["hero_detail_ui.common.btns.btn_armor.select"]
    self.txArmor2 = uiMap["hero_detail_ui.common.btns.btn_armor.select.tx_select"]
    self.btnRunes = uiMap["hero_detail_ui.common.btns.btn_runes"]
    self.lockRunes = uiMap["hero_detail_ui.common.btns.btn_runes.content.img_lock"]
    self.txRunes1 = uiMap["hero_detail_ui.common.btns.btn_runes.content.tx_btn"]
    self.selectRunes = uiMap["hero_detail_ui.common.btns.btn_runes.select"]
    self.txRunes2 = uiMap["hero_detail_ui.common.btns.btn_runes.select.tx_select"]
    self.btnLeft = uiMap["hero_detail_ui.common.btn_left"]
    self.btnRight = uiMap["hero_detail_ui.common.btn_right"]

    self.txHero1:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
    self.txHero2:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
    self.txWeapon1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_1))
    self.txWeapon2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_1))
    self.txArmor1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_2))
    self.txArmor2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_2))
    self.txRunes1:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_6))
    self.txRunes2:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_6))
    if not DataManager.EquipData:isWeaponOpen() then
        self.lockWeapon:setVisible(true)
        GFunc.centerImgAndTx(self.lockWeapon, self.txWeapon1, 5)
    else
        self.lockWeapon:setVisible(false)
    end
    if not DataManager.EquipData:isArmorOpen() then
        self.lockArmor:setVisible(true)
        GFunc.centerImgAndTx(self.lockArmor, self.txArmor1, 5)
    else
        self.lockArmor:setVisible(false)
    end
    if not DataManager.RunesData:isOpen() then
        self.lockRunes:setVisible(true)
        GFunc.centerImgAndTx(self.lockRunes, self.txRunes1, 5)
    else
        self.lockRunes:setVisible(false)
    end

    self.heroList = DataManager.HeroData:getAllHeroesSort(self.formationType)
    self:updateSide()
    self:refreshShow()

    self.btnHero:addClickListener(function()
        self.panelType = GConst.HeroConst.PANEL_TYPE.HERO
        self:refreshShow()
    end)
    self.btnWeapon:addClickListener(function()
        if not DataManager.EquipData:isWeaponOpen(true) then
            return
        end
        self.panelType = GConst.HeroConst.PANEL_TYPE.WEAPON
        self:refreshShow()
    end)
    self.btnArmor:addClickListener(function()
        if not DataManager.EquipData:isArmorOpen(true) then
            return
        end
        self.panelType = GConst.HeroConst.PANEL_TYPE.ARMOR
        self:refreshShow()
    end)
    self.btnRunes:addClickListener(function()
        if not DataManager.RunesData:isOpen(true) then
            return
        end
        if not self.heroEntity:isActived() then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_24))
            return
        end
        self.panelType = GConst.HeroConst.PANEL_TYPE.RUNES
        self:refreshShow()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self.btnLeft:addClickListener(function()
        if not self:isShowLeftArrow() then
            return
        end
        self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxLast].cfgId)
        self:updateSide()
        self:refreshShow()
    end)
    self.btnRight:addClickListener(function()
        if not self:isShowRightArrow() then
            return
        end
        self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxNext].cfgId)
        self:updateSide()
        self:refreshShow()
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.RUNE_QUENCHING_SUCCESS, function(tempCount)
        if DataManager.RunesData:hasRedPoint(tempCount) then
            self.btnRunes:addRedPoint(-55, 0, 0.6)
        else
            self.btnRunes:removeRedPoint()
        end
    end)
    self:bind(DataManager.BagData.ItemData, "dirty", function()
        self:refreshRedPoint()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.HERO_UPGRADE_SUCCESS, function(id)
        self:refreshRedPoint()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPSECTION_SUCCESS, function(part)
        Logger.logHighlight("升段："..part)
        if part == GConst.EquipConst.PART_TYPE.WEAPON then
            self.compWeapon:playEffect(true, true)
        else
            self.compArmor:playUpgradeEffect(part)
        end
        self:refreshRedPoint()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPGRADE_SUCCESS, function(part)
        Logger.logHighlight("升级："..part)
        if part == GConst.EquipConst.PART_TYPE.WEAPON then
            self.compWeapon:playEffect(true, false)
        else
            self.compArmor:playUpgradeEffect(part)
        end
        self:refreshRedPoint()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI, function()
        self:closeUI()
    end)
end

function HeroDetailUI:updateSide()
    for index, data in ipairs(self.heroList) do
        if data.cfgId == self.heroEntity:getCfgId() then
            self.idxLast = index - 1
            self.idxNext = index + 1
        end
    end
end

function HeroDetailUI:refreshShow()
    self.txTitle:setText(self.heroEntity:getName())
    if self.panelType == GConst.HeroConst.PANEL_TYPE.HERO then
        self:showHeroInfo()
    elseif self.panelType == GConst.HeroConst.PANEL_TYPE.WEAPON then
        self:showWeaponInfo()
    elseif self.panelType == GConst.HeroConst.PANEL_TYPE.ARMOR then
        self:showArmorInfo()
    elseif self.panelType == GConst.HeroConst.PANEL_TYPE.RUNES then
        self:showRunesInfo()
    end
    if self.onlyLook then -- 仅查看的不显示升级和激活按钮
        self.btnHero:setActive(false)
        self.btnWeapon:setActive(false)
        self.btnArmor:setActive(false)
        self.btnRunes:setActive(false)
        self.btnLeft:setActive(false)
        self.btnRight:setActive(false)
        if self.panelType == GConst.HeroConst.PANEL_TYPE.HERO then
            self.commonInfo:setSizeDeltaY(SIZE_DELTA_Y_LOOK_HERO)
        end
    else
        self.btnHero:setActive(true)
        self.btnWeapon:setActive(true)
        self.btnArmor:setActive(true)
        self.btnRunes:setActive(true)
        self.btnLeft:setActive(self:isShowLeftArrow())
        self.btnRight:setActive(self:isShowRightArrow())
        self.commonInfo:setSizeDeltaY(SIZE_DELTA_Y_HERO)
        self:refreshRedPoint()
    end
end

-- 刷新标签红点
function HeroDetailUI:refreshRedPoint()
    if self.heroEntity:canLvUp() then
        self.btnHero:addRedPoint(-55, 0, 0.6)
    else
        self.btnHero:removeRedPoint()
    end
    if DataManager.EquipData:canUpgradeWeapon(self.heroEntity:getCfgId()) then
        self.btnWeapon:addRedPoint(-55, 0, 0.6)
    else
        self.btnWeapon:removeRedPoint()
    end
    if DataManager.EquipData:canUpgradeArmor(self.heroEntity:getCfgId()) then
        self.btnArmor:addRedPoint(-55, 0, 0.6)
    else
        self.btnArmor:removeRedPoint()
    end
    if DataManager.RunesData:hasRedPoint() then
        self.btnRunes:addRedPoint(-55, 0, 0.6)
    else
        self.btnRunes:removeRedPoint()
    end
end

function HeroDetailUI:showHeroInfo()
    self.heroInfo:setActive(true)
    self.selectHero:setActive(true)
    self.weaponInfo:setActive(false)
    self.selectWeapon:setActive(false)
    self.armorInfo:setActive(false)
    self.selectArmor:setActive(false)
    self.runesInfo:setActive(false)
    self.selectRunes:setActive(false)
    self.bg:setActive(true)

    if not self.compHero then
        self.heroInfo:initPrefabHelper()
        self.heroInfo:genAllChildren()
        self.compHero = self.heroInfo:addLuaComponent(COMP_HERO)
    end

    self.compHero:setHeroData(self.heroEntity, self.onlyLook)
    self.compHero:refresh()
end

function HeroDetailUI:showWeaponInfo()
    self.heroInfo:setActive(false)
    self.selectHero:setActive(false)
    self.weaponInfo:setActive(true)
    self.selectWeapon:setActive(true)
    self.armorInfo:setActive(false)
    self.selectArmor:setActive(false)
    self.runesInfo:setActive(false)
    self.selectRunes:setActive(false)
    self.bg:setActive(true)

    if not self.compWeapon then
        self.weaponInfo:initPrefabHelper()
        self.weaponInfo:genAllChildren()
        self.compWeapon = self.weaponInfo:addLuaComponent(COMP_WEAPON)
        self.compWeapon:setUI(self)
    end

    self.compWeapon:setHeroData(self.heroEntity)
    self.compWeapon:refresh()
end

function HeroDetailUI:showArmorInfo()
    self.heroInfo:setActive(false)
    self.selectHero:setActive(false)
    self.weaponInfo:setActive(false)
    self.selectWeapon:setActive(false)
    self.armorInfo:setActive(true)
    self.selectArmor:setActive(true)
    self.runesInfo:setActive(false)
    self.selectRunes:setActive(false)
    self.bg:setActive(true)

    if not self.compArmor then
        self.armorInfo:initPrefabHelper()
        self.armorInfo:genAllChildren()
        self.compArmor = self.armorInfo:addLuaComponent(COMP_ARMOR)
        self.compArmor:setUI(self)
    end

    self.compArmor:setHeroData(self.heroEntity)
    self.compArmor:refresh()
end

function HeroDetailUI:showRunesInfo()
    self.heroInfo:setActive(false)
    self.selectHero:setActive(false)
    self.weaponInfo:setActive(false)
    self.selectWeapon:setActive(false)
    self.armorInfo:setActive(false)
    self.selectArmor:setActive(false)
    self.runesInfo:setActive(true)
    self.selectRunes:setActive(true)
    self.bg:setActive(false)

    if not self.compRunes then
        self.runesInfo:initPrefabHelper()
        self.runesInfo:genAllChildren()
        self.compRunes = self.runesInfo:addLuaComponent(COMP_RUNES)
    end

    self.compRunes:setHeroData(self.heroEntity)
    self.compRunes:refresh()
end

-- 是否显示左箭头
function HeroDetailUI:isShowLeftArrow()
    if self.panelType == GConst.HeroConst.PANEL_TYPE.RUNES then
        return false
    end

    return self.idxLast and self.idxLast > 0
end

-- 是否显示右箭头
function HeroDetailUI:isShowRightArrow()
    if self.panelType == GConst.HeroConst.PANEL_TYPE.RUNES then
        return false
    end

    return self.idxNext and self.idxNext <= #self.heroList
end

return HeroDetailUI