using System.Linq;
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace LobbyServer
{
    class ScoresManager
    {
        public const string FileName = "scores.json";
        public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName);

        private List<ScoreEntry> _scores;
        public IReadOnlyList<ScoreEntry> Scores => _scores.AsReadOnly();

        public ScoresManager()
        {
            if (File.Exists("scores.json"))
            {
                _scores = JsonConvert.DeserializeObject<List<ScoreEntry>>(File.ReadAllText(FilePath));
            }
            else
            {
                _scores = new List<ScoreEntry>();
            }
        }

        public void Add(ScoreEntry entry)
        {
            _scores.Add(entry);
            _scores.OrderByDescending(x => x.Score);
        }

        public void Save()
        {
            File.WriteAllText(FilePath, JsonConvert.SerializeObject(_scores));
        }
    }
}