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

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

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

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

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


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

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

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

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

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

                trans.Complete();
            }
        }
    }
}