SCORES_LUA = true

scores = {}

scores.max  = 5
scores.file = "data/HighScores.dat"
scores.data = {}

function scores:create()
    -- load up current high scores from score file
    -- set variables like numscores, create score screen, uuh

    local f = loadfile(self.file)
    if f then f() end
end

function scores:get_scores()
	if( scores.data == not(nil)) then
        return scores.data
	end
end

function scores:add_score( initials, track, time )
    local newscore = {}

    newscore.timeInSeconds = time
    newscore.initials 	   = initials
    newscore.track    	   = track
    newscore.time          = format_time(time)
    
    table.insert(scores.data, newscore)
    self:sort_scores()
    self:prune_scores()
    self:write_to_file()
end


function scores:sort_scores()                                    
    table.sort( scores.data, function (a,b) 
				 return parse_time(a.time)<parse_time(b.time)
			     end)
end


function scores:prune_scores()
    -- keep only the highest [self.max] number of scores.  
    -- If there are extras, get rid of them.
    if ( table.getn(scores.data) > scores.max ) then
		table.remove (scores.data)
	end
end


-- time passed should be in seconds
function scores:is_top( track, time )
    -- check to see if a [time] on a particular [track] is one of the
    -- top [self.max] scores
    lastIndex = table.getn(scores.data)
    if(lastIndex < self.max) then
	return true
    else
	return time < parse_time(scores.data[lastIndex].time)
    end
end

function scores:write_to_file()
    -- output the scores to [self.file]
    -- in a format that you have specified for later reading by self:load_file()

    -- one suggestion, format the output as a runnable lua file 
    -- which contains a single table definition for scores.data
    -- then you only need one line in your self:load_file() function:
    --     "dofile(self.file)"

    -- in other words make the output to the file look like this:
    -- scores.data = {
    -- { ["initials"] = "cjk", ["track"] = "default", ["time"] = "32:20.20", },
    -- { ["initials"] = "wer", ["track"] = "default", ["time"] = "32:20.20", },
    -- { ["initials"] = "afe", ["track"] = "default", ["time"] = "32:20.20", },
    -- }

    -- you will need to refer to the documentation on the lua.org website,
    -- (both the reference manual and the "programming in lua" book are 
    -- helpful and online) particularly under the io standard library
    
    -- some other stuff that might be useful
    --  - use string.format("%d, %d, %d", x, y, z) to create strings
    --    using the c language's printf() syntax
    --  - string.format("%q", str)  will print out the string [str]
    --    with surrounding quotations. 
    --    ex: string.format("%q", "hello") --> '"hello"'
    --  - check out svn://cube.evl.uic.edu/bvis/stereoviewer
    --    for an example of outputting executable lua code.
    --    Look specifically at the save_offsets() function in 
    --    viewstereo.lua, and at the config/offsets.lua file.

 
    -- save the current output destination, then direct output to file
    local current = io.output()
    io.output(self.file)

    -- write first line of file, containing the table's name
    io.write("scores.data = { \n")
                    
    -- for each element of the table, write out a line in the file
    -- scores.data = {
    -- { ["initials"] = "cjk", ["track"] = "default", ["time"] = "32:20.20", },
    -- { ["initials"] = "wer", ["track"] = "default", ["time"] = "32:20.20", },
    -- { ["initials"] = "afe", ["track"] = "default", ["time"] = "32:20.20", },
    -- }
    --[[
    for k, v in pairs(self.scores.data) do
	io.write("{ ["initials"] = v[2], ["track"] = v[3], ["time"] = v[4], },")
    end
    
    -- io.write("    [", string.format("%q", k), "] = { ")
    --]]
    
    
    for k, v in pairs(scores.data) do
    	io.write("{ ",
		 string.format("%s%q%s%q %s%q%s%q %s%q%s%q %s",
			       "["  , "initials", "] =", v.initials,
			       ", [", "track",    "] =", v.track,
			       ", [", "time",     "] =", v.time,
			       "},\n"))
    end
    
    -- finalize the table
    io.write("}\n")
    
    -- close the file
    io.output():close()
    -- redirect output to the saved destination
    io.output(current)
                                                                  
end

