/*
  * The contents of this file are subject to the Mozilla Public
  * License Version 1.1 (the "License"); you may not use this
  * file except in compliance with the License. You may obtain
  * a copy of the License at http://www.mozilla.org/MPL/
  *
  * Software distributed under the License is distributed on an
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  * or implied. See the License for the specific language governing
  * rights and limitations under the License.
  *
  *
  * The Original Code is .NET RASP toolkit.
  *
  * The Initial Developer of the Original Code is Accenture and Avanade.
  * Portions created by Accenture and Avanade are Copyright (C) 2007
  * Danish National IT and Telecom Agency (http://www.itst.dk). 
  * All Rights Reserved.
  *
  * Contributor(s):
  *   Gert Sylvest (gerts@avanade.com)
  *   Patrik Johansson (p.johansson@accenture.com)
  *   Michael Nielsen (michaelni@avanade.com)
  *   Dennis Søgaard (dennis.j.sogaard@accenture.com)
  *   Ramzi Fadel (ramzif@avanade.com)
  *   Mikkel Hippe Brun (mhb@itst.dk)
  *   Finn Hartmann Jordal (fhj@itst.dk)
  *   Christian Lanng (chl@itst.dk)
  *
  */
using System;
using System.Collections.Generic;
using System.Text;
using dk.gov.oiosi.uddi.ars;
using dk.gov.oiosi.uddi.category;
using dk.gov.oiosi.uddi;

namespace dk.gov.oiosi.oioublProfileLibrary {
    internal class OioublServiceFactory {


        /// <summary>
        /// Creates an OioublService.
        /// Creates an ArsEndpoint, attaches ArsBindings and ArsProcessInstances
        /// </summary>
        /// <param name="oioublProfile">The profile the service should be attached to.
        /// An ArsProcessInstance is created with a reference to the process definition defined in the profile.
        /// </param>
        /// <param name="serviceDefinition">This defines which service (document type) to create</param>
        /// <param name="oioublEndpointKey">The service identier defines which cvr/ean number that owns the service</param>
        /// <param name="endpointAddress">The address of the service (http, smtp, ...)</param>
        /// <param name="certificateSubject">The certificate subject of the person who is creating this service</param>
        /// <param name="businessEntity">The UddiId of the business entity owning the service</param>
        /// <returns></returns>
        public static OioublService CreateOioublService(IOioublProfile oioublProfile,
            OioublServiceDefinition serviceDefinition, OioublEndpointKey oioublEndpointKey,
            OioublEndpointAddress endpointAddress, string certificateSubject,
            IOioublBusinessEntity businessEntity) {
            

            ArsEndpoint arsEndpoint = GetArsEndpoint(serviceDefinition, oioublEndpointKey,
                certificateSubject, businessEntity.UddiId);
            OioublService oioublService = new OioublService(arsEndpoint, businessEntity);

            if (endpointAddress.HttpEndpointAddress != null) {
                string addressUri = endpointAddress.HttpEndpointAddress.AbsoluteUri;
                oioublService.AddBinding(oioublProfile, serviceDefinition, OioublEndpointAddressType.Http, addressUri);
            }

            if (endpointAddress.SmtpEndpointAddress != null) {
                string addressUri = endpointAddress.SmtpEndpointAddress.AbsoluteUri;
                oioublService.AddBinding(oioublProfile, serviceDefinition, OioublEndpointAddressType.Smtp, addressUri);
            }

            oioublService.Save();
            return oioublService;
        }


        private static ArsEndpoint GetArsEndpoint(OioublServiceDefinition serviceDefinition,
            OioublEndpointKey oioublEndpointKey, string certificateSubject,
            string businessEntityUddiId) {
            
            // Custom values
            ArsEndpointKey endpointKey = oioublEndpointKey.GetArsEndpointKey();
            EndpointKeytype endpointKeytype = oioublEndpointKey.GetEndpointKeytype();
            EndpointCertificate endpointCertificate = new EndpointCertificate(certificateSubject);
            Name serviceName = new Name(serviceDefinition.Name + " endpoint");
            Description serviceDescription = new Description("Endpoint til " + serviceDefinition.Name);

            // Default values
            OIOTaxonomy oioTaxonomy = new OIOTaxonomy("OIO subject scheme / TRADE AND INDUSTRY / E-COMMERCE /", "uddi:ea4bc88f-9479-4f9b-a354-4acabdb99336");
            EndpointContactEmail endpointContactEmail = new EndpointContactEmail("");
            EndpointActivationDate endpointActivationDate = new EndpointActivationDate(DateTime.Now.AddDays(-2));
            EndpointExpirationDate endpointExpirationDate = new EndpointExpirationDate(DateTime.Now.AddYears(50));
            TermsOfUseUrl termsOfUseUrl = new TermsOfUseUrl();
            Version version = new Version("1.0.0");
            NewerVersionReference newerVersionReference = new NewerVersionReference();
            UddiId businessKey = new UddiGuidId(businessEntityUddiId, true);

            ArsEndpoint arsEndPoint = new ArsEndpoint(oioTaxonomy, endpointContactEmail, endpointKey,
                endpointKeytype, endpointActivationDate, endpointExpirationDate, termsOfUseUrl,
                endpointCertificate, version, newerVersionReference, serviceName,
                serviceDescription, businessKey);

            return arsEndPoint;
        }
    }
}
