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

namespace BankDatabaseImplement.Implements
{
    public class TransferStorage : ITransferStorage
    {
        public List<TransferViewModel> GetFullList()
        {
            using var context = new BankDatabase();
            return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<TransferViewModel> GetFilteredList(TransferSearchModel model)
        {
            if (!model.Id.HasValue && !model.OperatorId.HasValue)
            {
                return new();
            }
            if (model.OperatorId.HasValue)
            {
                using var context = new BankDatabase();
                return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
                .Where(x => x.OperatorId == model.OperatorId)
                .Select(x => x.GetViewModel)
                .ToList();
            }
            else
            {
                using var context = new BankDatabase();
                return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
                .Where(x => x.Id == model.Id)
                .Select(x => x.GetViewModel)
                .ToList();
            }
        }
        public TransferViewModel? GetElement(TransferSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new BankDatabase();
            return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
            .FirstOrDefault(x => x.Id == model.Id)
            ?.GetViewModel;
        }
        public TransferViewModel? Insert(TransferBindingModel model)
        {
            using var context = new BankDatabase();
            var newTransfer = Transfer.Create(context, model);
            if (newTransfer == null)
            {
                return null;
            }
            context.Transfers.Add(newTransfer);
            context.SaveChanges();
            return newTransfer.GetViewModel;
        }
        public TransferViewModel? Update(TransferBindingModel model)
        {
            using var context = new BankDatabase();
            var transfer = context.Transfers.FirstOrDefault(x => x.Id == model.Id);
            if (transfer == null)
            {
                return null;
            }
            transfer.Update(model);
            context.SaveChanges();
            return transfer.GetViewModel;
        }
        public TransferViewModel? Delete(TransferBindingModel model)
        {
            using var context = new BankDatabase();
            var element = context.Transfers.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Transfers.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
