using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using ElectroShop.DAL;
using ElectroShop.DAL.Entities;
using ElectroShop.Models;
using ElectroShop.ViewModels;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace ElectroShop.Services
{
    public class ElectronicComponentService
    {
        private readonly ElectroShopDBContext context;

        public ElectronicComponentService(ElectroShopDBContext context)
        {
            this.context = context;
        }

        public IQueryable<ElectronicComponentModel> GetAllQueriable()
        {
            return context.ElectronicComponents.OrderBy(x => x.Name).Select(
                s => new ElectronicComponentModel
                {
                    Id = s.Id,
                    Name = s.Name,
                    ParametersValues = s.ParametersValues,
                    NumberAvailable = s.NumberAvailable,
                    ImgBase64 = s.ImgBase64,
                    ElectronicComponentCategoryId = s.ElectronicComponentCategoryId,
                    ElectronicComponentCategory = new ElectronicComponentCathegoryModel(s.ElectronicComponentCategory),
                    DatasheetId = s.DatasheetId,
                    ImgId = s.ImgId
                }
                );
        }
        //vrati objekt soucastky podle id
        public ElectronicComponentModel GetById(int id)
        {
            return context.ElectronicComponents.OrderBy(x => x.Name)
                .Where(x => x.Id == id).Select(
                s => new ElectronicComponentModel
                {
                    Id = s.Id,
                    Name = s.Name,
                    ParametersValues = s.ParametersValues,
                    NumberAvailable = s.NumberAvailable,
                    ImgBase64 = s.ImgBase64,
                    ElectronicComponentCategoryId = s.ElectronicComponentCategoryId,
                    ElectronicComponentCategory = new ElectronicComponentCathegoryModel(s.ElectronicComponentCategory),
                    DatasheetId = s.DatasheetId,
                    ImgId = s.ImgId
                }
                ).FirstOrDefault();
        }
        //vytvari novy objekt tridy ElectronicComponentModel
        public void Create(ElectronicComponentModel vm)
        {
            var entity = new ElectronicComponent()
            {
                Name = vm.Name,
                ParametersValues = vm.ParametersValues,
                NumberAvailable = vm.NumberAvailable,
                ImgBase64 = vm.ImgBase64,
                DatasheetId = vm.DatasheetId,
                ImgId = vm.ImgId,
                ElectronicComponentCategoryId = vm.ElectronicComponentCategoryId

            };

            context.ElectronicComponents.Add(entity);
            context.SaveChanges();
        }
        //aktualizue udaje soucastky
        public void Update(ElectronicComponentModel vm)
        {
            var entity = context.ElectronicComponents.FirstOrDefault(s => s.Id == vm.Id);

            entity.Name = vm.Name;
            entity.ParametersValues = vm.ParametersValues;
            entity.NumberAvailable = vm.NumberAvailable;
            entity.ImgBase64 = vm.ImgBase64;
            entity.ElectronicComponentCategoryId = vm.ElectronicComponentCategoryId;
            entity.DatasheetId = vm.DatasheetId;
            entity.ImgId = vm.ImgId;

            context.SaveChanges();
        }

        //maze objekt soucastky z databaze
        public void Delete(ElectronicComponentModel vm)
        {
            var entity = context.ElectronicComponents.FirstOrDefault(s => s.Id == vm.Id);

            context.ElectronicComponents.Remove(entity);

            context.SaveChanges();
        }

        //vrati true jestli komponenta existuje v databazi. Jako vstup prijima nazev komponenty.
        public bool ContainsAlreadyAName(string name)
        {
            return context.ElectronicComponents.Where(x => x.Name == name).Any();
        }
    }
}
