using System;

/*
    Copyright (c) 2011 flixmonk. (http://DVDSwarm.codeplex.com)
    All Rights Reserved.
        
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
        
    * Redistributions of source code must retain the above copyright notice, this list
      of conditions and the following disclaimer.
        
    * Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution. 
        
    * Neither the name of The Flixmonk nor the names of its contributors may be used to
    endorse or promote products derived from this software without specific prior written
    permission.
        
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
    SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace DVDSwarm.Tools.Vorbis
{
    /// <summary>
    /// Utility methods for the Vorbis CLI.
    /// </summary>
    public class Utility
    {
        #region Methods

        /// <summary>
        /// Computes the Vorbis variable bitrate (VBR) value from an EncoderQualityOption enumeration.
        /// </summary>
        /// <param name="value">EncoderQualityOption enumeration to convert.</param>
        /// <returns>An Int32 representing the variable bitrate value in kbps.</returns>
        public static Int32 ComputeVariableBitrate(EncoderQualityOption value)
        {
            // Value indexes correspond to EncoderQualityOption enum.
            Int32[] variableBitrate  = new Int32[5] { 320, 192, 128, 96, 64 };

            return variableBitrate[(Int32)value];
        }

        /// <summary>
        /// Generates CLI arguments for the encoder based on input values. The encoder filename must not be included.
        /// </summary>
        /// <param name="source">The filename of the source audio to encode.</param>
        /// <param name="output">The output audio filename.</param>
        /// <param name="quality">The EncoderQualityOption setting.</param>
        /// <returns>The command line arguments as a string.</returns>
        /// <exception cref="System.ArgumentException">Thrown when source is null or empty.</exception>
        /// <exception cref="System.ArgumentException">Thrown when output is null or empty.</exception>
        public static string GenerateArguments(string source, string output, EncoderQualityOption quality)
        {
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentException("source is null or empty.");
            }

            return "-b " + ComputeVariableBitrate(quality).ToString() + " -o \"" + output + "\" \"" + source + "\"";
        }

        /// <summary>
        /// Parses the Ogg encoder arguments and returns the source and output filenames.
        /// </summary>
        /// <param name="arguments">A string created by GernerateArguments method.</param>
        /// <param name="source">The source WAV filename.</param>
        /// <param name="output">The ouput OGG filename</param>
        public static void ParseFilenames(string arguments, ref string source, ref string output)
        {
            source = string.Empty;
            output = string.Empty;

            if (!arguments.Contains("\""))
            {
                //
                // Filenames not enclosed in quotes.
                // -{mode} {bitrate/1000} -o {ouputFilename} {sourceFilename}
                //

                string[] lines = arguments.Split(new char[] { ' ' });
                if (lines.Length == 5)
                {
                    output = lines[3];
                    source = lines[4];
                }
            }
            else
            {
                //
                // Filenames enclosed in quotes.
                // -{mode} {bitrate/1000} -o "{ouput filename}" "{source filename}"
                //

                Int32 startIndex = arguments.IndexOf('"');
                Int32 endIndex = -1;

                // Find output filename.
                if (startIndex != -1)
                {
                    endIndex = arguments.IndexOf('"', startIndex + 1);
                    if (endIndex != -1)
                    {
                        output = arguments.Substring(startIndex + 1, endIndex - startIndex - 1);
                    }
                }

                // Find source filename.
                if (!string.IsNullOrEmpty(output))
                {
                    startIndex = arguments.IndexOf('"', endIndex + 1);
                    if (startIndex != -1)
                    {
                        endIndex = arguments.IndexOf('"', startIndex + 1);
                        if (endIndex != -1)
                        {
                            source = arguments.Substring(startIndex + 1, endIndex - startIndex - 1);
                        }
                    }
                }
            }
        }

        #endregion
    }
}
