#region Copyright © 2015 CLIPSOFT Co.,Ltd. All Rights Reserved.
//
// All rights are reserved. Reproduction or transmission in whole or in part, 
// in any form or by any means, electronic, mechanical or otherwise, is 
// prohibited without the prior written consent of the copyright owner.
//
// Filename:PatientInfoCtrl.cs
//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace CLIP.eForm.Consent.UI
{
    /// <summary>
    /// 웹서비스 연결 관련 클래스
    /// </summary>
    /// <remarks>
    /// <p>[설계자]</p>
    /// <p>  클립소프트 연구소 홍지철 (jchong@clipsoft.co.kr)</p>
    /// <p>[원본 작성자]</p>
    /// <p>  클립소프트 기술부 이창훈 (chlee@clipsoft.co.kr)</p>
    /// <p>[수정 작성자]</p>
    /// <p>  클립소프트 기술부 이인희</p>
    /// <p>----------------------------------------------------------------------------------------</p>
    /// <p>[HISTORY]</p>
    /// <p> 2015-07-30 : 최초작성</p>
    /// <p>----------------------------------------------------------------------------------------</p>
    /// </remarks>
    class WebMethodCommon
    {
        /// <summary>
        /// consentWebService를 리턴한다.
        /// </summary>
        /// <returns></returns>
        public static ConsentSvcRef.ConsentSvcSoapClient GetConsentWebService(string serverUrl)
        {
            ConsentSvcRef.ConsentSvcSoapClient consentWebService = null;
            var method = serverUrl.Substring(0, serverUrl.IndexOf(":"));

            BasicHttpBinding consentSvcBinding = null;

            if (method.ToUpper() == "HTTPS")
            {
                consentSvcBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            }
            else
            {
                consentSvcBinding = new BasicHttpBinding();
            }

            consentSvcBinding.Name = "ConsentSvcSoap";
            consentSvcBinding.MaxReceivedMessageSize = 2147483647;
            consentSvcBinding.MaxBufferSize = 2147483647;
            consentSvcBinding.MaxBufferPoolSize = 2147483647;
            consentSvcBinding.ReaderQuotas.MaxArrayLength = 2147483647;
            consentSvcBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
            consentSvcBinding.ReaderQuotas.MaxDepth = 2147483647;
            consentSvcBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            consentSvcBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
            consentSvcBinding.BypassProxyOnLocal = false;
            consentSvcBinding.AllowCookies = false;

            EndpointAddress consentSvcendpoint = new EndpointAddress(serverUrl);

            consentWebService = new ConsentSvcRef.ConsentSvcSoapClient(consentSvcBinding, consentSvcendpoint);

            ConfigureClientEndPoint(consentWebService.Endpoint);

            return consentWebService;
        }

        /// <summary>
        /// hospitalWebService 리턴한다.
        /// </summary>
        /// <returns></returns>
        public static HospitalSvcRef.HospitalSvcSoapClient GetHospitalWebService(string serverUrl)
        {
            HospitalSvcRef.HospitalSvcSoapClient hospitalWebService = null;

            var method = serverUrl.Substring(0, serverUrl.IndexOf(":"));

            BasicHttpBinding hospitalSvcBinding = null;
            if (method.ToUpper() == "HTTPS")
            {
                hospitalSvcBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            }
            else
            {
                hospitalSvcBinding = new BasicHttpBinding();
            }

            //BasicHttpBinding hospitalSvcBinding = new BasicHttpBinding();

            hospitalSvcBinding.Name = "HospitalSvcSoap";
            hospitalSvcBinding.MaxReceivedMessageSize = 2147483647;
            hospitalSvcBinding.MaxBufferSize = 2147483647;
            hospitalSvcBinding.MaxBufferPoolSize = 2147483647;
            hospitalSvcBinding.ReaderQuotas.MaxArrayLength = 2147483647;
            hospitalSvcBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
            hospitalSvcBinding.ReaderQuotas.MaxDepth = 2147483647;
            hospitalSvcBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            hospitalSvcBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
            hospitalSvcBinding.BypassProxyOnLocal = false;
            hospitalSvcBinding.AllowCookies = false;

            EndpointAddress hospitalSvcendpoint = new EndpointAddress(serverUrl);
            hospitalWebService = new HospitalSvcRef.HospitalSvcSoapClient(hospitalSvcBinding, hospitalSvcendpoint);
            
            ConfigureClientEndPoint(hospitalWebService.Endpoint);

            return hospitalWebService;
        }

        private static void ConfigureClientEndPoint(ServiceEndpoint endPoint)
        {
            //개체 그래프에서 직렬화 또는 역직렬화할 수 있는 최대 항목 수는 '65536'입니다. 개체 그래프를 변경하거나 MaxItemsInObjectGraph 할당량을 늘리십시오.  
            foreach (OperationDescription operation in endPoint.Contract.Operations)
            {
                DataContractSerializerOperationBehavior dataContractBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
                if (dataContractBehavior != null)
                {
                    dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
                }
            }
        }
    }
}
