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

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


        public void Create(CmTenant data) {
            checkName(data);

            data.BuildingId = _context.CmBuilding.Where(x => x.SiteId == data.SiteId && x.IsUse == true).Select(x => x.BuildingId).SingleOrDefault();
            _context.CmTenant.Add(data);
            _context.SaveChanges();
        }

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

        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 Edit(int id, int siteId, CmTenant data) {
            var persist = _context.CmTenant
                            .Where(x => x.SiteId == data.SiteId
                                && x.TenantId == data.TenantId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }
            if (!persist.Name.Equals(data.Name)) {
                checkName(data);
            }
            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.Homepage = data.Homepage;
            persist.Comment = data.Comment;
            persist.IsUse = data.IsUse;
            persist.ChargeEmail = data.ChargeEmail;
            _context.CmTenant.Update(persist);
            _context.SaveChanges();
        }


        public int checkName(CmTenant data, bool isExcel = false) {
            var check = _context.CmTenant.Where(x => x.SiteId == data.SiteId && x.BuildingId == data.BuildingId && x.FloorId == data.FloorId && x.Name == data.Name).Count();
            if (!isExcel && check > 0) {
                throw new ServiceException("동일 정보가 존재합니다.[빌딩명칭-층명칭-회사명칭]");
            }
            return check;
        }
        public void Delete(int id, int siteId) {
            var data = _context.CmTenant.First(x => x.SiteId == siteId && x.TenantId == id);
            _context.CmTenant.Remove(data);
            _context.SaveChanges();
        }

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


        public IQueryable<CmTenant> Get(int id, int siteId) {
            var data = _context.CmTenant.Where(x => x.TenantId == id && x.SiteId == siteId);
            return data;
        }
    }
}