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;

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

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

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

        public IQueryable<FmsMaterialWarehouse> Gets(int id, int siteId) {
            var data = _context.FmsMaterialWarehouse.Where(x => x.WarehouseId == id && x.SiteId == siteId);
            return data;
        }

        public void Create(FmsMaterialWarehouse data) {
            var persist2 = _context.FmsMaterialWarehouse
            .Where(x => x.SiteId == data.SiteId && x.BusinessFieldId == data.BusinessFieldId && x.Name == data.Name).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }

            data.BuildingId = _context.CmBuilding.Where(x => x.SiteId == data.SiteId).Select(x => x.BuildingId).FirstOrDefault();
            _context.FmsMaterialWarehouse.Add(data);
            _context.SaveChanges();
        }

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

            var persist2 = _context.FmsMaterialWarehouse
            .Where(x => x.WarehouseId != id && x.SiteId == siteId && x.BusinessFieldId == data.BusinessFieldId && x.Name == data.Name).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }

            persist.SiteId = data.SiteId;
            persist.BusinessFieldId = data.BusinessFieldId;
            persist.WarehouseId = data.WarehouseId;
            persist.Name = data.Name;
            persist.IsUse = data.IsUse;
            persist.BuildingId = _context.CmBuilding.Where(x => x.SiteId == data.SiteId).Select(x => x.BuildingId).FirstOrDefault();
            persist.FloorId = data.FloorId;
            persist.ZoneId = data.ZoneId;
            _context.FmsMaterialWarehouse.Update(persist);
            _context.SaveChanges();
        }

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


        public int checkName(FmsMaterialWarehouse data, bool isExcel = false) {
            var check = _context.FmsMaterialWarehouse.Where(x => x.SiteId == data.SiteId && x.BusinessFieldId == data.BusinessFieldId && x.Name == data.Name).Count();
            if (!isExcel && check > 0) {
                throw new ServiceException("동일 정보가 존재합니다. [업무분야-창고명]");
            }
            return check;
        }


        public void ImportExcel(IList<FmsMaterialWarehouse> list) {
            using (var transaction = _context.Database.BeginTransaction()) {
                _context.FmsMaterialWarehouse.AddRange(list);
                _context.SaveChanges();

                transaction.Commit();
            }
        }
    }
}