-- Profile
----------------------------------------------------------
-- Last update: August 15th, 2007 by Renaud
-- KNOWN ISSUES:




----------------------------------------------------------
-- PUBLIC FUNCTIONS
-- setDefaults me
-- loadProfile me, fileName
-- saveProfile me
-- 
-- addKeyItem me, theItem
-- checkKeyUsed me, theItem
-- useKey me, theItem

-- common
property pPlayerName
property pFileName
property pPlayed

-- game specific
property pLevel -- the level were at
property pRound -- the round were at

property pShowEnding

property pOverAllGameTime -- in seconds
property pBeatGameCount

property pHelpPopups -- prop list holds help popups that need displaying

property pGameIO

on new me, savePath
  
  pGameIO = new(script "Parent_GameIO", savePath)
  
  pPlayerName = ""
  
  -- inits vars to default
  setDefaults me
  
  return me
end

on setDefaults me
  
  pOverAllGameTime = 0
  pBeatGameCount = 0
  
  pShowEnding = false
  pPlayed = false
  pLevel = 1
  pRound = 1
  
  pHelpPopups = []
  
end

on beatGame me
  pBeatGameCount = pBeatGameCount + 1
end

on addOverallTime me, seconds
  pOverAllGameTime = pOverAllGameTime + seconds
end

on createNew me, fileName, playerName
  pPlayerName = playerName
  pFileName = fileName
  me.setDefaults()
  me.saveProfile()
end

on loadProfile me, fileName
  pFileName = fileName
  
  saveData = pGameIO.LoadData(pFileName)
  
  theValue = value(saveData)
  
  if NOT(listp(theValue)) then
    
    me.debug("Invalid profile loaded.")
    
  else
    
    pPlayerName = thevalue.playerName
    pLevel = theValue.level
    pRound = theValue.round
    pPlayed = theValue.played
    
    pOverAllGameTime = theValue.OverAllGameTime
    pBeatGameCount = theValue.BeatGameCount
    
    pHelpPopups = theValue.helpPopups
    
    
  end if
end

on getSaveString me
  saveData = [#playerName:pPlayerName,\
              #level:pLevel,\
              #round:pRound,\
              #played:pPlayed,\
              #OverAllGameTime:pOverAllGameTime,\
              #BeatGameCount:pBeatGameCount,\
              #helpPopups:pHelpPopups]
  
  -- make sure none of these values are <void>
  if string(saveData) contains "<void>" then
    me.debug("unable to save profile: prop = void")
  end if
  
  return string(saveData)
end

on saveProfile me
  saveStr = me.getSaveString()
  
  pGameIO.SaveData(pFileName, saveStr) -- into registry
end

on debug me, str
  if getAuthor() then
    alert(str)
  end if
end