using System;
using System.Linq;
using FMSAdmin.Data;
using FMSAdmin.Entities;
using FMSAdmin.Helpers;
using Microsoft.Extensions.Logging;

namespace FMSAdmin.Services {
    public class OrganizationChartStatusService {
        private readonly ILogger<OrganizationChartStatusService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        public OrganizationChartStatusService(
            ILogger<OrganizationChartStatusService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }

        public void Create(CmOrganizationChartStatus data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                checkName(data);
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "organizationChartStatus");
                    data.CmFile.SiteId = data.SiteId;
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile = null;
                }
                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "organizationChartStatus");
                    data.CmFile2.SiteId = data.SiteId;
                    data.CmFile2.FileCategory = category;
                    data.CmFile2.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile2 = null;
                }

                data.AddDate = DateTime.Now;

                _context.CmOrganizationChartStatus.Add(data);
                _context.SaveChanges();
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    _storage.CopyEntity(data.CmFile.Path, data.CmFile);
                    _storage.PdfConvertImage(data.CmFile);
                }

                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    _storage.CopyEntity(data.CmFile2.Path, data.CmFile2);
                }

                transaction.Commit();
            }
        }
        public int checkName(CmOrganizationChartStatus data) {
            var check = _context.CmOrganizationChartStatus.Where(x => x.SiteId == data.SiteId && x.Month == data.Month && x.Year == data.Year).Count();
            if (check > 0) {
                throw new ServiceException(data.Year + "년 " + data.Month + "월 정보가 이미 존재합니다.");
            }
            return check;
        }
        public void Edit(int id, int siteId, CmOrganizationChartStatus data) {

            using (var transaction = _context.Database.BeginTransaction()) {
                var persist = _context.CmOrganizationChartStatus
                                .Where(x => x.OrganizationChartStatusId == id && x.SiteId == siteId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }

                if (!persist.Year.Equals(data.Year) && !persist.Month.Equals(data.Month))
                    checkName(data);

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "organizationChartStatus");
                    data.CmFile.SiteId = data.SiteId;
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                    persist.CmFile = data.CmFile;
                }
                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "organizationChartStatus");
                    data.CmFile2.SiteId = data.SiteId;
                    data.CmFile2.FileCategory = category;
                    data.CmFile2.CreatedDate = DateTime.Now;
                    persist.CmFile2 = data.CmFile2;
                }
                if (data.CmFile2 != null && data.CmFile2.IsDelete) {
                    persist.CmFile2 = null;
                    persist.FileId2 = null;
                }
                persist.Year = data.Year;
                persist.Month = data.Month;
                persist.SiteId = data.SiteId;
                persist.UpdateDate = DateTime.Now;

                _context.CmOrganizationChartStatus.Update(persist);
                _context.SaveChanges();

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
                    _storage.PdfConvertImage(data.CmFile);
                }

                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    _storage.CopyEntity(data.CmFile2.Path, persist.CmFile2);
                }

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

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

        public IQueryable<CmOrganizationChartStatus> Get(int id, int siteId) {
            var data = _context.CmOrganizationChartStatus.Where(x => x.OrganizationChartStatusId == id && x.SiteId == siteId);
            return data;
        }
    }
}