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

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

        public SitePriceHistoryService(
            ILogger<SitePriceHistoryService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }

        public void Save(BemsSitePriceHistory[] data) {
            using (var trans = new TransactionScope()) {

                foreach (BemsSitePriceHistory row in data) {

                    var persist = _context.BemsSitePriceHistory
                                    .Where(x => x.SiteId == row.SiteId && x.PayDate == row.PayDate && x.FuelTypeId == row.FuelTypeId && x.PriceTypeId == row.PriceTypeId && x.PriceCode == row.PriceCode).FirstOrDefault();

                    var persist2 = _context.BemsPriceCode.Where(x => x.FuelTypeId == row.FuelTypeId && x.PriceCode == row.PriceCode).FirstOrDefault();

                    if (!persist2.Unit.Equals("성명") && !persist2.Unit.Equals("날짜")) {
                        try {
                            Convert.ToDecimal(Util.IsNullOrEmpty(row.PriceValue) ? "0" : row.PriceValue);
                        } catch {
                            throw new ServiceException("잘못된 형식입니다.(ex>형식 1,000.00)");
                        }
                    }

                    if (persist == null) {
                        _context.BemsSitePriceHistory.Add(row);
                        _context.SaveChanges();
                    } else {
                        persist.PriceValue = row.PriceValue;
                        //
                        _context.SaveChanges();
                    }
                }
                trans.Complete();
            }
        }


        public IQueryable<BemsSitePriceHistory> GetAll() {
            var query = _context.BemsSitePriceHistory/*.AsNoTracking()*/;
            return query;
        }
        public void Delete(int siteId, int fuelTypeId, int priceTypeId) {
            using (var trans = new TransactionScope()) {
                var data = _context.BemsSitePriceHistory.First(x => x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.PriceTypeId == priceTypeId);
                //data.IsUse = false;
                _context.BemsSitePriceHistory.Remove(data);
                _context.SaveChanges();
                trans.Complete();
            }
        }

        public IQueryable<BemsFuelType> GetFuelType(int? siteId) {
            siteId = siteId == null ? 0 : siteId;
            //var data = _context.BemsFuelType.Where(x => x.FuelTypeId != 0);
            var data = _context.BemsSitePrice.Where(x => x.SiteId == siteId).Select(x => x.FuelType);
            return data;
        }

        public IQueryable<BemsSitePrice> GetPriceCode(int siteId, int fuelTypeId, int priceTypeId) {
            var data = _context.BemsSitePrice.Where(x => x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.PriceTypeId == priceTypeId && x.UseYn == "Y" && !x.PriceCodeNavigation.Unit.Contains("성명") && !x.PriceCodeNavigation.Unit.Contains("날짜"));

            return data;
        }
    }
}