dofile("data/database.lua")
data_debut = 1
selecteur_data = Image.load("images/select_dat.png")
y_curseur_data = 20
data_curseur_debut = 1
data_tmp = 1

function afficher_data_name()
   tmp_ligne_data = {}
   nb_tmp_ligne_data = 0
   last=0
   for i=1, string.len(database[data_tmp].description) do
      sub_tmp = string.sub(database[data_tmp].description,i,i)
      if sub_tmp == "\n" then
         nb_tmp_ligne_data = nb_tmp_ligne_data + 1
         tmp_ligne_data[nb_tmp_ligne_data] = string.sub(database[data_tmp].description,last,i-1)
         last = i + 1
      end
   end
   while true do
      pad = Controls.read()
      y_tmp = 30
      screen:clear()
      screen:print(100,2,database[data_tmp].name,blanc)
      for i=1 , nb_tmp_ligne_data do
         screen:print(0,y_tmp,tmp_ligne_data[i],blanc)
         y_tmp = y_tmp + 10
      end
      if pad:cross() and not oldpad:cross() then
         oldpad = pad
         return
      end
      screen.waitVblankStart()
      screen.flip()
      oldpad = pad
   end 
end

function afficher_data()
   y_data = 20
   for i=data_debut , data_debut + 21 do
      if i<=nb_database then
         screen:print(40,y_data,database[i].name,blanc)
      end
   y_data = y_data + 10
   end
end

function afficher_selecteur_data()
   if pad:down() and not oldpad:down() then
      if data_tmp < nb_database then
      y_curseur_data = y_curseur_data + 10
      data_tmp = data_tmp + 1
         if y_curseur_data > 230 then
         data_debut = data_debut + 22
         data_curseur_debut = data_curseur_debut + 21
         y_curseur_data = 20
         end
      end
   end
   if pad:up() and not oldpad:up() then
      y_curseur_data = y_curseur_data - 10
      data_tmp = data_tmp - 1
      if y_curseur_data < 20 then
         if data_debut > 1 then
            data_debut = data_debut - 21
            data_curseur_debut = data_curseur_debut - 21
            y_curseur_data = 230
         else
            y_curseur_data = y_curseur_data + 10
            data_tmp = data_tmp + 1
         end
      end
   end
   if pad:cross() and not oldpad:cross() then
      oldpad=pad
      afficher_data_name()
   end
   screen:blit(10,y_curseur_data,selecteur_data)
end

while true do
   pad = Controls.read()
   screen:clear()
   if pad:select() and not oldpad:select() then break end 
   screen:print(100,2,"Lua Fonctions: "..nb_database,blanc)
   afficher_data()
   afficher_selecteur_data()
   screen.waitVblankStart()
   screen.flip()
   oldpad = pad
end 

selecteur_data = nil
System.memclean()
