using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using FMSAdmin.Models;
using System.Transactions;

namespace FMSApp.Services {
    public class ManualService {
        private readonly ILogger<ManualService> _logger;
        private readonly FMSContext _context;

        private readonly AppSettings _appSettings;
        private readonly StorageHelper _storage;

        public ManualService(
            ILogger<ManualService> logger,
            FMSContext context,
            IOptions<AppSettings> appSettings,
            StorageHelper storage
            ) {
            _logger = logger;
            _context = context;
            _appSettings = appSettings.Value;
            _storage = storage;
        }

        public void Save(FmsManual data) {
            using (var trans = new TransactionScope()) {

                var persist = _context.FmsManual
                                .Where(x => x.SiteId == data.SiteId
                                    && x.ManualId == data.ManualId).FirstOrDefault();
                if (persist == null) {
                    var check = _context.FmsManual.Where(x => x.SiteId == data.SiteId
                        && x.ManualId == data.ManualId).Count();
                    if (check > 0) {
                        throw new ServiceException("아이디가 중복됩니다.");
                    }

                    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.FmsManualHistory.Add(new FmsManualHistory {
                        SiteId = data.SiteId,
                        RevisionNo = 0,
                        Description = data.Description,
                        CmFile = data.CmFile,
                        UpdatedDate = DateTime.Now,
                        UpdatedUserId = data.CreateUserId
                    });
                    _context.FmsManual.Add(data);
                    _context.SaveChanges();

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

                } else {
                    persist.ManualNo = data.ManualNo;
                    persist.ManualTypeId = data.ManualTypeId;
                    persist.ManualGroupId = data.ManualGroupId;
                    persist.Name = data.Name;
                    persist.ManualNo = data.ManualNo;
                    persist.Description = data.Description;
                    _context.FmsManual.Update(persist);
                    _context.SaveChanges();
                }

                trans.Complete();
            }
        }

        public void Delete(int siteId, int ManualId) {
            var data = _context.FmsManual.First(x => x.SiteId == siteId && x.ManualId == ManualId);
            _context.FmsManual.Remove(data);

            _context.SaveChanges();
        }

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