
function meu:define_ui()
	local ref = self.ref
	local ui = self.ui
	local bu
	local par

	local ix = 1
	local iy = 1
	local SY = 1
	local DY = .2

	self:add_camera()

	iy = 1
	bu = self:add_selector(	{ix,iy,		8,SY},	"Env"	)
	:set_item_text( 1, "Test", "Maa", "MDL", "VivaIssy", "AAATuto", "Valencia" )
	ui.bu_sel = bu
	iy = iy + SY + DY

	bu = self:add_text_info({ix,iy,			8,SY},	"File"		)
		:set_text()
		:set_text_align_x( "center" )
		ui.bu_filename = bu
	iy = iy + SY + DY

	self:add_button(		{ix,iy,			4,SY},	"Slide", 	self, "b_slide", true	)
	iy = iy + SY
	bu = self:add_text_info({ix,iy,			8,SY},	"Id"		)
		:set_text()
		:set_text_align_x( "center" )
		ui.bu_id = bu
	iy = iy + SY + DY
	self:add_trig_method(	{ix,iy,			4,SY*2},	"Prev", 	self, "inc_slide", -1	)
	self:add_trig_method(	{ix+4,iy,		4,SY*2},	"Next", 	self, "inc_slide", 1	)
	iy = iy + SY*2 + DY

	self:add_button(		{ix,iy,			4,SY},	"Grid", 	self, "b_grid", true	)
	self:add_button(		{ix,iy+SY,		4,SY},	"boid", 	self, "b_boid", true	)
	iy = iy + SY*2 + DY

	ix,iy = 9,3
	self:add_button(		{ix,iy,			SY,SY},	"Logo", 		self, "b_logo",		true	)
	self:add_slider(		{ix+1,iy+SY,	3,SY},	"Logo.Size" , 	self, "logo_size",	.5,	0,4	)
	self:add_slider(		{ix+1,iy+SY*2,	3,SY},	"Logo.U" , 		self, "logo_u",		3.5,	-5,5	)
		:set_color_back("u")
	self:add_slider(		{ix+1,iy+SY*3,	3,SY},	"Logo.V" , 		self, "logo_v",		1.75,	-3,3	)
		:set_color_back("v")
	iy = iy + SY * 3 + DY
end

function meu:load_media()
	local dir = "AAAPowow/"
	if not self.imgs then
		MEDIA.push_dir_media( dir )
		--MEDIA.set_dir_media( dir )
			local t = {}
--			for i=1,2 do
--				t[i] = IMGS.get_img_with_error_dialog( "Sitem_"..i..".jpg" )
--			end
			t.hand    		= IMGS.get_img_with_error_dialog( "AAASeed_Hand.png" )
			t.artcast 		= IMGS.get_img_with_error_dialog( "ArtCast4D_logo.png" )
			t.logo    		= IMGS.get_img_with_error_dialog( "LOGO VIVA ISSY 2023.png" )
			t.pilot_issy	= IMGS.get_img_with_error_dialog( "Pilot_Issy_1.jpg" )
			t.partners		= IMGS.get_img_with_error_dialog( "ArtCast4D_partners.jpg" )
			t.qrcode		= IMGS.get_img_with_error_dialog( "AAASeed_QrCode.jpg" )
			t.issy_1		= IMGS.get_img_with_error_dialog( "Issy_fresco.jpg" )
			t.issy_2		= IMGS.get_img_with_error_dialog( "Issy_Cartomaton.jpg" )
			t.issy_3		= IMGS.get_img_with_error_dialog( "Issy_hownlow.jpg" )
			t.issy_4		= IMGS.get_img_with_error_dialog( "Issy_Mix.jpg" )
			t.cpp 			= IMGS.get_img_with_error_dialog( "AAASeed_Cpp.tga" )
			t.cpp_lua 		= IMGS.get_img_with_error_dialog( "AAASeed_Cpp_lua.tga" )
			t.gabu 			= IMGS.get_img_with_error_dialog( "GABU.png" )
			t.meu 			= IMGS.get_img_with_error_dialog( "MEU.png" )
			
			self.imgs = t
		MEDIA.pop_dir()
	end
	if not self.videos then
		VIDEOS:set_dir_media( dir )
		local v_names = 
		{
			monaco = "Immersion.mp4",
			floating_point = "floatingPoint3.v3.credit.mp4",
--			microcosmos = "Microcosmos.mp4",
			epidemik = "Epidemik-Plateau-reportage-H.264 800 Kbps.mov",
			aaaquatik = "SWIMING_POOL_Djeddah_2019.mp4"
		}
		local t = {}
		for key,fname in pairs( v_names ) do
			t[key] = VIDEOS:get( fname )
			self:print_debug( "videos key "..key.." is "..t[key] )
		end
		self.videos = t
		table.print( self.videos, "VIDEOS POWOW" )
	end

