-------------------------------------------------------------------------------
class 'FishManager'
-------------------------------------------------------------------------------
local SCALE_FOUND          = 'SCALE_FOUND'
local SCALE_APPLIED        = 'SCALE_APPLIED'

-------------------------------------------------------------------------------
function FishManager:__init()
    local vecTools = ParseDirectory(PATH_SCALES)
    
    self.tools = {}
    
    -- make fly animation both for tools and seeds -----------------------------
	for i = 0, vecTools:size() - 1 do
		local name = string.sub(ExtractFileName(vecTools:get(i)), 7, -1)
		
		local tool = {}
		tool.name = name
		tool.create_fly_object = function(name)
			local path = PATH_SCALES..'scale_'..name..'.png'
			local anim  = CreateAnimationByTextureName(path)
			local obj   = play_field:CreateObject(name..'_Flying',anim,512,384,300,0,1,1)
			
			return obj
		end
		
		self.tools[name] = tool
	end
end
-------------------------------------------------------------------------------
function FishManager:IsScaleFound(name)
    return HasGameState(SCALE_FOUND, name)
end
-------------------------------------------------------------------------------
function FishManager:FindScale(name)
    AddUniqueGameState(SCALE_FOUND, name)
end
-------------------------------------------------------------------------------
function FishManager:IsScaleApplied(name)
    return HasGameState(SCALE_APPLIED, name)
end
-------------------------------------------------------------------------------
function FishManager:ApplyScale(name)
    AddUniqueGameState(SCALE_APPLIED, name)
end
-------------------------------------------------------------------------------
function FishManager:GetInfo(name)
    return self.tools[name]
end
-------------------------------------------------------------------------------
function FishManager:_fly_object(scale_name, target_name)
    local M_E		 = 2.718281828459045235360
    
    local manager = GetCurrentLevel():GetFishManager()
    local fly_object = manager:GetInfo(scale_name).create_fly_object(scale_name)
    local target     = Object(target_name)
    
    fly_object:AttachTo( play_field )
    fly_object:SetZ( play_field:GetMaxObjectZ() + 1 )
    fly_object:SetLayer(GetCurrentLevel():GetPanel():GetLayer() + 5)
    
    local level  = GetCurrentLevel()
	level.mouseBlocker:Push()
	local freeztime = 2
	local flytime = .4
	local start_scale = 1.2
	local mouse_pos = GetMousePos()
	fly_object:SetVisible(true)
	
	local dest_x = 120
	local dest_y = 670
	
	
	local eff = CreateAnimationEffector()
	eff:SetDestroysOnEnd(true)
    eff:AttachTo(fly_object)
	
	eff:SetKey(_TRACK_X, freeztime, SCR_W / 2)
	eff:SetKey(_TRACK_Y, freeztime, SCR_H / 2)
	eff:SetKey(_TRACK_X, freeztime+flytime, dest_x)
	eff:SetKey(_TRACK_Y, freeztime+flytime, dest_y)
	
	eff:SetKey(_TRACK_SX, freeztime, start_scale)
	eff:SetKey(_TRACK_SY, freeztime, start_scale)
	eff:SetKey(_TRACK_SX, freeztime+flytime, 0)
	eff:SetKey(_TRACK_SY, freeztime+flytime, 0)
	
	level.timer:Add(function()
							fly_object:SetVisible(false)
						end, freeztime+flytime)
	eff:Play()
		
    level.timer:Add(function()
                    level.mouseBlocker:Pop()
                    end, freeztime+flytime)
                    
    ShowBlackBar('SCALE_MESSAGE')                 
end
-------------------------------------------------------------------------------
-- Регистрирует тулзу чешуйки для нахождения
function RegisterScaleObject(scale_name, target_name, on_found)
    local level     = GetCurrentLevel()
    local manager   = level:GetFishManager()
    local state = GM:GetCurrPlayer().gameState
    
    if manager:IsScaleFound(scale_name) then
        if on_found then
            local event  = {}
            event.save   = true
            event.name   = name
            event.target = target_name
        
            on_found(event)
        end
        manager:FindScale(scale_name)
    else
		 local function on_find(event)
			manager:_fly_object(scale_name, target_name)
			manager:FindScale(scale_name)
			level.timer:Add(function()
								level:GetPanelLuaObject():OnFindScale()
							end, 1.4)
							
			if on_found then
				local event  = {}
				event.save   = false
				event.name   = name
				event.target = target_name
			
				on_found(event)
			end
			ObjectVisible(target_name, false)
		end
		
		local level_name = GetCurrentLevel().name
		
		local state_value = level_name..'('..target_name..')'
		if HasGameState(OBJECT_CLICKED, state_value) then
			RemoveGameState(OBJECT_CLICKED, state_value)
		end
		
		RegisterActiveObject(target_name, on_find, false)
    end
end
-------------------------------------------------------------------------------

function FindScale(scale_name)
    local level     = GetCurrentLevel()
    local manager   = level:GetFishManager()
    
    return manager:FindScale(scale_name)
end
-------------------------------------------------------------------------------

function ApplyScale(scale_name)
    local level     = GetCurrentLevel()
    local manager   = level:GetFishManager()
    
    return manager:ApplyScale(scale_name)
end
-------------------------------------------------------------------------------

function IsScaleFound(scale_name)
    local level     = GetCurrentLevel()
    local manager   = level:GetFishManager()
    
    return manager:IsScaleFound(scale_name)
end
-------------------------------------------------------------------------------

function IsScaleApplied(plant_name)
    local level     = GetCurrentLevel()
    local manager   = level:GetFishManager()
    
    return manager:IsScaleApplied(plant_name)
end
-------------------------------------------------------------------------------