Profile = {}

function Profile:new()
    Profile.data = {}
    Profile.data.level = 1
    Profile.data.keysCollected = 0
    Profile.data.cutscene = 1
    Profile.cache = {}
end

function Profile:resetToBegin()
    Profile.data.level = 1
    Profile.data.cutscene = 1
    
    table.foreach(Profile.data, function(item)
		if string.sub(item,1,3) == 'Loc' then
			local keys = Profile:getKeys(item)
			Profile.data[item] = {}
			Profile:setKeys(item,keys)
			Profile.cache[item] = pluto_s:serialize_table(Profile.data[item])
		end
	end)
end

function Profile:save(file_locker)
    Profile.cache['level'] = Profile.data.level
    Profile.cache['keysCollected'] = Profile.data.keysCollected
    Profile.cache['cutscene'] = Profile.data.cutscene
    pluto_s:save_table(Profile.cache, file_locker:filename())
end

function Profile:serializeFromLocation(name, chapter)
    local loc_data = Profile.data[name]
    if loc_data ~= nil then
        Profile.cache[name] = pluto_s:serialize_table(loc_data)
    end
end

function Profile:load(file_locker)
	
	Profile.cache = pluto_s:load_table(file_locker:filename())
    table.foreach(Profile.cache, 
        function(item)  
				Profile.data[item] = pluto_s:unserialize_table(Profile.cache[item])
        end)
        
	Profile.data.level = Profile.cache['level']
    Profile.data.keysCollected = Profile.cache['keysCollected']
    Profile.data.cutscene = Profile.cache['cutscene']

end

function Profile:pack()
    return pluto_s:serialize_table(Profile.data)
end

function Profile:unpack(aData)
    Profile.data = pluto_s:unserialize_table(aData)
end

function Profile:setLevel(aLevel)
    Profile.data.level = aLevel
end

function Profile:getLevel()
    return Profile.data.level
end

function Profile:check(aLocationName, aScene) 
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
end

function Profile:getSceneState(aLocationName, aScene) -- new / initialized / started

    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end

    if Profile.data[aLocationName]['scene_'..aScene] == nil then
        Profile.data[aLocationName]['scene_'..aScene] = 'new'
    end

    return Profile.data[aLocationName]['scene_'..aScene]
end

function Profile:onSceneInit(aLocationName, aScene) -- new / initialized / started
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['scene_'..aScene] = 'initialized'
end

function Profile:onSceneStart(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['scene_'..aScene] = 'started'
end

function Profile:getHidState(aLocationName, aScene, aName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['hid_'..aScene..'_'..aName]
end

function Profile:setHidState(aLocationName, aScene, aName, aState)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['hid_'..aScene..'_'..aName] = aState
end

function Profile:getInteractState(aLocationName, aScene, aName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['int_'..aScene..'_'..aName]
end

function Profile:getAmbListeners(aLocationName, aScene)
    if Profile.data[aLocationName]['amb_listeners_'..aScene] == nil then
        Profile.data[aLocationName]['amb_listeners_'..aScene] = {}
    end

    return Profile.data[aLocationName]['amb_listeners_'..aScene]
end

function Profile:setInteractState(aLocationName, aScene, aName, aState)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['int_'..aScene..'_'..aName] = aState
end

function Profile:getExtraState(aLocationName, aName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['extra_'..'_'..aName]
    -- (self.mLocation.mName, name)
end

function Profile:setExtraState(aLocationName, aName, aState)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['extra_'..'_'..aName] = aState
end

function Profile:getInvList(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    if Profile.data[aLocationName]['inv_'..aScene] == nil then
        Profile.data[aLocationName]['inv_'..aScene] = {}
    end

    return Profile.data[aLocationName]['inv_'..aScene]
end

function Profile:getLocationInfo(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['loc_'..aScene]
end

function Profile:setLocationInfo(aLocationName, aScene, aInfo)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['loc_'..aScene] = aInfo
end

function Profile:getMixes(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    
    if Profile.data[aLocationName]['mix_'..aScene] == nil then
        Profile.data[aLocationName]['mix_'..aScene] = {}
    end

    return Profile.data[aLocationName]['mix_'..aScene]
end

function Profile:getWinTerms(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    
    if Profile.data[aLocationName]['win_'..aScene] == nil then
        Profile.data[aLocationName]['win_'..aScene] = {}
    end

    return Profile.data[aLocationName]['win_'..aScene]
end

function Profile:getSceneTask(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    if Profile.data[aLocationName]['task_'..aScene] == nil then
        Profile.data[aLocationName]['task_'..aScene] = {}
    end

    return Profile.data[aLocationName]['task_'..aScene]
end

function Profile:setSceneTask(aLocationName, aScene, aTask)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['task_'..aScene] = aTask
end

function Profile:getSceneRemark(aLocationName, aScene)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['remark_'..aScene]
end

function Profile:setSceneRemark(aLocationName, aScene, aRemark)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['remark_'..aScene] = aRemark
end

function Profile:getCloseUpState(aLocationName, aScene, aName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['cu_'..aScene]
end

function Profile:setCloseUpState(aLocationName, aScene, aName, aState)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['cu_'..aScene] = aState
end

function Profile:getKeys(aLocationName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['keys']
end

function Profile:setKeys(aLocationName, aKeys)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['keys'] = aKeys
end

function Profile:getKeysFoundNow(aLocationName)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    return Profile.data[aLocationName]['keys_found_now']
end

function Profile:setKeysFoundNow(aLocationName, aKeys)
    if Profile.data[aLocationName] == nil then Profile.data[aLocationName] = {} end
    Profile.data[aLocationName]['keys_found_now'] = aKeys
end

function Profile:getUserData(aName)
    if not Profile.cache then return nil end
    local data = Profile.cache[aName]
    if not data then return nil end
    return pluto_s:unserialize_table( data )
end

function Profile:setUserData(aName,aData)
    Profile.cache[aName] = pluto_s:serialize_table(aData)
end
