local HeroSkinUI = class("HeroSkinUI", BaseUI)
local SIZE_DELTA_Y_SKIN = 942
local SIZE_DELTA_Y_LOOK = 720

function HeroSkinUI:isFullScreen()
    return false
end

function HeroSkinUI:getPrefabPath()
    return "assets/prefabs/ui/hero/hero_skin_ui.prefab"
end

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

function HeroSkinUI:ctor(parmas)
    self.heroEntity = DataManager.HeroData:getHeroById(parmas.heroId)

    if parmas.onlyLookSkinId then
        self.onlyLook = true
        self.selectSkinId = parmas.onlyLookSkinId
    else
        self.onlyLook = false
        self.selectSkinId = self.heroEntity:getSkinId()
    end
end

function HeroSkinUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.bg = uiMap["hero_skin_ui.bg"]
    self.content = uiMap["hero_skin_ui.content"]
    self.txTitle = uiMap["hero_skin_ui.common.img_title.tx_title"]
    self.btnClose = uiMap["hero_skin_ui.common.btn_close"]
    self.txName = uiMap["skin_info.name.tx_name"]
    self.bgQlt = uiMap["skin_info.bg_qlt"]
    self.iconQlt = uiMap["skin_info.bg_qlt.icon_qlt"]
    self.txQlt = uiMap["skin_info.bg_qlt.tx_qlt"]
    self.spineSkin = uiMap["skin_info.spine_skin"]
    self.txDesc1 = uiMap["skin_info.tx_desc_1"]
    self.txDesc2 = uiMap["skin_info.tx_desc_2"]
    self.scrollrectComp = uiMap["skin_info.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollrectContent = uiMap["skin_info.scrollrect.viewport.content"]
    self.btnUse = uiMap["skin_info.bottom.btn_use"]
    self.txBtn = uiMap["skin_info.bottom.btn_use.tx_desc"]
    self.txSource = uiMap["skin_info.bottom.tx_desc"]
    self.btnAttr = uiMap["skin_info.attr_2.btn_attr"]
    self.attr = {}
    for i = 1, 2 do
        self.attr[i] = uiMap["skin_info.attr_" .. i]
    end

    self.txTitle:setText(self.heroEntity:getName())
    self.txDesc1:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN_ATTR))
    self.txDesc2:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN_LIST))

    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self.btnUse:addClickListener(function()
        ModuleManager.SkinManager:reqChangeSkin(self.heroEntity:getCfgId(), self.selectSkinId)
    end)
    self.btnAttr:addClickListener(function()
        UIManager:showUI("app/ui/hero/hero_attr_ui", {heroEntity = self.heroEntity})
    end)
    self:bind(self.heroEntity, "isDirty", function()
        self:onRefresh()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.SKIN_SELECT, function(skinId)
        self:refreshSelectSkin(skinId)
    end)
end

function HeroSkinUI:onRefresh()
    local ids = DataManager.SkinData:getHeroAllSkinIdsSort(self.heroEntity:getCfgId())
    self.scrollrectComp:addInitCallback(function()
		return "app/ui/hero/cell/skin_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(ids[index].id, ids[index].id == self.selectSkinId)
	end)
	self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#ids)

    self:refreshSelectSkin()
end

