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 FMSApp.Services {
    public class FloorService {
        private readonly ILogger<FloorService> _logger;
        private readonly FMSContext _context;

        private readonly AppSettings _appSettings;

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

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

                _context.CmFloor.Add(data);
                _context.SaveChanges();
            } else {
                persist.Name = data.Name;
                //
                _context.SaveChanges();
            }
        }

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