
CollapseChip = {}

CollapseChip.sPool = {}

CollapseChip.STATE_NORMAL = 0
CollapseChip.STATE_HLIGHTED = 1

CollapseChip.LAYERS = {'fish_color', 'color_bomb', 'bomb', 'shuffle', 'freeze', 'glow'}

----------------------------------------------------------------------------------------------------
function CollapseChip:new(aType, aSceneRoot)
    obj = {}
	setmetatable(obj, self)
	self.__index = self

    local tChip = nil
    obj.mLayers = {}
    for i = 1, #CollapseChip.sPool do
        if not CollapseChip.sPool[i].used then
            tChip = CollapseChip.sPool[i].chip
            CollapseChip.sPool[i].used = true
            table.foreach(CollapseChip.LAYERS,
                function(type)
                    obj.mLayers[CollapseChip.LAYERS[type]] = CollapseChip.sPool[i][CollapseChip.LAYERS[type]]
                end)
            break
        end
    end

    assert(tChip)
    if tChip == nil then
        return nil
    end

    obj.mType = aType
    obj.mState = CollapseChip.STATE_NORMAL
    tChip:setActive(true)
    play_clip(obj.mLayers['fish_color']:getCharacter(), {label_begin = aType, label_end = aType})
    table.foreach(obj.mLayers,
        function(type)
            if type ~= 'fish_color' then
                play_clip(obj.mLayers[type]:getCharacter(), {frame_begin = 0, frame_end = 0})
                --obj.mLayers[type]:setVisible(false)
            end
        end)
                
    obj.mChip = tChip
    obj.mBonus = nil
    obj.mTarget = nil
    obj.mSpeed = {x = 0.0, y = 0.0}
    obj.mMoveTime = 0.0

    obj.mSceneRoot = aSceneRoot
    
    self.mStopListener = nil

    return obj
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setBonus(aBonus)

    if aBonus == self.mBonus then
        return
    end

    table.foreach(self.mLayers,
        function(type)
            play_clip(self.mLayers[type]:getCharacter(), {frame_begin = 0, frame_end = 0})
            --self.mLayers[type]:setVisible(false)
        end)

    local hide_chip = (aBonus == 'color_bomb') or (aBonus == 'bomb')
    self.mLayers['fish_color']:setVisible(not hide_chip)
    if not hide_chip then
        play_clip(self.mLayers['fish_color']:getCharacter(), {label_begin = self.mType, label_end = self.mType})
    end

    if aBonus == 'color_bomb' then
        play_clip(self.mLayers['color_bomb']:getCharacter(), {label_begin = self.mType, label_end = self.mType})
    elseif aBonus ~= nil and self.mLayers[aBonus] ~= nil then
       --self.mLayers[aBonus]:setVisible(true)
       play_clip(self.mLayers[aBonus]:getCharacter(), {frame_begin = 1, frame_end = 1})
    end

    self.mBonus = aBonus 
end

----------------------------------------------------------------------------------------------------
function CollapseChip:destroy()

    --if self.mTarget ~= nil then
        --t = 0
    --end

    self:setRollOverListener(nil)
    self:setRollOutListener(nil)
    self:setStopListener(nil)

    table.foreach(self.mLayers,
        function(type)
            if type ~= 'fish_color' then
                --self.mLayers[type]:setVisible(false)
                play_clip(self.mLayers[type]:getCharacter(), {frame_begin = 0, frame_end = 0})
            end
        end)
    self.mLayers['fish_color']:setVisible(true)


    self.mChip:setActive(false)
    self.mTarget = nil

    for i = 1, #CollapseChip.sPool do
        if CollapseChip.sPool[i].chip == self.mChip then
            self.mChip:setVisible(false)
            CollapseChip.sPool[i].used = false
            break
        end
    end
end

----------------------------------------------------------------------------------------------------
function CollapseChip:isMoving()
    return (self.mTarget ~= nil)
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setPos(aX, aY)
    self.mChip:setPos(Vec2(aX, aY))
end

----------------------------------------------------------------------------------------------------
function CollapseChip:moveTo(aX, aY, aTime)
    self.mTarget = {x = aX, y = aY}

    local tPos = self.mChip:getPos()
    self.mSpeed.x = (aX - tPos.x) / aTime
    self.mSpeed.y = (aY - tPos.y) / aTime
    if self.mSpeed.x > 40 then
        i = 0
    end
    self.mMoveTime = aTime
    self.mChip:setActive(false)
end

----------------------------------------------------------------------------------------------------
function CollapseChip:update(aElapsed)
    if self.mTarget ~= nil then
        local tPos = self.mChip:getPos()
        self:setPos(tPos.x + self.mSpeed.x * aElapsed, tPos.y + self.mSpeed.y * aElapsed)

        self.mMoveTime = self.mMoveTime - aElapsed
        if self.mMoveTime <= 0.0 then
            self:setPos(self.mTarget.x, self.mTarget.y)
            self.mTarget = nil

            self.mChip:setActive(true)
            if self.mStopListener ~= nil then
                self.mStopListener(self)
            end
        end
    end
end

----------------------------------------------------------------------------------------------------
function CollapseChip:event(aChipName, aEventType)
    for i = 1, #CollapseChip.sPool do
        if CollapseChip.sPool[i].chip:getName() == aChipName then
            self.mChip:setVisible(false)
            CollapseChip.sPool[i].used = false
            break
        end
    end 
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setClickListener(aFunc)
    local tChip = self
    self.mSceneRoot:set_element_eh(self.mChip:getName(),
        function()
            if self.mTarget == nil then
                aFunc(tChip)
            end
        end)
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setRollOverListener(aFunc)
    self.mSceneRoot:set_element_eh(self.mChip:getName(), function() aFunc(self) end, {eh_type = eiRollOver})
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setRollOutListener(aFunc)
    self.mSceneRoot:set_element_eh(self.mChip:getName(), function() aFunc(self) end, {eh_type = eiRollOut})
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setStopListener(aFunc)
    self.mStopListener = aFunc
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setState(aState)
    if self.mState == aState then
        return
    end

    self.mState = aState

    local frame = 0
    if aState == CollapseChip.STATE_HLIGHTED then
        frame = 1
    end
    play_clip(self.mLayers['glow']:getCharacter(), {frame_begin = frame, frame_end = frame})
    --self.mLayers['glow']:setVisible(aState == CollapseChip.STATE_HLIGHTED)
end

----------------------------------------------------------------------------------------------------
function CollapseChip:getType()
    return self.mType
end

----------------------------------------------------------------------------------------------------
function CollapseChip:setVisible(state)
    self.mChip:setVisible(state)
end
