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 ContractTypeService {
        private readonly ILogger<ContractTypeService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

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

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

        public IQueryable<FmsContractType> Get(int contractTypeId) {
            var data = _context.FmsContractType.Where(x => x.ContractTypeId == contractTypeId);
            return data;
        }

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

        public void Edit(int contractTypeId, FmsContractType data) {

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

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

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

        public void Delete(int contractTypeId) {
            var data = _context.FmsContractType.First(x => x.ContractTypeId == contractTypeId);
            //data.IsUse = false;

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

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

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