using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseImplement.Implements
{
    public class MediaFileStorage : IMediaFileStorage
    {
        public MediaFileViewModel? Delete(MediaFileBindingModel model)
        {
            using var context = new Database();
            var element = context.MediaFiles
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.MediaFiles.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public MediaFileViewModel? GetElement(MediaFileSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.MediaFiles
            .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }

        public List<MediaFileViewModel> GetFilteredList(MediaFileSearchModel model)
        {
            if (!model.ProductId.HasValue && !model.Id.HasValue)
            {
                return new();
            }
            using var context = new Database();
            if (model.ProductId.HasValue) 
            { 
                return context.MediaFiles.Where(x => x.ProductId == model.ProductId).Select(x => x.GetViewModel).ToList();
            }
            return context.MediaFiles.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
        }

        public List<MediaFileViewModel> GetFullList()
        {
            using var context = new Database();
            return context.MediaFiles
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public MediaFileViewModel? Insert(MediaFileBindingModel model)
        {
            using var context = new Database();
            var newProduct = MediaFile.Create(model);
            if (newProduct == null)
            {
                return null;
            }
            context.MediaFiles.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }

        public MediaFileViewModel? Update(MediaFileBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.MediaFiles.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
