dofile('data/Scripts/match3/panel/M3AbilityCounter.lua')
dofile('data/Scripts/match3/help/AboutMatch3Ability.lua')

-------------------------------------------------------------------------------
class 'M3AbilitySlot' (EventDispatcher)
-------------------------------------------------------------------------------

M3AbilitySlot.CLICKABLE_AREA_SIZE = 150
M3AbilitySlot.OPEN_X       = -75
M3AbilitySlot.CLOSE_X      = 260

M3AbilitySlot.APPEARING_TIME = 0.5
M3AbilitySlot.ENABLE_TIME    = 0.5
-------------------------------------------------------------------------------

function M3AbilitySlot:__init(id, maxCount)
    EventDispatcher.__init(self)
    
    self.id        = id
    self.anchor    = nil
    self.indicator = nil
    self.area      = nil
    self.counter   = M3AbilityCounter(maxCount)
    
    self.bVisible = false
    self.bEnable  = nil
    
    self.effectors = {}
    
    self:buildContent()
    self.counter:createDots(self.anchor, 1)
    self:setEnable(false, 0)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:destroy()
    self.counter:destroy()
    self.counter = nil
    
    EventDispatcher.destroy(self)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:buildContent()
    self:_createAnchor()
    self:_createBg()
    self:_createInfoButton()
    self:_createIndicator()
    self:_createClickableArea()
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getAnchor()
    return self.anchor
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getIndicator()
    return self.indicator
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getArea()
    return self.area
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getCount()
    return self.counter:getCount()
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getAbility()
    return self.id
end
-------------------------------------------------------------------------------

function M3AbilitySlot:getIndicatorTexture()
    return M3Game.TEXTURE_PATH..'Abilities/indicators/'..self.id..'.png'
end
-------------------------------------------------------------------------------

function M3AbilitySlot:show(time, delay)
    if not self.bVisible then
        time  = time or M3AbilitySlot.APPEARING_TIME
        delay = delay or 0
        
        self.bVisible = true
        self:_moveTo(M3AbilitySlot.OPEN_X, time, delay, true)
        
        local event = {
                        eventName = 'SlotShow',
                        slot = self,
                        time = time + delay
                      }
        self:dispatchEvent(event)
    end
end
-------------------------------------------------------------------------------

function M3AbilitySlot:hide(time, delay)
    if self.bVisible then
        time  = time or M3AbilitySlot.APPEARING_TIME
        delay = delay or 0
        
        self.bVisible = false
        self:_moveTo(M3AbilitySlot.CLOSE_X, time, delay, false)
        
        local event = {
                        eventName = 'SlotHide',
                        slot = self,
                        time = time + delay
                      }
        self:dispatchEvent(event)
    end
end

-------------------------------------------------------------------------------

function M3AbilitySlot:setEnable(bEnable, time)
    time = time or M3AbilitySlot.ENABLE_TIME
    
    local disableScale   = 0.8
    local disableOpacity = 0.3
    
    if self.bEnable ~= bEnable then
        self.bEnable = bEnable
        
        if self.effectors['enable'] then
            self.effectors['enable']:destroy()
            self.effectors['enable'] = nil
        end
        
        if time == 0 then
          --  self.bttn:SetEnabled(self.bEnable)
            
            if bEnable then
                self.indicator:setScale(1)
                self.indicator:setOpacity(1)
            else
                self.indicator:setScale(disableScale)
                self.indicator:setOpacity(disableOpacity)
            end
            
            self.area:setVisible(self.bEnable)
        else
            local eff = CreateAnimationEffector()
            eff:setDestroysOnEnd(false)
            eff:attachTo(self.indicator)
            
            eff:setKey(_TRACK_S, 0, self.indicator:getScale().x)
            eff:setKey(_TRACK_O, 0, self.indicator:getOpacity())
            
            if bEnable then
                eff:setKey(_TRACK_S, time, 1)
                eff:setKey(_TRACK_O, time, 1)
            else
                eff:setKey(_TRACK_S, time, disableScale)
                eff:setKey(_TRACK_O, time, disableOpacity)
            end
            
            eff:play()
            self.effectors['enable'] = eff
            
            if self.bEnable then
                current_scene:getPlayField().timer:add(function()
                                                self.area:setVisible(true)
                                            --    self.bttn:SetEnabled(self.bEnable)
                                            end,
                                            time)
            else
                self.area:setVisible(false)
           --     self.bttn:SetEnabled(self.bEnable)
            end
        end
        
        --self.area:SetVisible(self.bEnable)
    end
end
-------------------------------------------------------------------------------

function M3AbilitySlot:isVisible()
    return self.bVisible
end
-------------------------------------------------------------------------------

function M3AbilitySlot:setCount(count)
    self.counter:setCount(count)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:modifyCount(delta, bDontHideIfEmpty)
    local count = self.counter:getCount() + delta
    self.counter:setCount(count, 0.2)
    
    if count == 0 and not bDontHideIfEmpty then
        self:hide()
    end
end
-------------------------------------------------------------------------------

function M3AbilitySlot:OnButtonClick(e)
    local match3 = current_scene:getPlayField().match3
    if not match3.panel.bActive then
        return
    end

    AboutMatch3Ability(match3, self)
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- PRIVATE
-------------------------------------------------------------------------------

