local CollectionCell = class("CollectionCell", BaseCell)

function CollectionCell:init()
    local uiMap = self:getUIMap()
    self.imgQlt = uiMap["collection_cell.img_qlt"]
    self.imgType = uiMap["collection_cell.img_qlt.img_type"]
    self.imgS = uiMap["collection_cell.img_s"]
    self.txLevel = uiMap["collection_cell.tx_level"]
    self.txName = uiMap["collection_cell.tx_name"]
    self.txMax = uiMap["collection_cell.tx_max"]
    self.btnCollect = uiMap["collection_cell.btn_collect"]
    self.imgIcon = uiMap["collection_cell.btn_collect.img_icon"]
    self.txValue = uiMap["collection_cell.btn_collect.tx_value"]
    self.spineRoot = uiMap["collection_cell.spine_root.spine_hero"]

    self.btnCollect:addClickListener(function()
        if self.collectionType == GConst.CollectionConst.TYPE.HERO then
            self:onClickCollectionHero()
        elseif self.collectionType == GConst.CollectionConst.TYPE.SKIN then
            self:onClickCollectionSkin()
        end
    end)
    self:bind(DataManager.CollectionData, "isDirty", function()
        self:refresh()
    end)
end

function CollectionCell:onClickCollectionHero()
    if DataManager.CollectionData:canCollectPoint(GConst.CollectionConst.TYPE.HERO, self.collectionData.id) then
        AudioManager:playEffect(AudioManager.EFFECT_ID.STAR_GET)
        ModuleManager.CollectionManager:reqHeroPoint(self.collectionData.id)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.COLLECTION_CLICK_GET_POINT, self.imgIcon:getPosition())
    else
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_12))
    end
end

function CollectionCell:onClickCollectionSkin()
    if DataManager.SkinData:isUnlock(self.collectionData.id) and DataManager.CollectionData:canCollectPoint(GConst.CollectionConst.TYPE.SKIN, self.collectionData.id) then
        ModuleManager.CollectionManager:reqSkinPoint(self.collectionData.id)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.COLLECTION_CLICK_GET_POINT, self.imgIcon:getPosition())
    else
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.SKIN_GOT_TIPS))
    end
end

function CollectionCell:refresh(type, data)
    self.collectionType = type or self.collectionType
    self.collectionData = data or self.collectionData

    if self.collectionType == GConst.CollectionConst.TYPE.HERO then
        self:showHero()
    elseif self.collectionType == GConst.CollectionConst.TYPE.SKIN then
        self:showSkin()
    end
end

-- 展示英雄收集
function CollectionCell:showHero()
    local heroEntity = DataManager.HeroData:getHeroById(self.collectionData.id)

    SpineManager:loadHeroAsync(heroEntity:getModelId(), self.spineRoot, function(spineObject)
        if self.spineHero then
            self.spineHero:destroy()
            self.spineHero = nil
        end
        spineObject:setDefaultMix(0)
        self.spineHero = spineObject
    end)
    self.imgS:setActive(false)
    self.txLevel:setActive(true)
    self.baseObject:setSprite(GConst.ATLAS_PATH.HERO, GConst.HERO_FRAME_QLT[self.collectionData.qlt])
    self.imgQlt:setSprite(GConst.ATLAS_PATH.HERO, GConst.HERO_FRAME_BG_QLT[self.collectionData.qlt])
    self.imgType:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[self.collectionData.position], function()
        self.imgType:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):SetNativeSize()
    end)
    self.txName:setText(ModuleManager.HeroManager:getHeroName(self.collectionData.id))
    self.txLevel:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, heroEntity:getLv()))
    self.txMax:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_2))

    -- 刷新点数状态
    local canGetValue = DataManager.CollectionData:getCanCollectPoint(GConst.CollectionConst.TYPE.HERO, self.collectionData.id)
    -- Logger.logHighlight("id:"..self.collectionData.id..",level:"..heroEntity:getLv()..",canGetValue:"..canGetValue)
    if DataManager.CollectionData:canCollectPoint(GConst.CollectionConst.TYPE.HERO, self.collectionData.id) then
        self.txValue:setText("+" .. canGetValue)
        self.txLevel:setText("<color=#FCFF13>"..I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, heroEntity:getLv()).."</color>")
        GFunc.getShakeSeq(self.txLevel, false, 1, true)
        self.btnCollect:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_3")
        self.btnCollect:setActive(true)
        self.txMax:setActive(false)
    else
        self.txValue:setText("+" .. canGetValue)
        self.txLevel:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, heroEntity:getLv()))
        GFunc.getShakeSeq(self.txLevel, true)
        self.btnCollect:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
        self.btnCollect:setActive(not heroEntity:isMaxLv())
        self.txMax:setActive(heroEntity:isMaxLv())
    end
    GFunc.centerTxAndImg(self.txValue, self.imgIcon, 2)
end

-- 展示皮肤收集
function CollectionCell:showSkin()
    SpineManager:loadHeroAsync(DataManager.SkinData:getModelId(self.collectionData.id), self.spineRoot, function(spineObject)
        if self.spineHero then
            self.spineHero:destroy()
            self.spineHero = nil
        end
        spineObject:setDefaultMix(0)
        self.spineHero = spineObject
    end)
    self.imgS:setActive(self.collectionData.qlt >= 4)
    self.txLevel:setActive(false)
    self.baseObject:setSprite(GConst.ATLAS_PATH.HERO, "hero_skin_frame_"..self.collectionData.qlt)
    self.imgQlt:setSprite(GConst.ATLAS_PATH.HERO, GConst.HERO_FRAME_BG_QLT[self.collectionData.qlt])
    self.imgType:setSprite(GConst.ATLAS_PATH.HERO, "hero_skin_"..self.collectionData.qlt, function()
        self.imgType:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):SetNativeSize()
    end)
    self.txName:setText(DataManager.SkinData:getName(self.collectionData.id))
    self.txMax:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))

    -- 刷新点数状态
    local canGetValue = DataManager.CollectionData:getCanCollectPoint(GConst.CollectionConst.TYPE.SKIN, self.collectionData.id)
    -- Logger.logHighlight("id:"..self.collectionData.id..",canGetValue:"..canGetValue)
    if DataManager.CollectionData:canCollectPoint(GConst.CollectionConst.TYPE.SKIN, self.collectionData.id) then
        -- 已解锁，未领点数
        self.txMax:setActive(false)
        self.btnCollect:setActive(true)
        self.btnCollect:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_3")
        self.txValue:setText("+" .. canGetValue)
    else
        if DataManager.SkinData:isUnlock(self.collectionData.id) then
            -- 已解锁，已领点数
            self.btnCollect:setActive(false)
            self.txMax:setActive(true)
        else
            -- 未解锁
            self.txMax:setActive(false)
            self.btnCollect:setActive(true)
            self.btnCollect:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
            self.txValue:setText("+" .. canGetValue)
        end
    end
    GFunc.centerTxAndImg(self.txValue, self.imgIcon, 2)
end

return CollectionCell