local SideBarBaseCellComp = class("SideBarBaseCellComp", BaseCell)

local DEFAULT_RED_POINT_POS_X = 30
local DEFAULT_RED_POINT_POS_Y = 30
local DEFAULT_RED_POINT_SCALE = 0.6

-- 需要继承重写的部分 ***********************************************************

-- 模块key值，对应ModuleManager.MODULE_KEY
function SideBarBaseCellComp:getModuleKey()
    return nil
end

function SideBarBaseCellComp:getHasPurchase()
    return true
end

function SideBarBaseCellComp:getIsOpen()
    return false
end

-- icon图标资源路径,为空时不显示
function SideBarBaseCellComp:getIconRes()
    -- return nil
    return "maincity_gm"
end

-- 图标动画资源路径,为空时不显示
function SideBarBaseCellComp:getSpineName()
    return nil
end

-- 图标动画资源默认名称
function SideBarBaseCellComp:getSpineAnimationName()
    return "idle"
end

-- 图标动画默认位置
function SideBarBaseCellComp:getSpinePositionY()
    return 0
end

-- 特效资源路径,为空时不显示
function SideBarBaseCellComp:getVfxRes()
    return nil
end

-- 是否有shake效果
function SideBarBaseCellComp:getHasShake()
    return false
end

function SideBarBaseCellComp:getIsShowRedPoint()
    return false
end

-- 红点偏移位置
function SideBarBaseCellComp:getRedPointPosition()
    return DEFAULT_RED_POINT_POS_X, DEFAULT_RED_POINT_POS_Y
end

-- 红点大小
function SideBarBaseCellComp:getRedPointScale()
    return DEFAULT_RED_POINT_SCALE
end

-- 广告红点偏移位置
function SideBarBaseCellComp:getAdPointPosition()
    return DEFAULT_RED_POINT_POS_X, DEFAULT_RED_POINT_POS_Y
end

-- 广告红点大小
function SideBarBaseCellComp:getAdPointScale()
    return DEFAULT_RED_POINT_SCALE
end

function SideBarBaseCellComp:refreshIcon()
    if not self:getIconRes() then
        self.icon:setVisible(false)
        return
    end
    self.icon:setVisible(true)
    self.icon:setSprite(GConst.ATLAS_PATH.MAIN, self:getIconRes())
end

function SideBarBaseCellComp:refreshSpine()
    -- if not self:getSpineName() then
    if true then
        self.spine:getSkeletonGraphic().enabled = false
        return
    end
    self.spine:setAnchoredPositionY(self:getSpinePositionY())
    if not self.isInitSpine then
        self.isInitSpine = true
        self.spine:loadAssetAsync(self:getSpineName(), function()
            self.spine:playAnim(self:getSpineAnimationName(), true, true, true)
            if self.isActive then
                self.spine:setActive(true)
            else
                self.spine:setActive(false)
            end
            if self.visible then
                self.spine:getSkeletonGraphic().enabled = true
            else
                self.spine:getSkeletonGraphic().enabled = false
            end
        end, false)
    else
        if not self.spine:getIsLoadingAsset() then
            self.spine:getSkeletonGraphic().enabled = true
        end
    end
end

function SideBarBaseCellComp:onClick()
end

function SideBarBaseCellComp:updateTime()
end

function SideBarBaseCellComp:onRefresh()
end

-- 常规逻辑 ***********************************************************

function SideBarBaseCellComp:init()
    local uiMap = self.baseObject:genAllChildren()
    self.icon = uiMap["side_bar_cell.icon"]
    self.spine = uiMap["side_bar_cell.ui_spine_obj"]
    self.descBg = uiMap["side_bar_cell.desc_bg"]
    self.descIcon = uiMap["side_bar_cell.desc_bg.icon"]
    self.descTx = uiMap["side_bar_cell.desc_bg.text"]
    self.timeBg = uiMap["side_bar_cell.time_bg"]
    self.timeTx = uiMap["side_bar_cell.time_bg.text"]
    self.descBg:setVisible(false)
    self.timeBg:setVisible(false)
    self.baseObject:addClickListener(function()
        self:onClick()
    end)