function M3AbilitySlot:_createAnchor()
    local objectName = 'm3abilitiesSlot'..self.id
 --    local anim = CreateAnimation(,0,0,8,8,1,0)
	-- anim:SetHotSpot(4,4)
	-- anim:SetBlendMode(2)
	-- anim:SetColor('0x00FFFFFF')
		
    local anim = CreateSpriteEntity('')
    anim:setAnimationSize(8, 8)
    anim:setHotSpot(4, 4)
    anim:setName(objectName)
    anim:setOrder(1)
    anim:setXY(M3AbilitySlot.CLOSE_X, 0)
    anim:setColor(Colorf(1, 1, 1, 0.01))
    anim:setOpacity(1)
	self.anchor = anim

	-- self.anchor = play_field:CreateObject(objectName,anim,0,0,1,0,1,1)
	-- self.anchor:SetOpacity(1)
	-- self.anchor:SetX(M3AbilitySlot.CLOSE_X)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_createBg()
    local texture    = M3Game.TEXTURE_PATH..'Abilities/slot_bg.png'
    local objectName = 'm3abilitySlotBg'..self.id
    
    -- local anim    = CreateAnimationByTextureName(texture)
    local anim = CreateSpriteEntity(texture)
    anim:setOrder(2)
	anim:attachTo(self.anchor)
	anim:setRespondable(false)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_createInfoButton()
    local x = 46
    local y = -55
    
    self.bttn = self:_createButton('bttn_m3_ability_info')
    self.bttn:attachTo(self.anchor)
    self.bttn:setOrder(100)
    self.bttn:setXY(x,y)  

    self.bttn.onPress:add(bind(self.OnButtonClick, self))    
    
    do
        local anim
        local objectName = 'area_m3abilitySlotBtnCover'..self.id
        
      -- anim = CreateAnimationByTextureName('data/Texture/Common/area.png')
        local anim = CreateSpriteEntity('')
        anim:setAnimationSize(8, 8)
        anim:setHotSpot(4, 4)       
        anim:setColor(Colorf(1, 0, 0, 0.01))   
        
        local scaleX = 32 / anim:getWidth()
        local scaleY = 32 / anim:getHeight()

        anim:setScaleX(scaleX)        
        anim:setScaleY(scaleY)        
        anim:setOrder(11) 
        anim:setName(objectName)       
        anim:attachTo(self.anchor)
        anim:setXY(x,y)
	end
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_createIndicator()
    local objectName = 'm3abilitySlotIndicator'..self.id
    local texture    = self:getIndicatorTexture()
    --local anim       = CreateAnimationByTextureName(texture)
    local anim = CreateSpriteEntity(texture)
    anim:setOrder(3)
    anim:setXY(0, 0)
    anim:setRespondable(false)
	self.indicator = anim
	self.indicator:attachTo(self.anchor)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_createClickableArea()
    local anim
    local objectName = 'area_m3abilitySlot'..self.id   

    local anim = CreateSpriteEntity('')
    anim:setAnimationSize(8, 8) 
    anim:setHotSpot(4, 4)
    anim:setName(objectName)
    anim:setOrder(10)
    anim:setXY(0, 0)
    anim:setColor(Colorf(1, 0, 0, 0.01))  
    
    local scaleX = M3AbilitySlot.CLICKABLE_AREA_SIZE / anim:getWidth()
    local scaleY = M3AbilitySlot.CLICKABLE_AREA_SIZE / anim:getHeight()

    anim:setScaleX(scaleX)
    anim:setScaleY(scaleY)
    
	self.area = anim
	self.area:attachTo(self.anchor)
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_moveTo(targetX, time, delay, bInertion)
    if self.effectors['move'] then
        self.effectors['move']:destroy()
        self.effectors['move'] = nil
    end
    
    if time == 0 then
        if delay == 0 then
            self.anchor:setX(targetX)
        else
            current_scene:getPlayField().timer:add(function()
                                            self.anchor:setX(targetX)
                                        end,
                                        delay)
        end
    else
        local startX = self.anchor:getX()
        local dx = targetX - startX
        
        local eff = CreateAnimationEffector()
        eff:setDestroysOnEnd(false)
        eff:attachTo(self.anchor)
        eff:setKey(_TRACK_X, 0,     startX)
        eff:setKey(_TRACK_X, delay, startX)
        
        if bInertion then
            eff:setKey(_TRACK_X, delay + time * 0.50, startX + dx * 1.2)
            eff:setKey(_TRACK_X, delay + time * 0.75, startX + dx * 0.9)
        end
        
        eff:setKey(_TRACK_X, delay + time, targetX)
        eff:play()
        
        self.effectors['move'] = eff
    end
end
-------------------------------------------------------------------------------

function M3AbilitySlot:_createButton(objectName, message, hotKey)   
    message = message or objectName
    local button = nil
    if not current_scene:getPlayField():hasEntityNamed('bttn_m3_ability_info') then
        local texture = M3Game.TEXTURE_PATH..'Abilities/buttons/bttn_info.png'    

        button = CreateButtonEntity(texture)
        button:setActiveRectSize(32, 32)
        button:setName('bttn_m3_ability_info')
        button:setOrder(1)
    end   
    
    -- if hotKey ~= nil then
    --     button:SetKeyEquivalent(hotKey)
    -- end
    
    return button
end
-------------------------------------------------------------------------------