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

        private readonly AppSettings _appSettings;

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

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

                _context.CmSite.Add(data);
                _context.SaveChanges();
            } else {
                persist.IsUse = data.IsUse;
                persist.IsControlSchedule = data.IsControlSchedule;
                //
                _context.SaveChanges();
            }
        }

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

        public IQueryable<CmSite> Get(int siteId) {
            var data = _context.CmSite.Where(x => x.SiteId == siteId);
            return data;
        }
    }
}