using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyTunnel.Models
{
    /// <summary>
    /// Sequential lists of prompt definitions which will be rendered to the customer.
    /// For Voice sessions, the sequence can be a mix of Text-To-Speech and pre-recorded audio
    /// </summary>
    public class PromptSequence
    {
        /// <summary>
        /// Sequential lists of prompt definitions which will be rendered to the customer.
        /// For Voice sessions, the sequence can be a mix of Text-To-Speech and pre-recorded audio
        /// </summary>
        public List<PromptDefinition> prompts { get; set; }

        public PromptSequence()
        {
            prompts = new List<PromptDefinition>();
        }

        /// <summary>
        /// Return prompt sequence definition in the format used by inContact scripts (PLAY, MENU, ASR actions, etc).
        /// </summary>
        public string getInContactScriptPromptSequenceString()
        {
            StringBuilder result = new StringBuilder();

            bool firstItem = true;
            foreach (PromptDefinition prompt in prompts)
            {
                string promptString = string.Empty;

                if (!string.IsNullOrEmpty(prompt.audioFilePath))
                {
                    promptString = prompt.audioFilePath;
                }
                else if (!string.IsNullOrEmpty(prompt.textToSpeech))
                {
                    // The '%' character indicates TextToSpeech in inContact script syntax (honored by inContact MediaServer)
                    promptString = "%" + prompt.textToSpeech;
                }
                else if (!string.IsNullOrEmpty(prompt.transcript))
                {
                    // The '%' character indicates TextToSpeech in inContact script syntax (honored by inContact MediaServer)
                    promptString = "%" + prompt.textToSpeech;
                }

                // The prompt definition is not legit.
                if (string.IsNullOrEmpty(promptString))
                    continue;

                if (!firstItem)
                {
                    // Add a space to seperate the prompt from the previous
                    result.Append(" ");
                }

                result.Append("\"");

                result.Append(promptString);

                result.Append("\"");

                firstItem = false;
            }

            return result.ToString();
        }
    }

    /// <summary>
    /// Definition of a single prompt in a PromptSequence
    /// </summary>
    public class PromptDefinition
    {
        /// <summary>
        /// Human readable, friendly text representation of the prompt.
        /// If the prompt is text-to-speech with markup for example, this representation is better for historical transcript
        /// If the prompt is a pre-recorded wav file, this value will be used in transcript
        /// </summary>
        public string transcript { get; set; }

        /// <summary>
        /// Audio payload supplied by the bot provider.  The inContact MediaServer will save this file locally, but
        /// the file will only be referenceable during the lifetime of the phone call identified with ContactId 
        /// (once the call ends, the file will be deleted).
        /// The MediaServer will automatically reformulate the "audioFilePath" to reference the local file, and
        /// remove this 'base64EncodedG711ulawWithWavHeader' payload before relaying the response to the ScriptEngine (too much data)
        /// </summary>
        public string base64EncodedG711ulawWithWavHeader { get; set; }

        /// <summary>
        /// Optional path to a pre-recorded wav file.  For Voice sessions, if a valid audioFilePath is supplied, the specified audio will
        /// be rendered instead of Text-To-Speech.
        /// Variables can be used in the path to support Persona variations (different personalities and languages)
        ///
        /// For example a {persona} variable of 'Emma' might be a german female recording for  
        /// '/prompts/{persona}/greeting.wav'
        /// </summary>
        public string audioFilePath { get; set; }

        /// <summary>
        /// Text to speech formated representation of the prompt.  SSML and other mark-up support varies by vendor.
        /// If 'textToSpeech' value is null or empty, the 'transcript' text will be used for TTS 
        /// </summary>
        public string textToSpeech { get; set; }

        /// <summary>
        /// For chat channels for example, this may contain instructions to render an image or a link
        /// </summary>
        public object mediaSpecificObject { get; set; }
    }
}