end

function meu:init()
	local ref = self.ref
	local data = {}
	ref.data		=	data
		data.layer		= self:get_layer(1)
		data.bdd		= self:get_layer_bdd(1)
		data.data_new	= param.get_ref( data.bdd, "data_new"		)
		data.y_max		= param.get_ref( data.bdd, "y_max" 			)
		data.filename	= param.get_ref( data.bdd, "data_filename"	)


	ref.layer_boid	= self:get_layer(2)
	ref.layer_attr	= self:get_layer(3)

	ref.text = {}
	for i=1,1 do
		local t = {}
		local ind = 3+i
		t.layer			= self:get_layer(ind)

		t.bdd			= self:get_layer_bdd(ind)
		t.font			= param.get_ref( t.bdd, "font"			)
		t.alignment		= param.get_ref( t.bdd, "alignment"		)
		t.text			= param.get_ref( t.bdd, "text"			)
		t.x 			= param.get_ref( t.bdd, "origin_x"		)
		t.y 			= param.get_ref( t.bdd, "origin_y"		)
		t.z 			= param.get_ref( t.bdd, "origin_z"		)
		t.llen 			= param.get_ref( t.bdd, "line_lenght"	)

		t.model			= aaa.layer.get_model_no_error( t.layer )
		t.size			= param.get_ref( t.model, "size_factor" )
		ref.text[i] = t
	end

	ref.slide_index = param.get_ref( aaa.ref.pref, "slide_index" )
	self.id_cur = 1
	self:__set_slide_index(1)
end

function meu:__get_slide_index()		return param.get( self.ref.slide_index )	end
function meu:__set_slide_index( id )	param.set( self.ref.slide_index, id )	end
function meu:inc_slide( add )
	self.id_cur = self.id_cur + add
	self:__set_slide_index( self.id_cur )
end

function meu:restart()
--	param.set( self.ref.boid, "restart_trig", 1  )
end


local BLACK = 20
local BLACKCOND = 21
local BOLD = 22
local BOLDCOND = 23
local SEMIEXT = 24
local REGULAR = 25
local SEMICN = 26
local SEMICNIT = 27

function meu:draw_text( i, txt, x,y, s, llen, align )
	local ref = self.ref.text[1]
	param.set( ref.text, txt or "" )
	if txt then
		if x then param.set( ref.x, x ) end
		if y then param.set( ref.y, y ) end
		if s then param.set( ref.size, s/100 ) end
		if llen then param.set( ref.llen, llen ) end
		if align then param.set( ref.alignment, align ) end
		aaa.obj.update_then_draw( ref.layer )
	end

end

function meu:draw_texture( bind )
--	gol.push_attrib()

	gol.bind_texture( bind )
	local sx,sy = aaa.img.get_size( bind )
	if sx then
		local S = 8
		gol.color_white()
		local SY = S	local SX = S*sx/sy
		local x,y, sx,sy = -SX*.5, -SY*.5, SX, SY
		gol.set_texture_dim( 2 )
		aaa.draw_rect_uv( x,	y, x+sx		, y+sy			)
		gol.set_texture_dim( 0 )
	else
	--	self:print( bind )
	end
