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

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;

namespace FMSAdmin.Services {
    public class WorkCodeDetailTypeService {
        private readonly ILogger<WorkCodeDetailTypeService> _logger;
        private readonly FMSContext _context;

        public WorkCodeDetailTypeService(
            ILogger<WorkCodeDetailTypeService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }

        public void Save(FmsWorkCodeDetailType data) {
            using (var trans = new TransactionScope()) {
                var persist = _context.FmsWorkCodeDetailType
                                .Where(x => x.WorkDetailTypeId == data.WorkDetailTypeId).FirstOrDefault();


                if (persist == null) {
                    var check = _context.FmsWorkCodeDetailType.Where(x => x.Name == data.Name && x.WorkTypeId == data.WorkTypeId).Count();
                    if (check > 0) {
                        throw new ServiceException("이미 동일한 정보가 정보가 존재합니다..");
                    }

                    _context.FmsWorkCodeDetailType.Add(data);
                    _context.SaveChanges();
                } else {
                    persist.WorkTypeId = data.WorkTypeId;
                    persist.Code = data.Code;
                    persist.Name = data.Name;
                    persist.Description = data.Description;
                    persist.IsUse = data.IsUse;

                    //
                    _context.SaveChanges();
                }
                trans.Complete();
            }
        }

        // public IQueryable<FmsWorkCodeDetailType> GetAll() {
        //     try {
        //         _logger.LogDebug("#################");
        //         var query = _context.FmsWorkCodeDetailType.AsNoTracking();
        //         _logger.LogError("Error:" + query.Count());
        //         return query;
        //     } catch (Exception e) {
        //         _logger.LogError("$$$$:" + e.ToString());
        //         return null;
        //     }


        // }

        public IQueryable<FmsWorkCodeDetailType> GetAll() {
            var query = _context.FmsWorkCodeDetailType/*.AsNoTracking()*/;
            return query;
        }

        public IQueryable<FmsWorkCodeDetailType> Get(int id) {
            var data = _context.FmsWorkCodeDetailType.Where(x => x.WorkDetailTypeId == id);
            return data;
        }

        public void Delete(int id) {
            using (var trans = new TransactionScope()) {
                var data = _context.FmsWorkCodeDetailType.First(x => x.WorkDetailTypeId == id);
                //data.IsUse = false;
                _context.FmsWorkCodeDetailType.Remove(data);
                _context.SaveChanges();
                trans.Complete();
            }
        }
    }
}