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

        private readonly AppSettings _appSettings;
        private readonly StorageHelper _storage;

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

        public void UpdateListData(CmTenant data) {
            var persist = _context.CmTenant.Where(x => x.SiteId == data.SiteId && x.TenantId == data.TenantId).FirstOrDefault();
            if (persist == null) throw new ServiceException("존재하지 않는 데이터 입니다.");

            persist.IsUse = data.IsUse;
            _context.CmTenant.Update(persist);
            _context.SaveChanges();
        }

        public void Save(CmTenant data) {

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

                if (data.BuildingId == null) {
                    var floor = _context.CmFloor.FirstOrDefault(x => x.SiteId == data.SiteId && x.FloorId == data.FloorId);
                    if (floor == null) throw new ServiceException("선택한 층정보를 찾을수 없습니다.");
                    data.BuildingId = floor.BuildingId;
                }

                _context.CmTenant.Add(data);
                _context.SaveChanges();
            } else {

                persist.BuildingId = data.BuildingId;
                if (data.BuildingId == null) {
                    var floor = _context.CmFloor.FirstOrDefault(x => x.SiteId == data.SiteId && x.FloorId == data.FloorId);
                    if (floor == null) throw new ServiceException("선택한 층정보를 찾을수 없습니다.");
                    persist.BuildingId = floor.BuildingId;
                }

                persist.CompanyTypeId = data.CompanyTypeId;
                persist.FloorId = data.FloorId;
                persist.Name = data.Name;
                persist.RepresentativeName = data.RepresentativeName;
                persist.RepresentativePhone = data.RepresentativePhone;
                persist.Fax = data.Fax;
                persist.AreaSize = data.AreaSize;
                persist.ExclusiveArea = data.ExclusiveArea;
                persist.ChargeName = data.ChargeName;
                persist.ChargePhone = data.ChargePhone;
                persist.AddressZip = data.AddressZip;
                persist.Address1 = data.Address1;
                persist.Address2 = data.Address2;
                persist.ChargeEmail = data.ChargeEmail;
                persist.Homepage = data.Homepage;
                persist.Comment = data.Comment;
                persist.IsUse = data.IsUse;
                _context.CmTenant.Update(persist);
                _context.SaveChanges();
            }
        }

        public void Delete(int siteId, int tenantId) {
            var data = _context.CmTenant.First(x => x.SiteId == siteId && x.TenantId == tenantId);
            _context.CmTenant.Remove(data);

            _context.SaveChanges();
        }

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