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 CaseStorage : ICaseStorage
    {
        public CaseViewModel? Delete(CaseBindingModel model)
        {
            using var context = new CaseAccountingDatabase();
            var element = context.Cases
                .Include(x => x.Specialization)
                .Include(x => x.User)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element == null)
            {
                return null;
            }
            context.Cases.Remove(element);
            context.SaveChanges();
            return element.GetViewModel;
        }

        public CaseViewModel? GetElement(CaseSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new CaseAccountingDatabase();
            return context.Cases
                .Include(x => x.Specialization)
                .Include(x => x.User)
                .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
                ?.GetViewModel;
        }

        public List<CaseViewModel> GetFilteredList(CaseSearchModel model)
        {
            using var context = new CaseAccountingDatabase();
            if (model.Id.HasValue)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.Id == model.Id)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.Name != null)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.Name == model.Name)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.Applicant != null)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.Applicant == model.Applicant)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.Defendant != null)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.Defendant == model.Defendant)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.Date != null)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.Date == model.Date)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.UserId.HasValue)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.UserId == model.UserId)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else if (model.SpecializationId.HasValue)
            {
                return context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .Where(x => x.SpecializationId == model.SpecializationId)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            else
            {
                return new();
            }
        }

        public List<CaseViewModel> GetFullList()
        {
            using var context = new CaseAccountingDatabase();
            return context.Cases
                .Include(x => x.Specialization)
                .Include(x => x.User)
                .Select(x => x.GetViewModel).ToList();
        }

        public CaseViewModel? Insert(CaseBindingModel model)
        {
			using var context = new CaseAccountingDatabase();
			var newCase = Case.Create(context, model);
            if (newCase == null)
            {
                return null;
            }

            context.Cases.Add(newCase);
            context.SaveChanges();
            return context.Cases
                         .Include(x => x.Specialization)
                         .Include(x => x.User)
                         .FirstOrDefault(x => x.Id == newCase.Id)
                         ?.GetViewModel;
        }

        public CaseViewModel? Update(CaseBindingModel model)
        {
            using var context = new CaseAccountingDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var _case = context.Cases
                    .Include(x => x.Specialization)
                    .Include(x => x.User)
                    .FirstOrDefault(x => x.Id == model.Id);

                if (_case == null)
                {
                    return null;
                }
                _case.Update(context, model);
                context.SaveChanges();
                _case.UpdateLawyers(context, model);
                transaction.Commit();
                return _case.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public List<HearingViewModel> GetCaseHearings(CaseSearchModel model)
        {
            if (model == null)
            {
                return new();
            }
            using var context = new CaseAccountingDatabase();
            var hearings = context.Hearings
                .Where(x => x.CaseId == model.Id)
                .Select(x => x.GetViewModel)
                .ToList();
            return hearings;
        }

        public List<SpecializationCasesViewModel>? GetSpecializationCases()
        {
            using var context = new CaseAccountingDatabase();
            var result = context.Specializations
                .GroupJoin(
                    context.Cases,
                    status => status.Id,
                    caseModel => caseModel.SpecializationId,
                    (status, caseModels) => new SpecializationCasesViewModel
                    {
                        Name = status.Name,
                        Count = caseModels.Count()
                    })
                .ToList();

            return result;
        }
    }
}
