panel = nil

function PanelOnSceneLoad(screen)
    panel = screen
    LoadData(screen)
	screen:GetObject("smolder"):SetVisible(false)
	--[[assert(screen:HasObject("ButtonPauseUp"))
	
    local up = screen:GetObject("ButtonPauseUp"):GetAnimationPtr()
	screen:GetObject("ButtonPauseUp"):SetVisible(false)
	local over = screen:GetObject("ButtonPauseOver"):GetAnimationPtr()
	screen:GetObject("ButtonPauseOver"):SetVisible(false)
	local pause_button = CreateButton(up, over, over, "Pause", screen)
	pause_button:AttachTo(screen)
	pause_button:SetXY(41, 693)
	pause_button:SetZ(111)
	
	local up = screen:GetObject("ButtonHelpUp"):GetAnimationPtr()
	screen:GetObject("ButtonHelpUp"):SetVisible(false)
	local over = screen:GetObject("ButtonHelpOver"):GetAnimationPtr()
	screen:GetObject("ButtonHelpOver"):SetVisible(false)
	local help_button = CreateButton(up, over, over, "Help", screen)
	help_button:AttachTo(screen)
	help_button:SetXY(59, 673)
	help_button:SetZ(111)]]
	
	local up = screen:GetObject("PanelMenuButtonUp"):GetAnimationPtr()
	screen:GetObject("PanelMenuButtonUp"):SetVisible(false)	
	local down = screen:GetObject("PanelMenuButtonDown"):GetAnimationPtr()
	screen:GetObject("PanelMenuButtonDown"):SetVisible(false)
    panel.menu_button = CreateButton(up, up, down, "Exit", screen)
    local menu_button = panel.menu_button
	menu_button:AttachTo(screen)
	menu_button:SetXY(72, 65)
	menu_button:SetZ(111)
	menu_button:SetHotKey(27)
	
	screen:GetObject('Blick'):SetVisible(false)
	screen:GetObject('BlickBig'):SetVisible(false)
	--[[local caption = menu_button:GetCaption()
	caption:SetText(GetString("MENU"));
	caption:SetFont("Comics");
	caption:SetZ(10)
	caption:SetX(-10)
	caption:SetY(-7)
	caption:SetTracking(-11)
	caption:SetAngle(-0.03)
	caption:SetScale(0.9)]]
	
	local up = screen:GetObject("CigarUp"):GetAnimationPtr()
	screen:GetObject("CigarUp"):SetVisible(false)
	local over = screen:GetObject("CigarOver"):GetAnimationPtr()
	screen:GetObject("CigarOver"):SetVisible(false)
	cigar_button = CreateButton(up, over, over, "Hint", screen)
	cigar_button:AttachTo(screen)
	cigar_button:SetXY(109, 591)
	cigar_button:SetZ(108)	
	
    TimerDoString("OpenNotebook()", 2,  panel:GetTimer())	
	--GTimerDoString("OpenNotebook()", 2);
	panel:GetObject("Cover"):GetAnimation():Stop()
	panel:GetObject("Cover"):GetAnimation():SetFrame(6);
	
	local af = CreateAnimationEffector()
	local obj = screen:GetObject("BlickBig")
	local anim_len = GetAnimationLength(obj:GetAnimation())
	af:SetMode(_ANIM_LOOP)
	af:AttachTo(obj)
	af:SetKey(_TRACK_O, 0, 1)
	af:SetKey(_TRACK_O, anim_len, 1)
	af:SetKey(_TRACK_O, anim_len + 0.1, 0)
	af:SetKey(_TRACK_O, 3 * anim_len, 0)
	af:Play()
	
	--[[local te = CreateTextEntity("Calisto_MT_31", "HINT")
	te:SetXY(70, 557)
	te:SetZ(1000)
	te:SetScale(0.5)
	te:SetTextAlignment(HGETEXT_TOP + HGETEXT_LEFT)
	--te:AttachTo(comics.objects[bubble])
	te:AttachTo(panel)]]
	
	--te:SetOpacity(0)
	
	local up = screen:GetObject("CigarBoxUp"):GetAnimationPtr()
	screen:GetObject("CigarBoxUp"):SetVisible(false)	
	local down = screen:GetObject("CigarBoxUp"):GetAnimationPtr()
	screen:GetObject("CigarBoxUp"):SetVisible(false)
	panel.cigarBoxButton = CreateButton(up, up, down, "CigarBox", screen)
	panel.cigarBoxButton:AttachTo(screen)
	local obj = screen:GetObject("CigarBoxUp")
	panel.cigarBoxButton:SetXY(obj:GetX(), obj:GetY())
	panel.cigarBoxButton:SetZ(0)

	--local caption = cigarBoxButton:GetCaption()
	
	local GetCigarsFound = function()
		local result = 0
		for i, v in ipairs(GetProfileManager():GetCurrentProfile().cigars_found) do
			if v then
				result = result + 1
			end
		end
		return result
	end
	
	panel.RefreshMetaGameCaption = function()
	    local cigarsFound = GetCigarsFound()
	    panel.cigarBoxButton:GetCaption():SetText(tostring(cigarsFound).."/"..tostring(GetLevelsCount()).." "..GetString("CIGARS_LEFT"));	
	end
	
	panel.cigarBoxButton:GetCaption():SetFont("TimesNewRoman13")
	panel.RefreshMetaGameCaption()
	panel.cigarBoxButton:GetCaption():SetColor("0xFFb7850e")
	panel.cigarBoxButton:GetCaption():SetY(-5)
	
	panel.BlickCigarBoxButton = function()
        panel.blicking_effector = CreateAnimationEffector()
        local t = 0.25
        panel.blicking_effector:AttachTo(panel.glow)
        --panel.blicking_effector:SetMode(_ANIM_LOOP)
        panel.blicking_effector:SetKey(_TRACK_O, 0, 1)
        panel.blicking_effector:SetKey(_TRACK_O, 0.5 * t, 0)        
		panel.blicking_effector:SetKey(_TRACK_O, 1 * t, 1)
        panel.blicking_effector:SetKey(_TRACK_O, 1.5 * t, 0)        
		panel.blicking_effector:SetKey(_TRACK_O, 2 * t, 1)
        panel.blicking_effector:SetKey(_TRACK_O, 2.5 * t, 0)        
		panel.blicking_effector:SetKey(_TRACK_O, 3 * t, 1)
        panel.blicking_effector:SetKey(_TRACK_O, 3.5 * t, 0)        
        panel.blicking_effector:Play()
    end

    panel.StopBlickingCigarBoxButton = function()
        if panel.blicking_effector then
            panel.blicking_effector:Destroy()
            panel.glow:SetOpacity(0)
            panel.blicking_effector = nil
        end
    end
	
	panel.glow = panel:GetObject('Glow')
	panel.glow:SetOpacity(0)
