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 CathegoryService
    {
        private readonly ElectroShopDBContext context;

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

        //vrati seznam vsech kategorii soucastek
        public List<ElectronicComponentCathegoryModel> GetAllCategories()
        {
            return context.ElectronicComponentCathegories.OrderBy(x => x.Name).Select(
                s => new ElectronicComponentCathegoryModel
                {
                    Id = s.Id,
                    Name = s.Name,
                    ParametersNames = s.ParametersNames
                }
                ).ToList();
        }

        //vrati seznam nazvu vsech kategorii
        public List<string> GetAllCategoriesNames()
        {
            return context.ElectronicComponentCathegories.OrderBy(x => x.Name).Select(
                s => s.Name
                ).ToList();
        }


        //vrati kategorii podle nazvu
        public ElectronicComponentCathegoryModel GetCathegoryByName(string name)
        {
            return context.ElectronicComponentCathegories
                .Where(x => x.Name == name).Select(
                s => new ElectronicComponentCathegoryModel
                {
                    Id = s.Id,
                    Name = s.Name,
                    ParametersNames = s.ParametersNames
                }
                ).FirstOrDefault();
        }

        //vytvori kategorii soucastky s parametrem (parametr - parsovany string)
        public void Create(ElectronicComponentCathegoryModel vm)
        {
            var cathegory = new ElectronicComponentCathegory()
            {
                Name = vm.Name,
                ParametersNames = vm.ParametersNames

            };

            context.ElectronicComponentCathegories.Add(cathegory);
            context.SaveChanges();
        }

        //dotaz na databazi zda nazev kategorii jiz existuje. Vrati true nebo false;

        public bool ContainsAlreadyAName(string name)
        {
            return context.ElectronicComponentCathegories.Where(x => x.Name == name).Any();
        }

    }
}
