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

namespace FMSAdmin.Services {
    public class AccidentService {
        private readonly ILogger<AccidentService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        public AccidentService(
            ILogger<AccidentService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }
        public void Create(FmsAccident data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "accident");
                    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 == "accident");
                    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 == "accident");
                    data.CmFile3.SiteId = data.SiteId;
                    data.CmFile3.FileCategory = category;
                    data.CmFile3.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile3 = null;
                }
                if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "accident");
                    data.CmFile4.SiteId = data.SiteId;
                    data.CmFile4.FileCategory = category;
                    data.CmFile4.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile4 = null;
                }
                _context.FmsAccident.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);
                }
                if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile4.Path, data.CmFile4);
                }
                transaction.Commit();
            }
        }

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

            using (var transaction = _context.Database.BeginTransaction()) {
                var persist = _context.FmsAccident
                                .Where(x => x.AccidentId == 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 == "accident");
                    data.CmFile1.FileCategory = category;
                    data.CmFile1.CreatedDate = DateTime.Now;
                }

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

                if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "accident");
                    data.CmFile3.FileCategory = category;
                    data.CmFile3.CreatedDate = DateTime.Now;
                }
                if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "accident");
                    data.CmFile4.FileCategory = category;
                    data.CmFile4.CreatedDate = DateTime.Now;
                }
                persist.SiteId = data.SiteId;
                persist.AccidentId = data.AccidentId;
                persist.AccidentTypeId = data.AccidentTypeId;
                persist.Name = data.Name;
                persist.StartDate = data.StartDate;
                persist.EndDate = data.EndDate;
                persist.ClosingDate = data.ClosingDate;
                persist.DepartmentId = data.DepartmentId;
                persist.AccidentLocation = data.AccidentLocation;
                persist.Action = data.Action;
                persist.Action2 = data.Action2;
                persist.VictimName = data.VictimName;
                persist.VictimRelation = data.VictimRelation;
                persist.VictimAge = data.VictimAge;
                persist.Reason = data.Reason;
                persist.Damage = data.Damage;
                persist.Measurement = data.Measurement;
                persist.CompanyId = data.CompanyId;
                persist.Firstwitness = data.Firstwitness;
                persist.IsInsurance = data.IsInsurance;
                persist.InsuranceContent = data.InsuranceContent;

                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;
                    }
                }
                if (data.CmFile4 != null) {
                    if (data.CmFile4.IsUpload) {
                        persist.CmFile4 = data.CmFile4;
                    }
                    if (data.CmFile4.IsDelete) {
                        persist.FileId4 = null;
                        persist.CmFile4 = null;
                    }
                }
                _context.FmsAccident.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);
                }
                if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile4.Path, persist.CmFile4);
                }
                transaction.Commit();
            }
        }
        public void Delete(int id, int siteId) {
            var data = _context.FmsAccident.First(x => x.AccidentId == id && x.SiteId == siteId);
            _context.FmsAccident.Remove(data);
            _context.SaveChanges();
        }

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

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

    }
}