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

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

                _context.CmInvestigation2.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, CmInvestigation2 data) {
            using (var trans = new TransactionScope()) {
                var persist = _context.CmInvestigation2
                                .Where(x => x.InvestigationId == id && x.SiteId == siteId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }

                persist.SiteId = data.SiteId;
                persist.InvestigationGroupId = data.InvestigationGroupId;
                persist.InvestigationTypeId = data.InvestigationTypeId;
                persist.InvestigationNo = data.InvestigationNo;
                persist.Name = data.Name;
                persist.Description = data.Description;

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

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

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

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

    }
}