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 CustomerManager : BLBase
    {
        public IList<ParameterDTO> GetBusinessType()
        {
            IList<GetParameter_Result> Result = context.GetParameter(5, 0, 0).ToList();

            IList<ParameterDTO> BusinessType = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetParameter_Result, ParameterDTO>();
                BusinessType = Mapper.Map<IList<GetParameter_Result>, IList<ParameterDTO>>(Result);
            }

            return BusinessType;
        }

        public IList<ParameterDTO> GetCustomerType()
        {
            IList<GetParameter_Result> Result = context.GetParameter(6, 0, 0).ToList();

            IList<ParameterDTO> CustomerType = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetParameter_Result, ParameterDTO>();
                CustomerType = Mapper.Map<IList<GetParameter_Result>, IList<ParameterDTO>>(Result);
            }

            return CustomerType;
        }

        public IList<ParameterDTO> GetCustomerGroup()
        {
            IList<GetParameter_Result> Result = context.GetParameter(7, 0, 0).ToList();

            IList<ParameterDTO> CustomerGroup = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetParameter_Result, ParameterDTO>();
                CustomerGroup = Mapper.Map<IList<GetParameter_Result>, IList<ParameterDTO>>(Result);
            }

            return CustomerGroup;
        }

        public CustomerDTO GetCustomer(int? Customer_Id, string Customer_Name)
        {
            GetCustomer_Result Result = null;

            if (Customer_Id != null)
                Result = context.GetCustomer(Customer_Id, null, null, null, null, null, null, null, null, null, null).FirstOrDefault();
            else
                Result = context.GetCustomer(null, null, Customer_Name, null, null, null, null, null, null, null, null).FirstOrDefault();

            CustomerDTO Customer = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetCustomer_Result, CustomerDTO>();
                Customer = Mapper.Map<GetCustomer_Result, CustomerDTO>(Result);
            }

            return Customer;
        }

        public IList<CustomerDTO> GetAllCustomer()
        {
            IList<GetCustomer_Result> Result = context.GetCustomer(null, null, null, null, null, null, null, null, null, null, null).ToList();

            IList<CustomerDTO> Customer = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetCustomer_Result, CustomerDTO>();
                Customer = Mapper.Map<IList<GetCustomer_Result>, IList<CustomerDTO>>(Result);
            }

            return Customer;
        }

        public void InsertUpdateCustomer(CustomerDTO Customer, int UpdateUserId)
        {
            ObjectParameter Cust_Id = new ObjectParameter("CustId", typeof(int));
            context.InsertUpdateCustomer(Customer.Customer_Id,
                                         Customer.Customer_Code,
                                         Customer.Customer_Name,
                                         Customer.Customer_Name_BL,
                                         Customer.Business_Type,
                                         Customer.Customer_Type,
                                         Customer.Customer_Group,
                                         Customer.Location_Id,
                                         Customer.Key_Account,
                                         Customer.Status,
                                         UpdateUserId,
                                         Customer.Sales_Channel,
                                         Cust_Id);
        }

        public IList<ActiveCustomerLocationRegionCountryDTO> GetCustomerLocationRegionCountry(int UserId, string SearchCustomer)
        {
            IList<GetActiveCustomerLocationRegionCountry_Result> Result = null;

            if (string.IsNullOrEmpty(SearchCustomer))
                Result = context.GetActiveCustomerLocationRegionCountry(null).ToList();
            else
                Result = context.GetActiveCustomerLocationRegionCountry(null).Where(c => c.Customer_Name.ToLower().StartsWith(SearchCustomer.ToLower())).ToList();

            IList<ActiveCustomerLocationRegionCountryDTO> Customer = null;
            if (Result != null)
            {
                Mapper.CreateMap<GetActiveCustomerLocationRegionCountry_Result, ActiveCustomerLocationRegionCountryDTO>();
                Customer = Mapper.Map<IList<GetActiveCustomerLocationRegionCountry_Result>, IList<ActiveCustomerLocationRegionCountryDTO>>(Result);
            }

            return Customer;
        }
    }
}
