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 MaterialCodeTypeService {
        private readonly ILogger<MaterialCodeTypeService> _logger;
        private readonly FMSContext _context;

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

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

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

        public void Create(FmsMaterialCodeType data) {
            //동일이름 체크
            var persist = _context.FmsMaterialCodeType
            .Where(x => x.Name == data.Name || x.Nickname == data.Nickname).FirstOrDefault();
            if (persist != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }
            //앞뒤 공백 체크
            if (data.Name.Substring(0, 1) == " " || data.Name.Substring(data.Name.Length - 1, 1) == " " || data.Nickname.Substring(0, 1) == " " || data.Nickname.Substring(data.Nickname.Length - 1, 1) == " ") {
                throw new ServiceException("저장 혹은 수정 반드시 필수 입력 정보를 입력하셔야 합니다.(앞뒤공백 허용안함)");
            }


            data.IsUse = true;
            _context.FmsMaterialCodeType.Add(data);
            _context.SaveChanges();
        }

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

            var persist2 = _context.FmsMaterialCodeType
            .Where(x => x.MaterialTypeId != id && (x.Name == data.Name || x.Nickname == data.Nickname)).FirstOrDefault();
            if (persist2 != null) {
                throw new ServiceException("이미 동일한 정보가 존재합니다.");
            }
            //앞뒤 공백 체크
            if (data.Name.Substring(0, 1) == " " || data.Name.Substring(data.Name.Length - 1, 1) == " " || data.Nickname.Substring(0, 1) == " " || data.Nickname.Substring(data.Nickname.Length - 1, 1) == " ") {
                throw new ServiceException("저장 혹은 수정 반드시 필수 입력 정보를 입력하셔야 합니다.(앞뒤공백 허용안함)");
            }

            //persist.SiteId = data.SiteId;
            persist.MaterialTypeId = data.MaterialTypeId;
            persist.Name = data.Name;
            persist.Nickname = data.Nickname;
            persist.IsUse = data.IsUse;
            _context.FmsMaterialCodeType.Update(persist);
            _context.SaveChanges();
        }

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