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

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

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

        public IQueryable<FmsMaterialTradeCompany> Gets(int id, int siteId) {
            var data = _context.FmsMaterialTradeCompany.Where(x => x.TradeCompanyId == id && x.SiteId == siteId);
            return data;
        }

        public void Create(FmsMaterialTradeCompany data) {
            var persist2 = _context.FmsMaterialTradeCompany
            .Where(x => x.SiteId == data.SiteId && x.RegistrationNo == data.RegistrationNo).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }

            _context.FmsMaterialTradeCompany.Add(data);
            _context.SaveChanges();
        }

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

            var persist2 = _context.FmsMaterialTradeCompany
            .Where(x => x.TradeCompanyId != id && x.SiteId == siteId && x.RegistrationNo == data.RegistrationNo).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }

            persist.SiteId = data.SiteId;
            persist.TradeCompanyId = data.TradeCompanyId;
            persist.Name = data.Name;
            persist.RegistrationNo = data.RegistrationNo;
            persist.RepresentativeName = data.RepresentativeName;
            persist.PhoneNo = data.PhoneNo;
            persist.FaxNo = data.FaxNo;
            persist.SalesName = data.SalesName;
            persist.SalesPhoneNo = data.SalesPhoneNo;
            persist.AddressZip1 = data.AddressZip1;
            persist.AddressZip2 = data.AddressZip2;
            persist.Address1 = data.Address1;
            persist.Adderss2 = data.Adderss2;
            persist.HomepageUri = data.HomepageUri;
            persist.Note = data.Note;
            persist.IsUse = data.IsUse;
            _context.FmsMaterialTradeCompany.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(int id, int siteId) {
            var data = _context.FmsMaterialTradeCompany.First(x => x.TradeCompanyId == id && x.SiteId == siteId);
            _context.FmsMaterialTradeCompany.Remove(data);
            _context.SaveChanges();
        }

        public void ImportExcel(IList<FmsMaterialTradeCompany> list) {
            using (var transaction = _context.Database.BeginTransaction()) {
                _context.FmsMaterialTradeCompany.AddRange(list);
                _context.SaveChanges();

                transaction.Commit();
            }
        }
    }
}