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 DrawingHistoryService {
        private readonly ILogger<DrawingHistoryService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

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

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

                var persist = _context.FmsDrawingHistory
                                .Where(x => x.HistoryId == data.HistoryId && x.SiteId == data.SiteId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }
                persist.SiteId = data.SiteId;
                persist.DrawingId = data.DrawingId;
                persist.Description = data.Description;
                persist.UpdatedDate = DateTime.Now;
                persist.UpdatedUserId = data.UpdatedUserId;
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    persist.CmFile = data.CmFile;
                }
                _context.FmsDrawingHistory.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 drawingId, int historyId) {
            var data = _context.FmsDrawingHistory.First(x => x.SiteId == siteId && x.DrawingId == drawingId && x.HistoryId == historyId);
            _context.FmsDrawingHistory.Remove(data);
            _context.SaveChanges();
        }

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


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

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

        public void Create(FmsDrawingHistory 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 == "drawing");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile = null;
                }
                data.UpdatedDate = DateTime.Now;

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

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

                trans.Complete();
            }
        }
    }
}