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

namespace FMSAdmin.Services {
    public class ComplaintService {
        private readonly ILogger<ComplaintService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        public ComplaintService(
            ILogger<ComplaintService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }
        public void Create(FmsComplaints data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile1.SiteId = data.SiteId;
                    data.CmFile1.FileCategory = category;
                    data.CmFile1.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile1 = null;
                }
                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile2.SiteId = data.SiteId;
                    data.CmFile2.FileCategory = category;
                    data.CmFile2.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile2 = null;
                }
                if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile3.SiteId = data.SiteId;
                    data.CmFile3.FileCategory = category;
                    data.CmFile3.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile3 = null;
                }
                _context.FmsComplaints.Add(data);
                _context.SaveChanges();
                if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile1.Path, data.CmFile1);
                }
                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile2.Path, data.CmFile2);
                }
                if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile3.Path, data.CmFile3);
                }

                transaction.Commit();
            }
        }

        public void Edit(int id, int siteId, FmsComplaints data) {

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

                if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile1.FileCategory = category;
                    data.CmFile1.CreatedDate = DateTime.Now;
                }

                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile2.FileCategory = category;
                    data.CmFile2.CreatedDate = DateTime.Now;
                }

                if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "complaint");
                    data.CmFile3.FileCategory = category;
                    data.CmFile3.CreatedDate = DateTime.Now;
                }

                persist.SiteId = data.SiteId;
                persist.ComplaintId = data.ComplaintId;
                persist.ComplaintTypeId = data.ComplaintTypeId;
                persist.Name = data.Name;
                persist.StartDate = data.StartDate;
                persist.EndDate = data.EndDate;
                persist.DepartmentId = data.DepartmentId;
                persist.ComplaintLocation = data.ComplaintLocation;
                persist.Action = data.Action;
                persist.Reason = data.Reason;
                persist.Damage = data.Damage;
                persist.Measurement = data.Measurement;
                persist.CompanyId = data.CompanyId;
                persist.Firstwitness = data.Firstwitness;

                if (data.CmFile1 != null) {
                    if (data.CmFile1.IsUpload) {
                        persist.CmFile1 = data.CmFile1;
                    }
                    if (data.CmFile1.IsDelete) {
                        persist.FileId1 = null;
                        persist.CmFile1 = null;
                    }
                }
                if (data.CmFile2 != null) {
                    if (data.CmFile2.IsUpload) {
                        persist.CmFile2 = data.CmFile2;
                    }
                    if (data.CmFile2.IsDelete) {
                        persist.FileId2 = null;
                        persist.CmFile2 = null;
                    }
                }
                if (data.CmFile3 != null) {
                    if (data.CmFile3.IsUpload) {
                        persist.CmFile3 = data.CmFile3;
                    }
                    if (data.CmFile3.IsDelete) {
                        persist.FileId3 = null;
                        persist.CmFile3 = null;
                    }
                }
                _context.FmsComplaints.Update(persist);
                _context.SaveChanges();

                if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile1.Path, persist.CmFile1);
                }
                if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile2.Path, persist.CmFile2);
                }
                if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile3.Path, persist.CmFile3);
                }
                transaction.Commit();
            }
        }
        public void Delete(int id, int siteId) {
            var data = _context.FmsComplaints.First(x => x.ComplaintId == id && x.SiteId == siteId);
            _context.FmsComplaints.Remove(data);
            _context.SaveChanges();
        }

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

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

    }
}