end


function OnUpdate(dt)
end

function OnExit()
end

function OnPanelDestroy()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)   
end


function OnObjectMouseUp(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
end

function OnObjectReachPanel(object)
end

function HintTimeEnded()
    cigar_button:SetVisible(true);
	cigar_button:Go();
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function PanelOnCigarClicked()
	panel:SetHintDelay(60)
	--DEBUG
	--panel:SetHintDelay(2)
	cigar_button:Stop()
	cigar_button:SetVisible(false)
	play_field:ResetWrongClicksCount();
end

function PanelButtonPress(title)
	if title == "CigarBox" then
		panel.StopBlickingCigarBoxButton()
		panel.cigarBoxButton:GetCaption():SetColor('0xFFB7850E')
		if tutorial_meta_game and tutorial_meta_game.IsVisible() and play_field:GetObject('t_w_tutorial_meta_game'):GetOpacity() == 1 then            
            tutorial_meta_game.Hide()
            TimerDo(play_field:GetTimer(), 0.6, load_screen, 'data/Lua/Screens/MetaGameScreen.lua')
        elseif not game_state.is_tutorial_must_be_shown then
            load_screen('data/Lua/Screens/MetaGameScreen.lua')
        end
	end
	if title == "Exit" then
	    local currentProfile = GetProfileManager():GetCurrentProfile()
        currentProfile.hint_delay_ = panel:GetHintDelay()
        load_screen('data/Lua/Screens/OptionsInGame.lua');
		panel:GetObject('Blick'):SetVisible(false)
    end
end

function PanelButtonEnter(title)
	if title == "Exit" then	    
		panel:GetObject('Blick'):SetVisible(true);  
		panel:GetObject('BlickBig'):SetVisible(true);
    end
    if title == "CigarBox" then
        panel.cigarBoxButton:GetCaption():SetColor('0xFFDBC25E')
    end
end

function PanelButtonLeave(title)
	if title == "Exit" then	    
		panel:GetObject('Blick'):SetVisible(false)  
		panel:GetObject('BlickBig'):SetVisible(false);
    end
    if title == "CigarBox" then
        panel.cigarBoxButton:GetCaption():SetColor('0xFFB7850E')
    end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Shell.png',0,0,86,64,1,0)
	anim:SetHotSpot(43,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Shell',anim,90,590,110,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Ashes.png',0,0,201,23,1,0)
	anim:SetHotSpot(100,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ashes',anim,112,590,104,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Smolder.png',0,0,60,26,10,10)
	anim:SetHotSpot(30,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('smolder',anim,389,536,106,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Blick.png',0,0,86,18,11,10)
	anim:SetHotSpot(43,9)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Blick',anim,71,109,112,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/BlickBig.png',0,0,130,119,25,10)
	anim:SetHotSpot(65,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BlickBig',anim,79,64,105,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/CigarUp.png',0,0,206,22,1,0)
	anim:SetHotSpot(103,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CigarUp',anim,110,591,106,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/CigarOver.png',0,0,216,32,1,0)
	anim:SetHotSpot(108,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CigarOver',anim,110,590,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/PanelMenuButtonDown.png',0,0,116,114,1,0)
	anim:SetHotSpot(58,57)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('PanelMenuButtonDown',anim,73,65,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/PanelMenuButtonUp.png',0,0,116,114,1,0)
	anim:SetHotSpot(58,57)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('PanelMenuButtonUp',anim,72,65,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/HintCaption.png',0,0,83,17,1,0)
	anim:SetHotSpot(41,8)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('HintCaption',anim,90,594,119,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Cover.dds',0,0,235,572,6,8)
	anim:SetHotSpot(118,286)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Cover',anim,103,342,150,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Panel/Panel.png',0,0,227,766,1,0)
	anim:SetHotSpot(113,383)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('PanelBackground',anim,96,384,1,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/MetaGame/CigarBoxUp.png',0,0,175,50,1,0)
	anim:SetHotSpot(87,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CigarBoxUp',anim,125,743,0,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/MetaGame/Glow.png',0,0,175,50,1,0)
	anim:SetHotSpot(87,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Glow',anim,125,743,2,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
