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

namespace FMSAdmin.Services {
    public class DrawingService {
        private readonly ILogger<DrawingService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        public DrawingService(
            ILogger<DrawingService> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }
        public void Create(FmsDrawing 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;
                } else {
                    data.CmFile = null;
                }
                data.FmsDrawingHistory.Add(new FmsDrawingHistory {
                    SiteId = data.SiteId,
                    RevisionNo = 0,
                    Description = data.Description,
                    CmFile = data.CmFile,
                    UpdatedDate = DateTime.Now,
                    UpdatedUserId = data.CreateUserId
                });

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

                trans.Complete();
            }
        }

        public void Edit(int id, int siteId, FmsDrawing data) {
            using (var trans = new TransactionScope()) {
                var persist = _context.FmsDrawing
                                .Where(x => x.DrawingId == id && x.SiteId == siteId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }

                persist.SiteId = data.SiteId;
                persist.DrawingGroupId = data.DrawingGroupId;
                persist.DrawingTypeId = data.DrawingTypeId;
                persist.DrawingNo = data.DrawingNo;
                persist.Name = data.Name;
                persist.Description = data.Description;

                _context.FmsDrawing.Update(persist);
                _context.SaveChanges();

                trans.Complete();
            }
        }
        public void Delete(int id, int siteId) {
            var data = _context.FmsDrawing.First(x => x.DrawingId == id && x.SiteId == siteId);
            _context.FmsDrawing.Remove(data);
            _context.SaveChanges();
        }

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

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

    }
}