end

function meu:update_ui(  )
	local ui = self.ui
	ui.bu_filename:set_text( self.data_filename )
	ui.bu_id:set_text( self.id_cur )
end

function meu:update(  )
	self:load_media()
	--self:set_text( "test" )
	--table.print( self.imgs )
end

function meu:update_datagrid()
	local ref = self.ref
	local ui = self.ui
	local dref = ref.data

	local env = ui.bu_sel:get_item_text()
	--table.print( env )
	local fname = "powow_"..string.lower( env )..".csv"
	self.data_filename = fname
	param.set( dref.filename, fname )
 
	aaa.obj.update_then_draw( dref.layer )
	if param.get_bool( dref.data_new ) then
		local bdd = dref.bdd
		--self.data_filename = param.get( dref.filename )
		--self:box_debug( "new Data" )
		local keys = {}
		local nb = 1
		for i=1,4096 do
			local v = aaa.obj_get_data( bdd, i, 1 )
			if v == nil then
				break
			else
				local key = string.gsub( v, "[%c%s]", '_' )
				key = string.lower( key )
				self:print( "Found key "..v.." -> "..key )
				keys[#keys+1] = key
			end
		end
		table.print( keys, "datagrid keys" )
		local y_max = param.get( dref.y_max )
		local x_max = #keys
		local tab = {}
		for l=2,y_max do
			local t = {}
			for i=1,x_max do
				t[keys[i]] = aaa.obj_get_data( bdd, i, l )
			end
			tab[l-1] = t
		end
		self.__data = tab
		table.print( tab, "datagrid data for "..self.data_filename, 2 )
		param.set( dref.data_new, false )
	end
end

function meu:draw_slide()
	local ref = self.ref

	if self.b_boid then
		aaa.obj.update_then_draw( self.ref.layer_boid )
	end

	self:draw_layer( 3 )
	local data = self.__data
	if not data then
		return
	end

	local id = self.b_slide and self:__get_slide_index() or self.id_cur
	id = math.fmod( id-1, #data ) + 1
	self:__set_slide_index( id )
	self.id_cur = id
	
	local t = {}
	local l = data[id]
	local function inter( na )
		if l[na] then
			local a = self[na] or 0
			if type(a)=="string" then
				a = 0
			end
			local b = l[na]
			if type(b)=="string" then
				b = 0
			end
			t[na] = interpolate( a, b, aaa.time.dt * 3. )
			self[na] = t[na]
		end
	end
	local function inter_table( t )
		for _,na in ipairs( t ) do inter(na) end
	end

	--self:print( "slide "..id )
	--table.print( l, "slide")

	if not l then
		return
	end

	for i = 1,2 do
		local pre
		if i==1 then
			pre = l.img and "" or "a_"
		else
			pre = "b_"
		end
		local key = pre.."img"
		local name = l[key]
		if name then
			if name == "" then
				self:print_debug( key..": name is empty" )
			elseif name == " " then
				self:print_debug( key..": name is space" )
			else
				--self:print( name )
				local img = IMGS.get_img( name )
				--local img = IMGS.get_img_with_error_dialog( "AAAPowow/"..l.img )
				if img then
					--table.print( l, "img", 2 )
					--gol.reset()
					inter_table( { pre.."media_x", pre.."media_y", pre.."media_size" } )
					--self:print( l[pre.."media_y"].." "..t[pre.."media_y"] )

					gol.set_texture_dim(2)
					local mem = img[pre.."mem"] or { x=0,y=0, s=6 }
					local x = t[pre.."media_x"]    or mem.x
					local y = t[pre.."media_y"]    or mem.y
					local s = t[pre.."media_size"] or mem.s
					aaa.show( i.." -> "..x.." "..y.." s "..s  )
					img[pre.."mem"] = { x=x,y=y, s=s }
					img:draw_xy_sx( x,y, s  )
				else
					self:print_debug( key..": no image for name "..name )
				end
			end
		end
	end

	self.bdd_point = aaa.bdd.get_cur()
	aaa.obj.update_then_draw( ref.layer_attr )

	--todo interpolate all (with flags)
	inter_table( { "ax","ay","as", "bx","by","bs", "cx","cy","cs" } )
	--	param.set( ref.font, REGULAR )
	--	param.set( ref.alignment, "center" )
	local function draw_rect_one( i, txt, x,y, s, llen, align )
		s = s or 1
		if txt then
			txt = string.gsub( txt, "\t", "        " )
			txt = string.gsub( txt, "\\t", "        " )
			txt = string.gsub( txt, "\n", "\n " )
			self:draw_text( i, txt, x,y, s, llen, align )
			--self:print( txt )
		end 
	end
	draw_rect_one( 1, l.at, t.ax,t.ay, t.as,	l.allen, l.aalign )  
	draw_rect_one( 2, l.bt, t.bx,t.by, t.bs,	l.bllen, l.balign )
	draw_rect_one( 3, l.ct, t.cx,t.cy, t.cs,	l.cllen, l.calign )

	if self.b_logo and self.imgs.logo then
	--	self.imgs.logo:draw_xy_sx( 3.9,-2, .5  )
		self.imgs.logo:draw_xy_sx( self.logo_u,self.logo_v, self.logo_size  )
	end

	local function draw_rect( vid, x,y, sx,sy )
		local bind = vid:get_bind()
		--self:print( vid.." "..bind )
		local vsx,vsy = vid:get_size()
		if not sy then
			sy = sx * vsy / vsx
		elseif not sx then
			sx = sy * vsx / vsy
		end
		gol.set_texture_dim( 2 )
		gol.bind_texture( bind )
		sx = sx * .5
		sy = sy * .5
		aaa.draw_rect_uv( x-sx, y-sy, x+sx, y+sy )
	end

	local function stop_video( vid )
		if vid then
			vid:stop()
			vid:restart()
		end
	end
	--table.print( self.videos, "Videos" )
	local video_name = l.video 
	--self:print( "video_name is "..video_name )
	if video_name then
		local vid = self.videos[video_name]
		--self:print( vid.." is "..vid )
		if vid then
			if vid ~= self.vid_cur then
				stop_video( self.vid_cur )
				self.vid_cur = vid
				vid:restart()
			end
	 		vid:play()
	 		draw_rect( vid, l.video_x,l.video_y, l.video_size )
	 		--draw_rect( vid, 0,0, 1 )
		end
		self.vid_cur = vid
	else
		if self.vid_cur then
			--self:print( self.vid_cur.." stop" )
			stop_video( self.vid_cur )
			self.vid_cur = nil
		end
	end
end

function meu:draw_grid( alpha )
	local sx,sy = 4,4
	gol.set_texture_dim(0)
	local step = 1

	gol.color_cyan( alpha * .5 )
	for i = 1,3 do
		gol.set_line_width( 4 * step )
		for x=-sx,sx,step do aaa.draw_line( x,-sy, x,sy ) end
		for y=-sy,sy,step do aaa.draw_line( -sx,y, sx,y ) end
		step = step / 4
	end

	gol.set_line_width( 8 )
	gol.color_red( alpha *.5 )
	aaa.draw_line( -sx,0, sx,0 )
	gol.color_green( alpha * .5 )
	aaa.draw_line( 0,-sy, 0,sy )
end

function meu:draw()
	self:draw_layers_begin()

		self:update_datagrid()
		local alpha = self:get_alpha()
		if self.b_grid then self:draw_grid(alpha) end
		gol.color_white( alpha )
		self:draw_slide()

	self:draw_layers_end()
end

