using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaseAccountingDataBaseImplement.Implements
{
    public class UserStorage : IUserStorage
    {
        public UserViewModel? Delete(UserBindingModel model)
        {
            using var context = new CaseAccountingDatabase();
            var element = context.Users
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element == null)
            {
                return null;
            }
            context.Users.Remove(element);
            context.SaveChanges();
            return element.GetViewModel;
        }

        public UserViewModel? GetElement(UserSearchModel model)
        {
            using var context = new CaseAccountingDatabase();
            return context.Users
                .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
                (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login &&
                !string.IsNullOrEmpty(model.Password) && x.Password == model.Password &&
                x.Role == model.Role))
                ?.GetViewModel;
        }

        public List<UserViewModel> GetFilteredList(UserSearchModel model)
        {
            using var context = new CaseAccountingDatabase();
            if (model.Id.HasValue)
            {
                return context.Users
                    .Where(x => x.Id == model.Id)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else
            {
                return new();
            }
        }

        public List<UserViewModel> GetFullList()
        {
            using var context = new CaseAccountingDatabase();
            return context.Users
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public UserViewModel? Insert(UserBindingModel model)
        {
            var newUser = User.Create(model);
            if (newUser == null)
            {
                return null;
            }

            using var context = new CaseAccountingDatabase();

            context.Users.Add(newUser);
            context.SaveChanges();
            return context.Users
                         .FirstOrDefault(x => x.Id == newUser.Id)
                         ?.GetViewModel;
        }

        public UserViewModel? Update(UserBindingModel model)
        {
            using var context = new CaseAccountingDatabase();

            var user = context.Users
                .FirstOrDefault(x => x.Id == model.Id);

            if (user == null)
            {
                return null;
            }
            user.Update(model);
            context.SaveChanges();
            return context.Users
                          .FirstOrDefault(x => x.Id == model.Id)
                          ?.GetViewModel;
        }
    }
}
