-- mainPanel

local mp_btn_left = "mp_left"
local mp_btn_right = "mp_right"

main_panel.mode = "inv"

main_panel.center_part_pos_normal = 693
main_panel.center_part_pos_lowered = 810
main_panel.frames_to_hide = 30
main_panel.center_part_visible = true -- false - hided, true - visible

----------------------------------------------------------------------------------------------------
function main_panel:set_mode(mode) -- mode -- "ho", "inv"
	main_panel:get_element("main_panel_inventory"):setVisible(mode == "inv")
	main_panel:get_element("mainpanel"):setVisible(mode == "inv")
	main_panel:get_element("main_panel_hiddenobject"):setVisible(mode == "ho")
	main_panel.mode = mode
	
	if mode == "ho" then
		main_panel:set_center_visible(true)
	else
		main_panel:set_center_visible(false)
	end
	
end

----------------------------------------------------------------------------------------------------
function main_panel:set_ho_text(index, str)
	main_panel:get_element("mp_ho_"..index):setText(str, true)
end

----------------------------------------------------------------------------------------------------
function main_panel:activate_arrows(val)
	main_panel:get_element(mp_btn_left):setVisible(val)
	main_panel:get_element(mp_btn_right):setVisible(val)
end

----------------------------------------------------------------------------------------------------
function main_panel:clear()
	for i = 1, 12 do
		main_panel:set_ho_text(i, "")
	end
end

----------------------------------------------------------------------------------------------------
function main_panel:set_back_level(name)
	if name == nil then
		main_panel:set_element_eh("mp_back_btn", nil)
		main_panel:get_element("mp_back_btn"):setEventHandler(eiRollOver, "")
		main_panel:get_element("mp_back_btn"):setEventHandler(eiRollOut, "")
		mouse:setCursorFrame("default_cursor")		
	else		
		main_panel:set_element_eh("mp_back_btn", function() level_mgr:switch_to_level(name) end)
		main_panel:get_element("mp_back_btn"):setEventHandler(eiRollOver, "mouse:setCursorFrame(\"go_back\")")
		main_panel:get_element("mp_back_btn"):setEventHandler(eiRollOut, "mouse:setCursorFrame(\"default\")")
		mouse:setCursorFrame("default_cursor")
	end
end

----------------------------------------------------------------------------------------------------
function main_panel:set_center_visible(val)
	if self.center_part_visible == val then
		return
	end
	
	if self.mode == "inv" then
		main_panel:get_element("mp_back_btn"):setActive(not val)
		mouse:setCursorFrame("default")
	end
	
	if timer_mgr:is_timer_exist("main_panel_timer") then
		timer_mgr:add_timer(function() main_panel:set_center_visible(val) end, 0.5, 1, "main_panel_wait_timer")
		return
	end
	
	self.center_part_visible = val
	local pos = Vec2()
	
	pos = main_panel:get_element("main_panel_inventory"):getPos()
	
	if val == false then
		pos.y = main_panel.center_part_pos_normal
	else
		pos.y = main_panel.center_part_pos_lowered
	end
	
	main_panel:get_element("main_panel_inventory"):setPos(pos)
	
	local delta = main_panel.center_part_pos_lowered - main_panel.center_part_pos_normal
	
	if val == true then
		delta = -delta
	end
	
	timer_mgr:add_timer(
	function()
		
		local pos = main_panel:get_element("main_panel_inventory"):getPos()
		main_panel:get_element("main_panel_inventory"):setPos(Vec2(pos.x, pos.y + delta/ main_panel.frames_to_hide))
		
	end,
	0, main_panel.frames_to_hide, "main_panel_timer")
end

----------------------------------------------------------------------------------------------------
function main_panel_UPDATE()

	if main_panel.mode == "inv" then
		if mouse:getPos().y > 730 then
			main_panel:set_center_visible(true)
		elseif mouse:getPos().y < 570 then
			main_panel:set_center_visible(false)	
		end
	end

	return 0
end

----------------------------------------------------------------------------------------------------
function main_panel_INIT()
	main_panel:set_element_eh("mp_hint",	function() log_message("HINT!!!") end)
	main_panel:set_element_eh("mp_left",	function() inventory_:scroll(-1) end)
	main_panel:set_element_eh("mp_right",	function() inventory_:scroll(1) end)
	
	main_panel:set_mode("inv")
end