/*
  * 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 dk.gov.oiosi.uddi.ars;
using dk.gov.oiosi.uddi;
using dk.gov.oiosi.communication.configuration;
using dk.gov.oiosi.uddi.category;
using dk.gov.oiosi.oioublProfileLibrary.Exceptions;

namespace dk.gov.oiosi.oioublProfileLibrary {

    /// <summary>
    /// Wrapper class for the OasisPortTypeRegistration class.
    /// An OioublServiceDefinition is similar to a DocumentType.
    /// In the UDDI registry the ServiceDefinition is stored as a OasisPortTypeRegistration.
    /// </summary>
    public class OioublServiceDefinition {
        
        private UddiId _uddiId;
        private string _name;

        /// <summary>
        /// The UDDI id of the Service Definition (OasisPortTypeRegistration)
        /// </summary>
        public UddiId UddiId {
            get { return _uddiId; }
        }

        /// <summary>
        /// The name of the ServiceDefinition
        /// </summary>
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Constructor taking the UDDI id of the OasisPortTypeRegistration as a parameter
        /// </summary>
        /// <param name="uddiId"></param>
        public OioublServiceDefinition(UddiId uddiId) {
            _uddiId = uddiId;
        }

        /// <summary>
        /// Utillity method to see if the ServiceDefinition is registered in the UDDI registry
        /// </summary>
        /// <returns></returns>
        public bool IsRegisteredInUddi() {
            OasisPortTypeRegistration portTypeRegistration = OasisPortTypeRegistration.Get(_uddiId);
            if (portTypeRegistration == null) return false;

            return true;
        }

        /// <summary>
        /// Returns the BindingRegistrationReference Id.
        /// The OioublServiceDefinition is equal to the ArsPortType
        /// </summary>
        /// <returns></returns>
        public UddiId GetBindingRegistrationReferenceUddiId(OioublEndpointAddressType oioublEndpointAddressType) {
            string modelId;
            switch (oioublEndpointAddressType) {
                case OioublEndpointAddressType.Http:
                    modelId = ArsBinding.FindBindingGuid(_uddiId, UddiOrgWsdlCategorizationTransportCode.http);
                    break;
                case OioublEndpointAddressType.Smtp:
                    modelId = ArsBinding.FindBindingGuid(_uddiId, UddiOrgWsdlCategorizationTransportCode.smtp);
                    break;
                default:
                    throw new UnhandledOioublEndpointAddressTypeException(oioublEndpointAddressType);
            }

            if (modelId == null)
                throw new BindingRegistrationReferenceNotFoundException(_uddiId);

            return new UddiGuidId(modelId, true);
        }

        /// <summary>
        /// Gets an OIOUBL service definition from document type
        /// </summary>
        /// <param name="documentType">The document type</param>
        /// <returns>The OIOUBL service definition object</returns>
        public static OioublServiceDefinition GetFromDocumentType(DocumentTypeConfig documentType) {
            UddiGuidId documentTypeUddiId = new UddiGuidId(documentType.ServiceContractTModel, true);
            OioublServiceDefinition serviceDefinition = new OioublServiceDefinition(documentTypeUddiId);
            serviceDefinition.Name = documentType.FriendlyName;
            return serviceDefinition;
        }


    }
}
