term=peripheral.find('monitor')
term.setBackgroundColor(colors.black)
term.clear()

premierX=2 -- Premier X
premierY=1 -- Premier Y
largeur=9  -- Largeur bouton
hauteur=5  -- Hauteur bouton
ligneY=3 -- hauteur du texte
marge=1    -- Marge entre deux boutons
coteRed="right"


liste={
	{
		nom='Sapin',
		color=colors.black,
		choix=true
	},
	{
		nom='Noir',
		color=colors.pink,
		choix=true
	},
	{
		nom='Acajou',
		color=colors.green,
		choix=true
	},
	{
		nom='Bouleau',
		color=colors.brown,
		choix=true
	},	
	{
		nom='Accacia',
		color=colors.blue,
		choix=true
	},	
	{
		nom='Chene',
		color=colors.purple,
		choix=true
	},
	{
		nom='Arcanique',
		color=colors.cyan,
		choix=true
	},
}

function actuWin(i)
	if liste[i].choix then
		liste[i].win.setBackgroundColor(colors.green)	
	else
		liste[i].win.setBackgroundColor(colors.red)
	end	
	liste[i].win.clear()
	liste[i].win.setCursorPos(1+math.floor((largeur-string.len(liste[i].nom))/2),ligneY)
	liste[i].win.write(liste[i].nom)
end
function actuRed()
	redsTous=colors.red
	table.foreach(liste,function(i,data)
		if liste[i].choix==false then
			redsTous=redsTous+liste[i].color
		end
	end)
	redstone.setBundledOutput(coteRed,redsTous)
end
table.foreach(liste,function(i,data)
	file=fs.open(tostring(i),"r")
	if file~=nil then
		if file.readLine()=='true' then
			liste[i].choix=true
		else
			liste[i].choix=false
		end
		file.close()
	else
		file=fs.open(tostring(i),"w")
		file.write(liste[i].choix)
		file.close()
	end
	liste[i].win=window.create(term,premierX+((i-1)*(largeur+marge)),premierY,largeur,hauteur,true)	
	actuWin(i)
end)
actuRed()

while true do
	event, side, xPos, yPos = os.pullEvent("monitor_touch")
	if (xPos-premierX)%(largeur+marge)<largeur then
		u=math.floor((xPos-premierX)/(largeur+marge))+1
		if liste[u] then
			if liste[u].choix then
				liste[u].choix=false
			else
				liste[u].choix=true
			end
			file=fs.open(tostring(u),"w")
			file.write(liste[u].choix)
			file.close()
			actuWin(u)
			actuRed()
		end
	end	
end
