// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Text.RegularExpressions;

namespace Azure.Iot.Operations.Protocol
{
    /// <summary>
    /// A CloudEvent that includes the DataContentType field.
    /// </summary>
    public class ExtendedCloudEvent : CloudEvent
    {
        /// <summary>
        ///  Content type of data value. This attribute enables data to carry any type of content,
        ///  whereby format and encoding might differ from that of the chosen event format.
        /// </summary>
        /// <remarks>
        /// This value must either be null, whitespace, or satisfy the regex of <see cref="ValidDataContentTypeRegex"/>
        /// </remarks>
        public string? DataContentType
        {
            get
            {
                return _dataContentType;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    _dataContentType = value;
                    return;
                }

                if (!Regex.IsMatch(value, ValidDataContentTypeRegex))
                {
                    throw new ArgumentException("The provided data content type does not satisfy the regex of " + ValidDataContentTypeRegex);
                }

                _dataContentType = value;
            }
        }

        private string? _dataContentType;

        public const string ValidDataContentTypeRegex = "^([-a-z]+)/([-a-z0-9.]+)(\\+([-a-z0-9.]+))?(;.*)?$";

        /// <summary>
        /// Initializes a new instance of the <see cref="ExtendedCloudEvent"/> class.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="type">The type of the event.</param>
        /// <param name="specversion">The version of the CloudEvents specification.</param>
        public ExtendedCloudEvent(Uri source, string type = "", string specversion = "1.0")
            : base(source, type, specversion)
        {
        }
    }
}
