using csharp_clients.MasterDataService;
using csharp_clients.ParcelService;
using GLS.MyGLS.ServiceData.APIDTOs.MasterData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace csharp_clients
{
    public class SoapClient
    {
        /*HELP:
         * - Proxy generation (from the service URL): https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
         */

        public static ParcelServiceClient _parcelServiceClient = new ParcelServiceClient();

        public static MasterDataServiceClient _masterDataServiceClient = new MasterDataServiceClient();

        #region "Parcel service"
        /// <summary>
        /// Label(s) generation by the service.
        /// </summary>
        public static void PrintLabels()
        {
            //Test request:
            PrintLabelsRequest request = new PrintLabelsRequest();

            request.Username = Program._username;
            request.Password = Program.Sha512(Program._password);
            
            request.ParcelList = PrepareParcelData();

            request.ShowPrintDialog = false;

            PrintLabelsResponse response = new PrintLabelsResponse();

            //Service calling:
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            response = _parcelServiceClient.PrintLabels(request);

            if (response != null && response.PrintLabelsErrorList.Length == 0 && response.Labels.Length > 0)
            {
                //Label(s) saving:
                File.WriteAllBytes("csharp_clients_soap_PrintLabels.pdf", response.Labels);
            }
        }

        /// <summary>
        /// Preparing label(s) by the service.
        /// </summary>
        public static GetPrintedLabelsRequest PrepareLabels()
        {
            PrepareLabelsResponse response = new PrepareLabelsResponse();

            //Test request:
            PrepareLabelsRequest step1Request = new PrepareLabelsRequest();
            step1Request.Username = Program._username;
            step1Request.Password = Program.Sha512(Program._password);

            step1Request.ParcelList = PrepareParcelData();

            //Service calling:
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            response = _parcelServiceClient.PrepareLabels(step1Request);

            int[] parcelIdList = new int[response.ParcelInfoList.Length];
            if (response != null && response.PrepareLabelsError.Length == 0 && response.ParcelInfoList.Length > 0)
            {
                for (int i = 0; i < response.ParcelInfoList.Length; i++)
                {
                    parcelIdList[i] = response.ParcelInfoList[i].ParcelId;
                }
            }

            //Test request:
            GetPrintedLabelsRequest step2Request = new GetPrintedLabelsRequest();
            step2Request.Username = Program._username;
            step2Request.Password = Program.Sha512(Program._password);
            step2Request.ParcelIdList = parcelIdList;
            step2Request.PrintPosition = 1;
            step2Request.ShowPrintDialog = false;

            return step2Request;
        }

        /// <summary>
        /// Get label(s) by the service.
        /// </summary>
        public static void GetPrintedLabels(GetPrintedLabelsRequest request)
        {
            GetPrintedLabelsResponse response = new GetPrintedLabelsResponse();

            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

            //Service calling:
            response = _parcelServiceClient.GetPrintedLabels(request);

            if (response != null && response.GetPrintedLabelsErrorList.Length == 0 && response.Labels.Length > 0)
            {
                //Label(s) saving:
                File.WriteAllBytes("csharp_clients_soap_GetPrintedLabels.pdf", response.Labels);
            }
        }

        /// <summary>
        /// Get parcel(s) information by date ranges.
        /// </summary>
        public static void GetParcelList()
        {
            //Test request:
            GetParcelListRequest request = new GetParcelListRequest();

            request.Username = Program._username;
            request.Password = Program.Sha512(Program._password);
            request.PickupDateFrom = new DateTime(2020, 04, 16, 0, 0, 0);
            request.PickupDateTo = new DateTime(2020, 04, 16, 0, 0, 0);
            request.PrintDateFrom = null;
            request.PrintDateTo = null;

            GetParcelListResponse response = new GetParcelListResponse();

            //Service calling:
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            response = _parcelServiceClient.GetParcelList(request);

            Console.WriteLine(response.GetParcelListErrors.Length);
            Console.WriteLine(response.PrintDataInfoList.Length);
        }

        /// <summary>
        /// Get parcel statuses.
        /// </summary>
        public static void GetParcelStatuses()
        {
            //Test request:
            GetParcelStatusesRequest request = new GetParcelStatusesRequest();

            request.Username = Program._username;
            request.Password = Program.Sha512(Program._password);
            request.ReturnPOD = true;
            request.ParcelNumber = 0;
            request.LanguageIsoCode = "HU";

            GetParcelStatusResponse response = new GetParcelStatusResponse();

            //Service calling:
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            response = _parcelServiceClient.GetParcelStatuses(request);

            Console.WriteLine(response.GetParcelStatusErrors.Length);
            if (response != null && response.GetParcelStatusErrors.Length == 0 && response.POD?.Length > 0)
            {
                //POD saving:
                File.WriteAllBytes("csharp_clients_soap_GetParcelStatuses.pdf", response.POD);
            }
        }
        #endregion

        #region "utility functions"
        /// <summary>
        /// Preparing parcel data.
        /// </summary>
        public static Parcel[] PrepareParcelData()
        {
            Parcel parcel = new Parcel();

            parcel.ClientNumber = Program._clientNumber;
            parcel.ClientReference = "TEST PARCEL";
            parcel.CODAmount = 0;
            parcel.CODReference = "COD REFERENCE";
            parcel.Content = "CONTENT";
            parcel.Count = 1;
            Address deliveryAddress = new Address();
            deliveryAddress.Name = "Delivery Address";
            deliveryAddress.Street = "Európa u.";
            deliveryAddress.HouseNumber = "2";
            deliveryAddress.City = "Alsónémedi";
            deliveryAddress.ZipCode = "2351";
            deliveryAddress.CountryIsoCode = "HU";
            deliveryAddress.ContactEmail = "something@anything.hu";
            deliveryAddress.ContactName = "Contact Name";
            deliveryAddress.ContactPhone = "+36701234567";
            deliveryAddress.HouseNumberInfo = "/b"; //V2
            parcel.DeliveryAddress = deliveryAddress;
            Address pickupAddress = new Address();
            pickupAddress.Name = "Delivery Address";
            pickupAddress.Street = "Európa u.";
            pickupAddress.HouseNumber = "2";
            pickupAddress.City = "Alsónémedi";
            pickupAddress.ZipCode = "2351";
            pickupAddress.CountryIsoCode = "HU";
            pickupAddress.ContactEmail = "something@anything.hu";
            pickupAddress.ContactName = "Contact Name";
            pickupAddress.ContactPhone = "+36701234567";
            pickupAddress.HouseNumberInfo = "/a"; //V2
            parcel.PickupAddress = pickupAddress;
            parcel.PickupDate = new DateTime(2019, 12, 14, 0, 0, 0);
            Service[] serviceList = new Service[]
            {
                new Service() { Code = "PSD", PSDParameter = new ServiceParameterStringInteger() { StringValue = "2351-CSOMAGPONT" } }
            };
            parcel.ServiceList = serviceList;

            Parcel[] parcels = new Parcel[]
            {
                parcel
            };

            return parcels;
        }
        #endregion
    }
}
