using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using FMSAdmin.Models;

namespace FMSAdmin.Services {
    public class ContractClassService {
        private readonly ILogger<ContractClassService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

        public ContractClassService(
            ILogger<ContractClassService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }

        public IQueryable<FmsContractClass> GetAll() {
            var query = _context.FmsContractClass;
            return query;
        }

        public IQueryable<FmsContractClass> Get(int contractClassId) {
            var data = _context.FmsContractClass.Where(x => x.ContractClassId == contractClassId);
            return data;
        }

        public void Create(FmsContractClass data) {
            //data.IsUse = true;
            _context.FmsContractClass.Add(data);
            _context.SaveChanges();
        }

        public void Edit(int contractClassId, FmsContractClass data) {

            var persist = _context.FmsContractClass
                            .Where(x => x.ContractClassId == contractClassId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            //persist.SiteId = data.SiteId;
            persist.ContractClassId = data.ContractClassId;
            persist.Name = data.Name;
            persist.IsUse = data.IsUse;

            _context.FmsContractClass.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(int contractClassId) {
            var data = _context.FmsContractClass.First(x => x.ContractClassId == contractClassId);
            //data.IsUse = false;

            _context.FmsContractClass.Remove(data);
            _context.SaveChanges();
        }

        // public void AddCount(int siteId, int contractClassId) {
        //     var persist = _context.FmsContractClass
        //                     .Where(x => x.ContractClassId == contractClassId && x.SiteId == siteId).FirstOrDefault();
        //     if (persist == null) {
        //         throw new ServiceException("정보를 찾을 수 없습니다.");
        //     }

        //     persist.ReadCount += 1;
        //
        //     _context.FmsContractClass.Update(persist);
        //     _context.SaveChanges();
        // }
    }
}