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

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

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

        public IQueryable<FmsPaymentType> Get(int paymentTypeId) {
            var data = _context.FmsPaymentType.Where(x => x.PaymentTypeId == paymentTypeId);
            return data;
        }

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

        public void Edit(int paymentTypeId, FmsPaymentType data) {

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

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

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

        public void Delete(int paymentTypeId) {
            var data = _context.FmsPaymentType.First(x => x.PaymentTypeId == paymentTypeId);
            //data.IsUse = false;

            _context.FmsPaymentType.Remove(data);  //실제 행이 삭제될 경우  처리
            _context.SaveChanges();
        }

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

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