-- Sorry for the mess. Here's the start of my Globals.
oldpad = Controls.read()
-- This prototype is used to temporarily hold my generated sentence so it can be outputted to screen or file without making a whole new sentence on accident.
thispattern = nil
backuppattern = nil
toggle = nil
oldfav = nil
favsread = io.open("homework_favs.txt", "r")
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
red = Color.new(255, 0, 0)
grey = Color.new(200, 200, 200)
mainback = Image.load("srsgmain.png")

-- Function for my main screen display.
function main()
screen:blit(0,0,mainback)
screen:print(140, 170, "<up> Generate an excuse", green)
screen:print(140, 180, "<right> Save to favorites", green)
screen:print(140, 190, "<down> Back one sentence", grey)
screen:print(140, 200, "<left> Review Homework favs", blue)
screen:print(140, 210, "", blue)
screen:print(140, 220, "<cross> Return to Categories", red)
screen:print(140, 230, "<menu> Exit SRSG", red)
screen:print(140, 240, "", red)
end

-- Prototype for my pattern table which seems to need to be in my main loop.
hwpatternArray={}
-- Currently all of my data is hardcoded. Here's the word lists seperated by word type.
animalsArray={"my little brother", "my little sister", "a bear", "a mountain lion", "a lion", "a dog", "a cat", "a mouse", "a gerbil", "a hamster", "a fox", "a raccoon", "a opossum", "a mole", "a smurf", "a crocodile"}
destroypastpartArray={"eaten", "stolen", "pooped on", "peed on", "ripped-up", "slobbered on", "devoured", "shredded", "erased", "crushed", "crumpled", "spit on"}
destroypastArray={"ate", "stole", "pooped on", "peed on", "slobbered on", "devoured", "shredded", "erased", "crushed", "crumpled", "spit on"}
fallplaceArray={"a lion's den", "a cave", "a lion's mouth", "a manhole", "a lava stream", "the Grand Canyon", "a mud puddle", "the toilet", "the snake pit from Indiana Jones", "the ocean", "an alligator's mouth", "a dragon's nest", "a plumber's buttcrack", "hell", "a large chasm", "Courtney Love's hole", "the sewer", "the Principal's secret stash"}


screen:clear()
main()
screen:print(78,65, "Welcome to the Homework Excuse Generator.", green)
screen:print(100,75, "Please choose an option to begin...", green)
screen.flip()

-- This is my function for generating random numbers. The random generator is seeded by the os.time
-- and is then fed into the random generator, one item at a time, AND returns an interger based on
-- (found withing the range of) the number of items in the respective table.
function rand(numer) 
randnumer = math.random(numer)
return randnumer
end

while true do
-- seeding my random generator within my main loop to allow constantly updated seeds
math.randomseed(os.time())
pad = Controls.read()

-- Here's my sentence patterns. They reference a category's table and randomly select an entry.
-- Each random number is restricted to the available table entries by using table.getn to set the
-- upper range of the randomly generated numbers. Later on in the code the patterns themselves are randomly
-- selected. Each pattern will create a basic sentence and the details are the only things that
-- actually change. Depending on how many patterns you have and how well you categorize your
-- word lists, your sentence quality and variety can rise or fall. The comment under each line is
-- a sentence that could be made by each pattern. These help me streamline the generation by
-- reminding me quickly what types of words should fit into each category.
hwpatternArray[1]="My homework was "..destroypastpartArray[rand(table.getn(destroypastpartArray))].." by "..animalsArray[rand(table.getn(animalsArray))].."!"
--My homework was eaten by a bear!
hwpatternArray[2]="My homework fell into "..fallplaceArray[rand(table.getn(fallplaceArray))].." and I couldn't get it back!"
--My homework fell into a lion's mouth and I couldn't get it back!
hwpatternArray[3]="I was keeping my homework in "..fallplaceArray[rand(table.getn(fallplaceArray))].." to hide it from "..animalsArray[rand(table.getn(animalsArray))].." but then "..animalsArray[rand(table.getn(animalsArray))].." took it and "..destroypastArray[rand(table.getn(destroypastArray))].." it!"
--I was keeping my homework in a cave to hide it from a lion but then my little brother took it and peed on it.



if pad:up() and oldpad:up() ~= pad:up() then
-- Here's my pattern being randomly selected and assigned to a temporary variable holder. I have also added
-- in a second holder to take the old value and preserve it till another sentence is created.
toggle = true
backuppattern = thispattern
thispattern = hwpatternArray[rand(table.getn(hwpatternArray))]
screen:clear()
main()
screen:print(20,40,string.sub(thispattern,1,53),green)
screen:print(20,50,string.sub(thispattern,54,105),green)
screen:print(20,60,string.sub(thispattern,106,157),green)
-- For some reason, having this in my main loop outside of an event causes flickering. Anyone
-- else have this problem? The tutorials I read suggested otherwise, so I was pretty happy when
-- I finally figured out that I could stop the flickering my placing my screen.flips in my events. 
screen.flip()
end

