using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.StoragesContracts;
using ZooContracts.BindingModels;
using ZooContracts.SearchModel;
using ZooContracts.StoragesContracts;
using ZooContracts.ViewModels;
using ZooDatabaseImplements;
using ZooDatabaseImplements.Models;

namespace ZooDatabaseImplements.Implements
{
    public class EmployeeStorage : IEmployeeStorage
    {
        public List<CostViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.Employees
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<CostViewModel> GetFilteredList(EmployeeSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            if (model.Id.HasValue)
            {
                var res = GetElement(model);
                return res != null ? new() { res } : new();
            }
            if (model.EmployeeName != null)
            {
                using var context = new ZooDatabase();
                return context.Employees
                .Where(rec => rec.EmployeeName.Contains(model.EmployeeName))
                .Select(x => x.GetViewModel)
                .ToList();
            }
            return new();
        }
        public CostViewModel? GetElement(EmployeeSearchModel model)
        {
            using var context = new ZooDatabase();
            if (model.Id.HasValue)
                return context.Employees.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            if (model.EmployeeName != null && model.EmployeePassword != null)
                return context.Employees
                    .FirstOrDefault(x => x.EmployeeName.Equals(model.EmployeeName)
                                      && x.EmployeePassword.Equals(model.EmployeePassword))
                    ?.GetViewModel;
            if (model.EmployeeName != null)
                return context.Employees.FirstOrDefault(x => x.EmployeeName.Contains(model.EmployeeName))?.GetViewModel;
            return null;
        }
        public CostViewModel? Insert(EmployeeBindingModel model)
        {
            using var context = new ZooDatabase();
            var res = Employee.Create(model);
            if (res != null)
            {
                context.Employees.Add(res);
                context.SaveChanges();
            }
            return res?.GetViewModel;
        }
        public CostViewModel? Update(EmployeeBindingModel model)
        {
            using var context = new ZooDatabase();
            var res = context.Employees.FirstOrDefault(x => x.Id == model.Id);
            res?.Update(model);
            context.SaveChanges();
            return res?.GetViewModel;
        }
        public CostViewModel? Delete(EmployeeBindingModel model)
        {
            using var context = new ZooDatabase();
            var res = context.Employees.FirstOrDefault(x => x.Id == model.Id);
            if (res != null)
            {
                context.Employees.Remove(res);
                context.SaveChanges();
            }
            return res?.GetViewModel;
        }
    }
}
