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

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

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

        public void Edit(CmInvestigation2History data) {
            using (var trans = new TransactionScope()) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "investigation2");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                    data.CmFile.SiteId = data.SiteId;
                }

                var persist = _context.CmInvestigation2History
                                .Where(x => x.HistoryId == data.HistoryId && x.SiteId == data.SiteId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }
                //persist.SiteId = data.SiteId;
                //persist.InvestigationId = data.InvestigationId;
                persist.Description = data.Description;
                persist.UpdatedDate = DateTime.Now;
                persist.UpdatedUserId = data.UpdatedUserId;
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    persist.CmFile = data.CmFile;
                }
                _context.CmInvestigation2History.Update(persist);
                _context.SaveChanges();

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

                trans.Complete();
            }
        }

        public void Delete(int siteId, int InvestigationId, int historyId) {
            var data = _context.CmInvestigation2History.First(x => x.SiteId == siteId && x.InvestigationId == InvestigationId && x.HistoryId == historyId);
            _context.CmInvestigation2History.Remove(data);
            _context.SaveChanges();
        }

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


        public IQueryable<CmInvestigation2History> Get(int siteId, int InvestigationId, int historyId) {
            var data = _context.CmInvestigation2History.Where(x => x.SiteId == siteId && x.InvestigationId == InvestigationId && x.HistoryId == historyId);
            return data;
        }

        public int GetRevisionNo(int siteId, int InvestigationId) {
            var data = _context.CmInvestigation2History.Where(x => x.SiteId == siteId && x.InvestigationId == InvestigationId).Max(x => x.RevisionNo);
            data += 1;
            return data.GetValueOrDefault();
        }

        public void Create(CmInvestigation2History data) {
            using (var trans = new TransactionScope()) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    data.CmFile.SiteId = data.SiteId;
                    var category = _context.CmFileCategory.First(x => x.Name == "investigation2");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile = null;
                }
                data.UpdatedDate = DateTime.Now;

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

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile.Path, data.CmFile);
                }

                trans.Complete();
            }
        }
    }
}