-- This is for going back a sentence. I want to allow the program to toggle back and forth between
-- the current and the last generated one, but for some reason there is a problem with changing from
-- the old sentence, back to the current one. I think it is a flipping problem, but it might be my
-- long list of conditions.
if pad:down() and oldpad:down() ~= pad:down() and thispattern ~= nil and toggle == false then
toggle = true
screen:clear()
main()
screen:print(20,40,string.sub(thispattern,1,53),green)
screen:print(20,50,string.sub(thispattern,54,105),green)
screen:print(20,60,string.sub(thispattern,106,157),green)
screen.flip()
end

if pad:down() and oldpad:down() ~= pad:down() and backuppattern ~= nil and toggle == true then
toggle = false
screen:clear()
main()
screen:print(20,40,string.sub(backuppattern,1,53),grey)
screen:print(20,50,string.sub(backuppattern,54,105),grey)
screen:print(20,60,string.sub(backuppattern,106,157),grey)
screen.flip()
end

-- Can't see something that's not there.
if pad:down() and oldpad:down() ~= pad:down() and backuppattern == nil then
screen:clear()
main()
screen:print(20,40,"Please generate 2 sentences first...",red)
screen.flip()
end

-- This saves extremely funny sentences to a file called homework_favs.txt. It appends to the 
-- end of the file and inserts a line break before each sentence. I added in conditions to prevent
-- a line return from being inserted at the top of a blank file. I realize I could probably do this
-- with fewer lines of code, but since it works, I'd rather not take the time to change it right now. =)
if pad:right() and oldpad:right() ~= pad:right() and thispattern ~= nil and toggle == true then
favscheck = io.open ("homework_favs.txt", "r")
favsblank = favscheck:read()
favscheck:close()
if favsblank == nil then
favs = io.open("homework_favs.txt", "a")
favs:write(thispattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to homework_favs.txt...",red)
screen:print(20,50,string.sub(thispattern,1,53),green)
screen:print(20,60,string.sub(thispattern,54,105),green)
screen:print(20,70,string.sub(thispattern,106,157),green)
screen.flip()
else
favs = io.open("homework_favs.txt", "a")
favs:write("\n"..thispattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to homework_favs.txt...",red)
screen:print(20,50,string.sub(thispattern,1,53),green)
screen:print(20,60,string.sub(thispattern,54,105),green)
screen:print(20,70,string.sub(thispattern,106,157),green)
screen.flip()
end
end

if pad:right() and oldpad:right() ~= pad:right() and thispattern ~= nil and toggle == false then
favscheck = io.open ("homework_favs.txt", "r")
favsblank = favscheck:read()
favscheck:close()
if favsblank == nil then
favs = io.open("homework_favs.txt", "a")
favs:write(backuppattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to homework_favs.txt...",red)
screen:print(20,50,string.sub(backuppattern,1,53),grey)
screen:print(20,60,string.sub(backuppattern,54,105),grey)
screen:print(20,70,string.sub(backuppattern,106,157),grey)
screen.flip()
else
favs = io.open("homework_favs.txt", "a")
favs:write("\n"..backuppattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to homework_favs.txt...",red)
screen:print(20,50,string.sub(backuppattern,1,53),grey)
screen:print(20,60,string.sub(backuppattern,54,105),grey)
screen:print(20,70,string.sub(backuppattern,106,157),grey)
screen.flip()
end
end

-- Again, you can't write somethign that's not there.
if pad:right() and oldpad:right() ~= pad:right() and thispattern == nil then
screen:clear()
main()
screen:print(20,40,"Please generate a sentence first...",red)
screen.flip()
end

if pad:left() and oldpad:left() ~= pad:left() then
oldfav = favsread:read()
if oldfav == nil then
favsread:close()
favsread = io.open("homework_favs.txt", "r")
screen:clear()
main()
screen:print(20,40,"End of file. Please start again.",red)
screen.flip()
else
screen:clear()
main()
screen:print(20,40,string.sub(oldfav,1,53),blue)
screen:print(20,50,string.sub(oldfav,54,105),blue)
screen:print(20,60,string.sub(oldfav,106,157),blue)
screen.flip()
end
end

screen.waitVblankStart()
oldpad = pad
if Controls.read():cross() then break end
-- The end
end
favsread:close()


