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

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

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

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

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

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

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

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

            persist.SiteId = data.SiteId;
            persist.CompanyId = data.CompanyId;
            persist.DepartmentId = data.DepartmentId;
            persist.Name = data.Name;
            persist.ShortName = data.ShortName;
            persist.FaxNo = data.FaxNo;
            persist.PhoneNo = data.PhoneNo;
            persist.Comment = data.Comment;
            persist.IsUse = data.IsUse;
            _context.CmDepartment.Update(persist);
            _context.SaveChanges();
        }

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