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 Investigation1Service {
        private readonly ILogger<Investigation1Service> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        public Investigation1Service(
            ILogger<Investigation1Service> logger,
            FMSContext context,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }
        public void Create(CmInvestigation1 data) {
            using (var trans = new TransactionScope()) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    var category = _context.CmFileCategory.First(x => x.Name == "investigation1");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                    data.CmFile.SiteId = data.SiteId;
                } else {
                    data.CmFile = null;
                }
                data.CmInvestigation1History.Add(new CmInvestigation1History {
                    SiteId = data.SiteId,
                    RevisionNo = 0,
                    Description = data.Description,
                    CmFile = data.CmFile,
                    UpdatedDate = DateTime.Now,
                    UpdatedUserId = data.CreateUserId
                });

                _context.CmInvestigation1.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, CmInvestigation1 data) {
            using (var trans = new TransactionScope()) {
                var persist = _context.CmInvestigation1
                                .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.CmInvestigation1.Update(persist);
                _context.SaveChanges();

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

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

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

    }
}