----------------------------------------------------------------------------------------------------
FlyPath = {}

FlyPath.AMPLITUDE = 0.22
FlyPath.SPEED = 400 -- пикселей / сек
FlyPath.FINAL_SCALE = 0.5
FlyPath.CLICK_SCALE = 1.3
FlyPath.CLICK_SCALE_TIME = 0.4
FlyPath.EFFECT_FREQUENCY = 25 -- в секунду

FlyPath.NUM_STAGES = 2

----------------------------------------------------------------------------------------------------
function FlyPath:new(aElem, aStart, aFinish, aCallback)
	obj = {}
	setmetatable(obj, self)
	self.__index = self

	obj.mElement = aElem

    local tLx = aFinish.x - aStart.x
	local tLy = aFinish.y - aStart.y
	
	obj.fdx = (1 - tLy / 740.0) * 0.5

    obj.mStart = matrix {{aStart.x}, {aStart.y}, {1}}
	obj.mFinish = matrix {{aFinish.x}, {aFinish.y}, {1}}

    obj.stage = 1
--[[
    obj.mScale = matrix {{tLx, 0.0, 0.0},
                         {0.0, tLy, 0.0},
                         {0.0, 0.0, 1.0}}

    obj.mShift = matrix {{1.0, 0.0,  aStart.x},
                         {0.0, 1.0, aFinish.y},
                         {0.0, 0.0,       1.0}}
]]

    obj.mScale = matrix {{tLx, 0.0, 0.0},
                         {0.0, -tLy, 0.0},
                         {0.0, 0.0, 1.0}}

    obj.mShift = matrix {{1.0, 0.0,  aStart.x},
                         {0.0, 1.0, aFinish.y},
                         {0.0, 0.0,       1.0}}

    obj.mTime = 0.0
	obj.mFlyTime = {1.0, 1.0}
	obj.mLastEffectTime = 0.0

    obj.mInitElemScale = 1.0
    obj.mInitElemAngle = 0.0

    if aElem ~= nil then
        obj.mInitElemScale = aElem:getScale()
        obj.mInitElemAngle = aElem:getAngle()
    end

    obj.mCallback = aCallback;
    --Panel.p_layer:linkEffect('Item_Appear', aElem:getCharacter())
    Panel.p_layer:linkEffect('hid_fly', aElem:getCharacter())

	return obj
end


function FlyPath:getOrd(aAbsc)
    local p1 = (aAbsc - self.fdx) / (1.0 - self.fdx)
    return 1.0 - p1 * p1
end

----------------------------------------------------------------------------------------------------
function FlyPath:term()
    
    if self.mCallback ~= nil then
        self.mCallback()
    end
    --Panel:unlink_effect(aElem:getName())
end

----------------------------------------------------------------------------------------------------
function FlyPath:update(aElapsed)

    if self.mElement == nil then
        return true
    end

    self.mTime = self.mTime + aElapsed

    local phase = self.mTime / self.mFlyTime[self.stage]

    if phase > 1.0 then
        self.stage = self.stage + 1
        self.mTime = 0.0
        phase = 0.0
        if self.stage == FlyPath.NUM_STAGES + 1 then
            Panel.p_layer:stopLinkedEffect(self.mElement:getCharacter())
            if self.mCallback ~= nil then
                self.mCallback()
            end
            return true
        end
    end

    if self.stage == 1 then
        local elemScale = math.sin(3 * math.pi * phase) / 5.0 + 1.0
        self.mElement:setScale(elemScale)

    elseif self.stage == 2 then
        local tPos = matrix {{phase}, {self:getOrd(phase) / self:getOrd(0.0)}, {1}}
        local tResultPoint = self.mShift:mul(self.mScale:mul(tPos))
        self.mElement:setPos(Vec2(tResultPoint[1][1], tResultPoint[2][1]))

        --if self.mTime > self.mLastEffectTime + 1.0 / FlyPath.EFFECT_FREQUENCY then
          --  self.mLastEffectTime = self.mTime
            
            --start_effect_at("Item_Appear", self.mElement:getPos(true))
        --end

        local elemScale = (FlyPath.FINAL_SCALE + (1.0 - phase) * (1.0 - FlyPath.FINAL_SCALE))
        self.mElement:setScale(elemScale)
        
        local elemAngle = 0.5 * math.pi * phase + self.mInitElemAngle
        self.mElement:setAngle(elemAngle)
    end
    return false
end