using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DataAccess;
using DTO;

namespace BusinessLayer
{
    public class ContactManager : BLBase
    {
        public ContactWithCustNameDTO GetContact(int? ContactId, int? CustomerId)
        {
            GetContactWithCustName_Result Result = context.GetContactWithCustName(ContactId, null, null, null, CustomerId, null, null, null, null).FirstOrDefault();

            ContactWithCustNameDTO Contact = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetContactWithCustName_Result, ContactWithCustNameDTO>();
                Contact = Mapper.Map<GetContactWithCustName_Result, ContactWithCustNameDTO>(Result);
            }

            return Contact;
        }

        public IList<ContactWithCustNameDTO> GetCustomerContacts(int? CustomerId)
        {
            IList<GetContactWithCustName_Result> Result = context.GetContactWithCustName(null, null, null, null, CustomerId, null, null, null, null).ToList();

            IList<ContactWithCustNameDTO> Contact = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetContactWithCustName_Result, ContactWithCustNameDTO>();
                Contact = Mapper.Map < IList<GetContactWithCustName_Result>, IList<ContactWithCustNameDTO>>(Result);
            }

            return Contact;
        } 

        public void InsertUpdateContact(ContactWithCustNameDTO Contact)
        {
            ObjectParameter Cont_Id = new ObjectParameter("Cont_Id", typeof(int));
            context.InsertUpdateContact(Contact.Contact_Id,
                                         Contact.Contact_Name,
                                         Contact.Contact_Name_BL,
                                         Contact.Customer_Id,
                                         Contact.Department,
                                         Contact.Department_BL,
                                         Contact.Mobile,
                                         Contact.Status,
                                         Cont_Id);
        }
    }
}
