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

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

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

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

        public void Create(CmCompany data) {
            var persist2 = _context.CmCompany
            .Where(x => x.SiteId == data.SiteId && x.CompanyTypeId == data.CompanyTypeId && x.Name == data.Name).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다. (회사구분,회사명칭 확인 필요)");
            }

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

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

            var persist2 = _context.CmCompany
            .Where(x => x.CompanyId != id && x.SiteId == siteId && x.CompanyTypeId == data.CompanyTypeId && x.Name == data.Name).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다. (회사구분,회사명칭 확인 필요)");
            }

            persist.SiteId = data.SiteId;
            persist.CompanyId = data.CompanyId;
            persist.CompanyTypeId = data.CompanyTypeId;
            persist.Name = data.Name;
            persist.PhoneNo = data.PhoneNo;
            persist.FaxNo = data.FaxNo;
            persist.RepresentativeName = data.RepresentativeName;
            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.Homepage = data.Homepage;
            persist.Comment = data.Comment;
            persist.IsUse = data.IsUse;
            _context.CmCompany.Update(persist);
            _context.SaveChanges();
        }

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