﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using GamesBarn.Models;
using GamesBarn.Repository.Configuration;
using MongoDB.Driver;

namespace GamesBarn.Repository
{
    public class GameRepository
    {
        private const string CST_GAME_COLLECTION_NAME = "Games";

        private readonly IMongoCollection<Game> _games;

        public GameRepository(MongoDbSettings settings)
        {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);
            _games = database.GetCollection<Game>(CST_GAME_COLLECTION_NAME);
        }

        public Game FindById(string id) => _games.Find(g => g.Id == id).FirstOrDefault();

        public List<Game> Get() => _games.Find(g => true).ToList();

        public List<Game> Get(Game criteria)
        {
            var filter = Builders<GameDBO>

            return _games.Find(criteria).ToList();
        }

        public Game Insert(Game gameIn)
        {
            var gameDBO =

            _games.InsertOne(gameIn);
            return gameIn;
        }

        public void Update(string id, Game gameIn)
        {
            _games.ReplaceOne(g => g.Id == id, gameIn);
        }

        public void Delete(string id) => _games.DeleteOne(g => g.Id == id);

        public void Delete(Game gameIn) => Delete(gameIn.Id);
    }
}