-- 刷新选择皮肤相关内容
function HeroSkinUI:refreshSelectSkin(selectId)
    if selectId then
        self.selectSkinId = selectId
        self.scrollrectComp:updateAllCell()
    end

    local qlt = DataManager.SkinData:getSkinCfg(self.selectSkinId).qlt
    self.bgQlt:setSprite(GConst.ATLAS_PATH.HERO, "hero_skin_bg_" .. qlt)
    self.iconQlt:setSprite(GConst.ATLAS_PATH.HERO, "hero_skin_" .. qlt)
    self.txQlt:setText("<color=" .. GConst.QLT_COLOR[qlt] .. ">" .. I18N:getGlobalText("QLT_DESC_" .. qlt) .. "</color>")
    self.txName:setText(DataManager.SkinData:getName(self.selectSkinId))

    -- 属性
    local showAttr = false
    local curAttr = DataManager.SkinData:getAttr(self.selectSkinId)
    local showAttrType = {}
    for index, obj in ipairs(self.attr) do
        if curAttr and curAttr[index] then
            showAttr = true
            obj:setVisible(true)
            local map = obj:genAllChildren()
            local imgIcon = map["img_icon"]
            local txTitle = map["tx_title"]
            local txNum = map["tx_num"]

            if curAttr[index].type == GConst.MATCH_ATTACK_NAME[self.heroEntity:getMatchType()] then
                -- 攻击
                table.insert(showAttrType, GConst.MATCH_ATTACK_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_5")
                txTitle:setText("<color=#FCB501>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_ATK).."</color>")
            elseif curAttr[index].type == GConst.MATCH_ATTACK_ADD_NAME[self.heroEntity:getMatchType()] then
                -- 攻击加成百分比
                table.insert(showAttrType, GConst.MATCH_ATTACK_ADD_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_5")
                txTitle:setText("<color=#FCB501>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_ATK).."</color>")
            elseif curAttr[index].type == GConst.MATCH_HP_FIX_NAME[self.heroEntity:getMatchType()] then
                -- 生命
                table.insert(showAttrType, GConst.MATCH_HP_FIX_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_4")
                txTitle:setText("<color=#FB6895>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_HP).."</color>")
            elseif curAttr[index].type == GConst.MATCH_HP_ADD_NAME[self.heroEntity:getMatchType()] then
                -- 生命加成百分比
                table.insert(showAttrType, GConst.MATCH_HP_ADD_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_4")
                txTitle:setText("<color=#FB6895>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_HP).."</color>")
            elseif curAttr[index].type == GConst.MATCH_NORMAL_HURT_NAME[self.heroEntity:getMatchType()] then
                -- 普攻增伤
                table.insert(showAttrType, GConst.MATCH_NORMAL_HURT_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_20")
                txTitle:setText("<color=#4CCFFA>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_NORMAL_HURT).."</color>")
            elseif curAttr[index].type == GConst.MATCH_SKILL_HURT_NAME[self.heroEntity:getMatchType()] then
                -- 技能增伤
                table.insert(showAttrType, GConst.MATCH_SKILL_HURT_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_21")
                txTitle:setText("<color=#EC80FF>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_SKILL_HURT).."</color>")
            elseif curAttr[index].type == GConst.MATCH_NORMAL_HURTP_NAME[self.heroEntity:getMatchType()] then
                -- 普攻增伤百分比
                table.insert(showAttrType, GConst.MATCH_NORMAL_HURTP_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_20")
                txTitle:setText("<color=#4CCFFA>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_NORMAL_HURTP).."</color>")
            elseif curAttr[index].type == GConst.MATCH_SKILL_HURTP_NAME[self.heroEntity:getMatchType()] then
                -- 技能增伤百分比
                table.insert(showAttrType, GConst.MATCH_SKILL_HURTP_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_21")
                txTitle:setText("<color=#EC80FF>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_SKILL_HURTP).."</color>")
            elseif curAttr[index].type == GConst.MATCH_CRIT_NAME[self.heroEntity:getMatchType()] then
                -- 暴击率百分比
                table.insert(showAttrType, GConst.MATCH_CRIT_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_22")
                txTitle:setText("<color=#FFF95F>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_CRIT).."</color>")
            elseif curAttr[index].type == GConst.MATCH_CRIT_TIME_NAME[self.heroEntity:getMatchType()] then
                -- 暴击伤害百分比
                table.insert(showAttrType, GConst.MATCH_CRIT_TIME_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_23")
                txTitle:setText("<color=#FF4848>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_CRIT_TIME).."</color>")
            elseif curAttr[index].type == GConst.MATCH_CURED_NAME[self.heroEntity:getMatchType()] then
                -- 治疗提升百分比
                table.insert(showAttrType, GConst.MATCH_CURED_NAME[self.heroEntity:getMatchType()])
                imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_24")
                txTitle:setText("<color=#85FF82>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_CURED).."</color>")
            else
                obj:setVisible(false)
            end
            txNum:setText(GFunc.getAttrShowValue(curAttr[index].type, curAttr[index].num))
        else
            obj:setVisible(false)
        end
    end
    if showAttr then
        self.txDesc1:setActive(true)
        self.txDesc2:setAnchoredPositionY(-561)
        self.scrollrectComp.baseObject:setAnchoredPositionY(-601)
        self.scrollrectComp.baseObject:setSizeDeltaY(170)
        self.scrollrectContent:setSizeDeltaY(170)
    else
        self.txDesc1:setActive(false)
        self.txDesc2:setAnchoredPositionY(-411)
        self.scrollrectComp.baseObject:setAnchoredPositionY(-441)
        self.scrollrectComp.baseObject:setSizeDeltaY(330)
        self.scrollrectContent:setSizeDeltaY(330)
    end

    self.spineSkin:getSkeletonGraphic().enabled = false
    self.spineSkin:loadAssetAsync(DataManager.SkinData:getModelId(self.selectSkinId), function()
        self.spineSkin:getSkeletonGraphic().enabled = true
        self.spineSkin:playAnim("idle", true, true, true)
    end, true)

    self.btnUse:setTouchEnable(true)
    if self.heroEntity:isActived() and DataManager.SkinData:isUnlock(self.selectSkinId) then
        -- 已解锁
        self.btnUse:setActive(true)
        self.txSource:setActive(false)
        if DataManager.SkinData:isActived(self.selectSkinId) then
            -- 已穿戴
            self.btnUse:setTouchEnable(false)
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
            self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.ON_USING_DESC))
        else
            -- 未穿戴
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
            self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_8))
        end
    else
        -- 未解锁
        self.btnUse:setActive(false)
        self.txSource:setActive(true)
        if self.heroEntity:isActived() then
            -- 未解锁皮肤
            self.txSource:setText(DataManager.SkinData:getSource(self.selectSkinId))
        else
            -- 未解锁英雄
            self.txSource:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_LOCKED))
        end
    end

    if self.onlyLook then -- 仅查看
        self.txDesc2:setVisible(false)
        self.scrollrectComp.baseObject:setVisible(false)
        self.bg:setSizeDeltaY(SIZE_DELTA_Y_LOOK)
        self.content:setSizeDeltaY(SIZE_DELTA_Y_LOOK)
    else
        self.txDesc2:setVisible(true)
        self.scrollrectComp.baseObject:setVisible(true)
        self.bg:setSizeDeltaY(SIZE_DELTA_Y_SKIN)
        self.content:setSizeDeltaY(SIZE_DELTA_Y_SKIN)
    end
end

return HeroSkinUI