using System.Linq;

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

namespace FMSApp.Services {
    public class ContractManagementService {
        private readonly ILogger<ContractManagementService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

        public ContractManagementService(
            ILogger<ContractManagementService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }

        public void Create(FmsContract data) {
            _context.FmsContract.Add(data);
            _context.SaveChanges();
        }

        public void Save(FmsContract data) {
            var persist = _context.FmsContract
                            .Where(x => x.ContractId == data.ContractId && x.SiteId == data.SiteId).FirstOrDefault();

            if (persist == null) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    data.CmFile.SiteId = data.SiteId;
                    var category = _context.CmFileCategory.First(x => x.Name == "contract");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile = null;
                }

                _context.FmsContract.Add(data);
                _context.SaveChanges();

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile.Path, data.CmFile);
                }
            } else {
                if (data.CmFile != null && data.CmFile.IsDelete) {
                    persist.CmFile = null;
                    persist.FileId = null;
                } else if (data.CmFile != null && data.CmFile.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "contract");
                    persist.CmFile = new CmFile {
                        SiteId = data.SiteId,
                        FileCategory = category,
                        CreatedDate = DateTime.Now,
                        Name = data.CmFile.Name,
                        FileSize = data.CmFile.FileSize,
                        ContentType = data.CmFile.ContentType,
                    };
                }

                persist.PartnerId = data.PartnerId;
                persist.PartnerTypeId = data.PartnerTypeId;
                persist.SiteId = data.SiteId;
                persist.Name = data.Name;
                persist.ContractDate = data.ContractDate;
                persist.StartDate = data.StartDate;
                persist.EndDate = data.EndDate;
                persist.OwnerShipName = data.OwnerShipName;
                persist.OwnerShipPhoneNo = data.OwnerShipPhoneNo;
                persist.CommitmentMan = data.CommitmentMan;
                persist.ContractClassId = data.ContractClassId;
                persist.PaymentTypeId = data.PaymentTypeId;
                persist.ContractTypeId = data.ContractTypeId;
                persist.ContractMethodId = data.ContractMethodId;
                persist.Comment = data.Comment;
                persist.ContractAmountText = data.ContractAmountText;
                persist.ContractExeGuaranteeRate = data.ContractExeGuaranteeRate;
                persist.DefectGuaranteePeriodText = data.DefectGuaranteePeriodText;
                persist.DefectGuaranteeRate = data.DefectGuaranteeRate;

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

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
                    if (persist.CmFile != null) {
                        // 파일 삭제시 진짜 DB삭제?
                    }
                }

            }
        }

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

        public FmsContract Get(int id, int siteId) {
            var data = _context.FmsContract.Single(x => x.ContractId == id && x.SiteId == siteId);
            return data;
        }

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

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