/*
  * 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.category;
using dk.gov.oiosi.addressing;
using dk.gov.oiosi.oioublProfileLibrary.Exceptions;

namespace dk.gov.oiosi.oioublProfileLibrary {

    /// <summary>
    /// Enum with the currently supported key types.
    /// A key type is EAN, CVR, ...
    /// </summary>
    public enum OioublEndpointKeyType { 
        /// <summary>
        /// EAN
        /// </summary>
        Ean, 

        /// <summary>
        /// CVR
        /// </summary>
        Cvr 
    };

    /// <summary>
    /// A class containing the key/value pair of an endpoint key.
    /// The OioublEndpointKey is the lookup key for the service, and as such 
    /// it must contain a value that uniquely defined the BusinessEntity owning the service-
    /// </summary>
    public class OioublEndpointKey {
        private OioublEndpointKeyType _oioublEndpointKeyType;
        private string _oioublEndpointKeyValue;

        /// <summary>
        /// Gets or sets the endpoint key type
        /// </summary>
        public OioublEndpointKeyType OioublEndpointKeyType {
            get { return _oioublEndpointKeyType; }
            set { _oioublEndpointKeyType = value; }
        }

        /// <summary>
        /// Gets or sets the endpoint key value
        /// </summary>
        public string OioublEndpointKeyValue {
            get { return _oioublEndpointKeyValue; }
            set { _oioublEndpointKeyValue = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="keyType"></param>
        /// <param name="keyValue"></param>
        public OioublEndpointKey(OioublEndpointKeyType keyType, string keyValue) {
            _oioublEndpointKeyType = keyType;
            _oioublEndpointKeyValue = keyValue;
        }

        /// <summary>
        /// Returns the corresponding ARS endpoint key value
        /// </summary>
        /// <returns></returns>
        public ArsEndpointKey GetArsEndpointKey() {
            switch (_oioublEndpointKeyType) {
                case OioublEndpointKeyType.Cvr:
                    return new ArsEndpointKey(new IdentifierCvr(_oioublEndpointKeyValue));
                case OioublEndpointKeyType.Ean:
                    return new ArsEndpointKey(new IdentifierEan(_oioublEndpointKeyValue));
                default:
                    throw new UnhandledOioublEndpointKeyTypeException(_oioublEndpointKeyType);
            }
        }

        /// <summary>
        /// Returns the corresponding ARS endpoint key type
        /// </summary>
        /// <returns></returns>
        public EndpointKeytype GetEndpointKeytype() {
            switch (_oioublEndpointKeyType) {
                case OioublEndpointKeyType.Cvr:
                    return new EndpointKeytype(EndpointKeyTypeCode.cvr);
                case OioublEndpointKeyType.Ean:
                    return new EndpointKeytype(EndpointKeyTypeCode.ean);
                default:
                    throw new UnhandledOioublEndpointKeyTypeException(_oioublEndpointKeyType);
            }
        }

        /// <summary>
        /// Returns the corresponding oioubl endpoint key type
        /// </summary>
        /// <param name="endpointKeyTypeCode"></param>
        /// <returns></returns>
        public OioublEndpointKeyType GetOioublEndpointKeyType(EndpointKeyTypeCode endpointKeyTypeCode) {
            switch (endpointKeyTypeCode) {
                case EndpointKeyTypeCode.cvr:
                    return OioublEndpointKeyType.Cvr;
                case EndpointKeyTypeCode.ean:
                    return OioublEndpointKeyType.Ean;
                default:
                    throw new UnhandledArsEndpointKeyTypeCodeException(endpointKeyTypeCode);
            }
        }


        /// <summary>
        /// Compares the provided EndpointKeytypeCode and endpointKey with the current instance
        /// </summary>
        /// <param name="endpointKeytypeCode"></param>
        /// <param name="endpointKey"></param>
        /// <returns></returns>
        internal bool Equals(EndpointKeyTypeCode endpointKeytypeCode, IIdentifier endpointKey) {
            OioublEndpointKeyType oioublEndpointKeyType = GetOioublEndpointKeyType(endpointKeytypeCode);
            if (!_oioublEndpointKeyType.Equals(oioublEndpointKeyType)) return false;

            string keyValue = endpointKey.GetAsString();
            if (!_oioublEndpointKeyValue.Equals(keyValue)) return false;

            return true;
        }
    }
}
