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

namespace dk.gov.oiosi.oioublProfileLibrary {

    /// <summary>
    /// Represents an OIOUBL profile
    /// </summary>
    public class OioublProfile: IOioublProfile {
        
        private string _uddiId;
        private string _name;
        private OioublProfileRole _role;
        private IEnumerable<DocumentTypeConfig> _documentTypes;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">Name of the profile</param>
        /// <param name="uddiId">UDDI ID of the profile</param>
        /// <param name="role">Role of the profile</param>
        /// <param name="documentTypes">Document type associated with the profile</param>
        public OioublProfile(string name, string uddiId, OioublProfileRole role, IEnumerable<DocumentTypeConfig> documentTypes) {
            if (name == null) throw new NullArgumentException("name");
            if (uddiId == null) throw new NullArgumentException("uddiId");
            if (documentTypes == null) throw new NullArgumentException("documentTypes");
            _name = name;
            _uddiId = uddiId;
            _role = role;
            _documentTypes = documentTypes;
        }


        #region IOioublProfile Members

        /// <summary>
        /// The name of the profile
        /// </summary>
        public string Name {
            get { return _name; }
        }

        /// <summary>
        /// The uddi tModel id of the profile
        /// </summary>
        public string UddiId {
            get { return _uddiId; }
        }

        /// <summary>
        /// The role of the profile
        /// </summary>
        public OioublProfileRole BusinessEntityRole {
            get { return _role; }
        }

        /// <summary>
        /// The document types of the profile
        /// </summary>
        public IEnumerable<DocumentTypeConfig> DocumentTypes {
            get { return _documentTypes; }
        }

        /// <summary>
        /// The service definitions of the profile
        /// </summary>
        public IEnumerable<OioublServiceDefinition> ServiceDefinitions {
            get { return GetServiceDefinitions(); }
        }

        /// <summary>
        /// Checks if registered in uddi
        /// </summary>
        /// <returns>True if registered in uddi</returns>
        public bool IsRegisteredInUddi() {
            UddiGuidId uddiGuidId = new UddiGuidId(_uddiId, true);
            ArsBusinessProcessDefinition arsBusinessProcessDefinition = ArsBusinessProcessDefinition.Get(uddiGuidId);
            if (arsBusinessProcessDefinition == null) return false;

            bool areServicesRegistered = AreServicesRegistered();
            if (!areServicesRegistered) return false;

            return true;
        }

        private bool AreServicesRegistered() {
            foreach (OioublServiceDefinition serviceDefinition in ServiceDefinitions) {
                if (!serviceDefinition.IsRegisteredInUddi()) return false;
            }
            return true;
        }

        #endregion

        private IEnumerable<OioublServiceDefinition> GetServiceDefinitions() {
            List<OioublServiceDefinition> serviceDefinitions = new List<OioublServiceDefinition>();
            foreach (DocumentTypeConfig documentType in _documentTypes) {
                serviceDefinitions.Add(OioublServiceDefinition.GetFromDocumentType(documentType));
            }
            return serviceDefinitions;
        }

    }
}
