using System;
using System.Windows.Forms;

/*
    Copyright (c) 2010 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
{
    public partial class EditJobAudioTrackDialog : Form
    {
        #region Fields

        private AudioFormat _sourceFormat = AudioFormat.Ac3;
        private AudioFormat _outputFormat = AudioFormat.Vorbis;
        private OutputContainerFormat _package = OutputContainerFormat.Matroska;
        
        #endregion

        #region Properties

        /// <summary>
        /// Gets the audio delay in milliseconds.
        /// </summary>
        public Int32 AudioDelay
        {
            get
            {
                return Convert.ToInt32(numericUpDownDelay.Value);
            }
        }
        /// <summary>
        /// Gets whether to apply Dynamic Range Compression (DRC) when converting
        /// AC3 and DTS audio tracks.
        /// </summary>
        public Boolean AudioDynamicRangeCompression
        {
            get
            {
                return checkBoxAudioDrc.Checked;
            }
        }
        /// <summary>
        /// Gets the audio output format.
        /// </summary>
        public AudioFormat AudioOutputFormat
        {
            get
            {
                return _outputFormat;
            }
        }
        /// <summary>
        /// Gets whether audio levels should be normalized.
        /// </summary>
        public Boolean AudioNormalize
        {
            get
            {
                return checkBoxAudioNormalize.Checked;
            }
        }
        
        #endregion

        #region Methods

        /// <summary>
        /// Creats a new instance of the EditJobAudioTrackDialog class.
        /// </summary>
        /// <param name="source">The source audio format.</param>
        /// <param name="output">The output audio format.</param>
        /// <param name="package">The output container format.</param>
        /// <param name="delay">The audio delay in milliseconds.</param>
        /// <param name="drc">Whether to apply Dynamic Range Compression (DRC) when converting
        /// AC3 and DTS audio tracks.</param>
        /// <param name="normalize">Whether to normalize audio levels.</param>
        public EditJobAudioTrackDialog(AudioFormat source, AudioFormat output, OutputContainerFormat package,
            Int32 delay, bool drc, bool normalize)
        {
            InitializeComponent();

            // Update app icon.
            this.Icon = Properties.Resources.App;

            checkBoxAudioDrc.Checked = drc;
            checkBoxAudioNormalize.Checked = normalize;
            labelFormatValue.Text = Utility.ConvertAudioFormatToName(source);
            numericUpDownDelay.Value = delay;

            _sourceFormat = source;
            _outputFormat = output;
            _package = package;

            comboBoxOutputFormat.Items.Clear();
            comboBoxOutputFormat.Items.AddRange(Utility.ConvertAudioFormatToNames(Utility.GetSupportedAudioOutputFormats(_sourceFormat, _package)));

            // Show first item as selection.
            if (comboBoxOutputFormat.Items.Count > 0)
            {
                comboBoxOutputFormat.Text = Utility.ConvertAudioFormatToName(_outputFormat);
            }

            // Disable for non-convertible formats.
            if (_sourceFormat != AudioFormat.Ac3 && _sourceFormat != AudioFormat.Dts)
            {
                checkBoxAudioDrc.Enabled = false;
                checkBoxAudioNormalize.Enabled = false;
            }
        }

        #endregion

        #region Events

        private void comboBoxOutputFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            _outputFormat = Utility.ConvertAudioFormatFromName(comboBoxOutputFormat.Text);
            
            checkBoxAudioDrc.Enabled = true;
            checkBoxAudioNormalize.Enabled = true;

            // Disable for non-convertible formats.
            if (_sourceFormat == _outputFormat)
            {
                checkBoxAudioDrc.Enabled = false;
                checkBoxAudioNormalize.Enabled = false;
            }

        }

        #endregion
    }
}
