using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;

namespace UniversityDataBaseImplemet.Implements
{
    public class DisciplineStorage:IDisciplineStorage
    {
        public DisciplineViewModel? GetElement(DisciplineSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Discipline
                .FirstOrDefault(record => record.Id == model.Id
                    || record.Name.Equals(model.Name))
                ?.GetViewModel;
        }
        public List<DisciplineViewModel> GetFilteredList(DisciplineSearchModel model)
        {
            using var context = new Database();
            if (model.Id.HasValue)
            {
                return context.Discipline
                    .Where(record => record.Id.Equals(model.Id))
                    .Select(record => record.GetViewModel)
                    .ToList();
            }
            else if (model.UserId.HasValue)
            {
                return context.Discipline
                   .Where(record => record.UserId == model.UserId)
                   .Select(record => record.GetViewModel)
                   .ToList();
            }
            else
            {
                return new();
            }
        }
        public List<DisciplineViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Discipline
                .Select(record => record.GetViewModel)
                .ToList();
        }
        public DisciplineViewModel? Insert(DisciplineBindingModel model)
        {
            var newDiscipline = Discipline.Create(model);
            if (newDiscipline == null)
            {
                return null;
            }
            using var context = new Database();
            context.Discipline.Add(newDiscipline);
            context.SaveChanges();
            return newDiscipline.GetViewModel;
        }
        public DisciplineViewModel? Update(DisciplineBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var stream = context.Discipline
                    .FirstOrDefault(record => record.Id.Equals(model.Id));
                if (stream == null)
                {
                    return null;
                }
                stream.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return stream.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public DisciplineViewModel? Delete(DisciplineBindingModel model)
        {
            using var context = new Database();
            var stream = context.Discipline
                .FirstOrDefault(record => record.Id.Equals(model.Id));
            if (stream == null)
            {
                return null;
            }
            context.Discipline.Remove(stream);
            context.SaveChanges();
            return stream.GetViewModel;
        }
    }
}