end

function SideBarBaseCellComp:refresh()
    self:refreshIcon()
    self:refreshSpine()
    if self:getHasShake() then
        self:doShake()
    else
        self:stopShake()
    end
    self:onRefresh()
end

function SideBarBaseCellComp:setAnchoredPositionY(y)
    self.baseObject:setAnchoredPositionY(y)
end

function SideBarBaseCellComp:setAnchoredPositionX(x)
    self.baseObject:setAnchoredPositionX(x)
end

function SideBarBaseCellComp:setVisible(visible)
    self.visible = visible
    self.baseObject:setVisible(visible)
    if not visible then
        self:stopShake()
    end
    if self.isInitSpine and self:getSpineName() and not self.spine:getIsLoadingAsset() then
        self.spine:getSkeletonGraphic().enabled = visible
    end
end

function SideBarBaseCellComp:checkIsOpen()
    -- 审核模式下大多数的带内购的侧边栏入口都不显示
    if self:getHasPurchase() and GFunc.isShenhe() then
        return false
    end
    return self:getIsOpen()
end

function SideBarBaseCellComp:getIsVisible()
    return self.visible
end

function SideBarBaseCellComp:getEntrancePos()
    if self:getIconRes() then
        return self.icon:getPosition()
    elseif self:getSpineName() then
        return self.spine:getPosition()
    end
    return nil
end

function SideBarBaseCellComp:getCellPath()
    return self.cellPath
end

function SideBarBaseCellComp:showRedPoint()
    if self.redPoint then
        return
    end
    self.redPoint = true
    local x, y = self:getRedPointPosition()
    self.baseObject:addRedPoint(x, y, self:getRedPointScale())
end

function SideBarBaseCellComp:hideRedPoint()
    if self.redPoint == false then
        return
    end
    self.redPoint = false
    self.baseObject:removeRedPoint()
end

function SideBarBaseCellComp:initWithParentUI(parentUI, cellPath, isLeftSide)
    self.parentUI = parentUI
    self.cellPath = cellPath
    self.isLeftSide = isLeftSide
end

function SideBarBaseCellComp:closeBtn()
    if self.parentUI then
        self.parentUI:closeSideBarBtn(self.isLeftSide)
    end
end

function SideBarBaseCellComp:setActive(isActive)
    if self.isActive == isActive then
        return
    end
    self.isActive = isActive
    if self.isInitSpine and self:getSpineName() and not self.spine:getIsLoadingAsset() then
        self.spine:setActive(isActive)
    end
end

function SideBarBaseCellComp:getIsActive()
    return self.isActive
end

function SideBarBaseCellComp:doShake()
    if self.shakeSeq then
        self.shakeSeq:Restart()
        return
    end
    self.shakeSeq = self.baseObject:createBindTweenSequence()
	self.shakeSeq:AppendInterval(2)
    local transform = self.baseObject:getTransform()
	self.shakeSeq:Append(transform:DOScale(1.12, 0.1))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.11))
	self.shakeSeq:Append(transform:DOScale(1.1, 0.15))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.2))
	self.shakeSeq:Append(transform:DOScale(1.05, 0.25))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.3))
	self.shakeSeq:Append(transform:DOScale(1, 0.3))
    self.shakeSeq:AppendInterval(0.3)
	self.shakeSeq:SetLoops(-1)
end

function SideBarBaseCellComp:stopShake()
    if self.shakeSeq then
        self.shakeSeq:Pause()
    end
end

function SideBarBaseCellComp:onDestroy()
    if self.shakeSeq then
        self.shakeSeq:Kill()
        self.shakeSeq = nil
    end
end

return SideBarBaseCellComp