ProfileList = {}

ProfileList.data = {}

ProfileList.data.current = -1
ProfileList.data.last = -1
ProfileList.fds = {}

ProfileList.list_ds = create_file_locker()
ProfileList.list_ds:lock('profile_list.txt')

function ProfileList:flush()
    ProfileList:checkList()
    pluto_s:save_table(ProfileList.data, ProfileList.list_ds:filename() ) 
end


function ProfileList:load()

    local data = pluto_s:load_table(ProfileList.list_ds:filename())
    if data ~= nil then

        ProfileList.data = data
        
        for i = 1, #ProfileList.data.profiles do
            local prof_rec = ProfileList.data.profiles[i]
            ProfileList.fds[i] = create_file_locker()
            ProfileList.fds[i]:lock(prof_rec.filename)
        end

        ProfileList:checkList()

        if ProfileList.data.current > 0 then
            Profile:new()
            Profile:load( ProfileList.fds[ProfileList.data.current] )
            Tutorial:init()
            setup_options(true)
            --ProfileList.showCutscene = 0
        else
            Profile:new()
        end
    else
        Profile:new()
    end
end

function ProfileList:delete(index)
    ProfileList.fds[index]:unlock()
    ProfileList.fds[index]:remove(ProfileList.data.profiles[index].filename)
    ProfileList.fds[index] = nil
    ProfileList.data.last = ProfileList.data.current
    ProfileList.data.current = index
    ProfileList:checkList()
    ProfileList:flush()
end

function ProfileList:switchProfile(index)
    ProfileList.data.last = ProfileList.data.current
    ProfileList.data.current = index
    ProfileList:checkList()

    if ProfileList.data.current > 0 then
        Profile:new()
        Profile:load(ProfileList.fds[index])
        HOLevel:cleanAll()
        Levels:reset()
        Location:resetAll()
        Tutorial:init()
        setup_options(true)
    end
end

function ProfileList:hasCurrent()
    return (ProfileList.data.current > 0)
end

function ProfileList:getCurrentName()
    return ProfileList.data.profiles[ProfileList.data.current].name
end

function ProfileList:hasList()
    return (ProfileList.data.profiles ~= nil and #ProfileList.data.profiles > 0)
end

function ProfileList:checkList()
    if ProfileList.data.profiles == nil or #ProfileList.data.profiles == 0 then
        return
    end

    local list = clone_table(ProfileList.data.profiles)
    local fds = clone_table(ProfileList.fds)
    ProfileList.data.profiles = {}
    ProfileList.fds = {}

    for i = 1, #list do
        if fds[i] ~= nil then
            table.insert(ProfileList.data.profiles, list[i])
            table.insert(ProfileList.fds, fds[i])
        else
				if #list == 1 then
					ProfileList.data.current = -1
				elseif #list >= ProfileList.data.current + 1 then
					--pass
				elseif #list >= ProfileList.data.current then
					ProfileList.data.current = ProfileList.data.current - 1
				else
					ProfileList.data.current = ProfileList.data.current - 1
                end
        end
    end
end

function ProfileList:add(aName)
    if ProfileList.data.profiles == nil then
        ProfileList.data.profiles = {}
    end

    if #ProfileList.data >= 5 then
        return 'ids_profile_list_full'
    end

    for i = 1, #ProfileList.data.profiles do
        local prof_rec = ProfileList.data.profiles[i]
        if prof_rec.name == aName then
            return 'ids_name_exists'
        end
    end

    local rec = {}
    rec.name = aName
    rec.filename = aName..'.txt'
   
    table.insert(ProfileList.data.profiles, rec)
    ProfileList.data.current = #ProfileList.data.profiles
    Profile:new()
    ProfileList:saveCurrent()

    --ProfileList.showCutscene = 1

    ProfileList:switchProfile(ProfileList.data.current)
    setup_options(false)
    return nil
end

function ProfileList:saveCurrent()

    if ProfileList.data.current == nil or ProfileList.data.current <= 0 then
        return
    end

    ProfileList.data.profiles[ProfileList.data.current].level = Profile.data.level
    ProfileList.data.profiles[ProfileList.data.current].time = get_sys_time()


    if ProfileList.fds[ProfileList.data.current] == nil then
        if ProfileList.data.last and ProfileList.data.last ~= -1 and ProfileList.fds[ProfileList.data.last] then
            ProfileList.fds[ProfileList.data.last]:unlock()
        end
        ProfileList.fds[ProfileList.data.current] = create_file_locker()
        local prof_rec = ProfileList.data.profiles[ProfileList.data.current]
        ProfileList.fds[ProfileList.data.current]:lock(prof_rec.filename)
    end
    
    
    if HOLevel.current then
        Profile:serializeFromLocation(HOLevel.current.mLocation.mName)
	elseif HOLevelExtra.mName then
		Profile:serializeFromLocation(HOLevelExtra.mName)
    end

    Profile:save(ProfileList.fds[ProfileList.data.current])

    ProfileList:flush()
end
