using System.Linq;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.Extensions.Logging;

namespace FMSApp.Services {
    public class PartnerService {
        private readonly ILogger<PartnerService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

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

        public void Create(CmPartner data) {
            _context.CmPartner.Add(data);
            _context.SaveChanges();
        }

        public void Save(CmPartner partner) {
            var persist = _context.CmPartner
                            .Where(x => x.PartnerId == partner.PartnerId && x.SiteId == partner.SiteId).FirstOrDefault();

            if (persist == null) {
                var check = _context.CmPartner.Where(x => x.SiteId == partner.SiteId
                    && x.PartnerId == partner.PartnerId).Count();
                if (check > 0) {
                    throw new ServiceException("아이디가 중복됩니다.");
                }
                _context.CmPartner.Add(partner);
                _context.SaveChanges();

            } else {

                persist.SiteId = partner.SiteId;
                persist.PartnerTypeId = partner.PartnerTypeId;
                persist.BusinessFieldId = partner.BusinessFieldId;
                persist.Name = partner.Name;
                persist.RegistrationNo = partner.RegistrationNo;
                persist.RepresentativeName = partner.RepresentativeName;
                persist.PhoneNo = partner.PhoneNo;
                persist.FaxNo = partner.FaxNo;
                persist.SalesName = partner.SalesName;
                persist.SalesPhoneNo = partner.SalesPhoneNo;
                persist.AddressZip1 = partner.AddressZip1;
                persist.AddressZip2 = partner.AddressZip2;
                persist.Address1 = partner.Address1;
                persist.Adderss2 = partner.Adderss2;
                persist.HomepageUri = partner.HomepageUri;
                persist.Note = partner.Note;
                persist.IsUse = partner.IsUse;
                _context.CmPartner.Update(persist);
                _context.SaveChanges();
            }
        }

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

        public CmPartner Get(int id, int siteId) {
            var data = _context.CmPartner.Single(x => x.PartnerId == id && x.SiteId == siteId);
            return data;
        }

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

        public void Delete(int id, int siteId) {
            var data = _context.CmPartner.First(x => x.PartnerId == id && x.SiteId == siteId);
            _context.CmPartner.Remove(data);
            _context.SaveChanges();
        }
    }
}