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

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

        public IQueryable<CmPartner> GetAll() {
            var query = _context.CmPartner.Where(x => x.PartnerType.Name == "공사");
            return query;
        }

        public IQueryable<CmPartner> Get(int siteId, int partnerId) {
            var data = _context.CmPartner.Where(x => x.PartnerId == partnerId && x.SiteId == siteId);
            return data;
        }

        public void Create(CmPartner data) {
            //data.IsUse = true;
            _context.CmPartner.Add(data);
            _context.SaveChanges();
        }

        public void Edit(int siteId, int partnerId, CmPartner data) {

            var persist = _context.CmPartner
                            .Where(x => x.PartnerId == partnerId && x.SiteId == siteId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.SiteId = data.SiteId;
            persist.PartnerId = data.PartnerId;
            persist.PartnerTypeId = data.PartnerTypeId;
            persist.BusinessFieldId = data.BusinessFieldId;
            persist.Name = data.Name;
            persist.RegistrationNo = data.RegistrationNo;
            persist.RepresentativeName = data.RepresentativeName;
            persist.PhoneNo = data.PhoneNo;
            persist.FaxNo = data.FaxNo;
            persist.SalesName = data.SalesName;
            persist.SalesPhoneNo = data.SalesPhoneNo;
            persist.AddressZip1 = data.AddressZip1;
            persist.AddressZip2 = data.AddressZip2;
            persist.Address1 = data.Address1;
            persist.Adderss2 = data.Adderss2;
            persist.HomepageUri = data.HomepageUri;
            persist.Note = data.Note;
            persist.IsUse = data.IsUse;

            _context.CmPartner.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(int siteId, int partnerId) {
            var data = _context.CmPartner.First(x => x.PartnerId == partnerId && x.SiteId == siteId);
            //data.IsUse = false;

            _context.CmPartner.Remove(data);
            _context.SaveChanges();
        }

        // public void AddCount(int siteId, int contractClassId) {
        //     var persist = _context.CmPartner
        //                     .Where(x => x.ContractClassId == contractClassId && x.SiteId == siteId).FirstOrDefault();
        //     if (persist == null) {
        //         throw new ServiceException("정보를 찾을 수 없습니다.");
        //     }

        //     persist.ReadCount += 1;
        //
        //     _context.CmPartner.Update(persist);
        //     _context.SaveChanges();
        // }
    }
}