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

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

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

        public IQueryable<FmsContractMethod> Get(int contractMethodId) {
            var data = _context.FmsContractMethod.Where(x => x.ContractMethodId == contractMethodId);
            return data;
        }

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

        public void Edit(int contractMethodId, FmsContractMethod data) {

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

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

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

        public void Delete(int contractMethodId) {
            var data = _context.FmsContractMethod.First(x => x.ContractMethodId == contractMethodId);
            //data.IsUse = false;

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

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

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