using Drakengard3MusicMaker.Support;
using System;
using System.IO;
using System.Windows.Forms;

namespace Drakengard3MusicMaker
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            if (!File.Exists("AppHelp.txt"))
            {
                SharedMethods.AppMsgBox("The 'AppHelp.txt' file is missing.\nPlease ensure that this file is present next to the app to use the Help option.", "Warning", MessageBoxIcon.Warning);
            }

            OgVolRadioButton.Checked = true;
            SampleRateNumUpDown.Value = 44100;

            DisableAudiotools();
        }


        private void Mp3BrowseBtn_Click(object sender, EventArgs e)
        {
            OFDInitializer("MP3 Audio File (*.mp3)", out OpenFileDialog mp3PathSelect, $"|{"*.mp3"}");

            if (mp3PathSelect.ShowDialog() == DialogResult.OK)
            {
                var mp3filePath = mp3PathSelect.FileName;
                var mp3TxtBoxText = Path.GetFullPath($"{mp3filePath}");
                Mp3PathTxtBox.Text = mp3TxtBoxText;

                EnableAudiotools();
            }
        }
        private void Mp3PathTxtBox_TextChanged(object sender, EventArgs e)
        {
            EnableAudiotools();
        }


        private void XXXBrowseBtn_Click(object sender, EventArgs e)
        {
            OFDInitializer("XXX Audio File (*.XXX)", out OpenFileDialog xxxPathSelect, $"|{"*.XXX"}");

            if (xxxPathSelect.ShowDialog() == DialogResult.OK)
            {
                var xxxFilePath = xxxPathSelect.FileName;
                var xxxTxtBoxText = Path.GetFullPath($"{xxxFilePath}");
                XXXPathTxtBox.Text = xxxTxtBoxText;

                EnableAudiotools();
            }
        }
        private void XXXPathTxtBox_TextChanged(object sender, EventArgs e)
        {
            EnableAudiotools();
        }


        private void PS3TOCBrowseBtn_Click(object sender, EventArgs e)
        {
            OFDInitializer("PS3TOC Text file", out OpenFileDialog tocPath_select, $"|{"PS3TOC.TXT"}");

            if (tocPath_select.ShowDialog() == DialogResult.OK)
            {
                var tocFilePath = tocPath_select.FileName;
                var tocTxtBoxText = Path.GetFullPath($"{tocFilePath}");
                PS3TOCPathTxtBox.Text = tocTxtBoxText;

                EnableAudiotools();
            }
        }
        private void PS3TOCPathTxtBox_TextChanged(object sender, EventArgs e)
        {
            EnableAudiotools();
        }


        private void ConvertAudiobtn_Click(object sender, EventArgs e)
        {
            try
            {
                ConvertAudiobtn.Text = "Converting....";
                EnableDisableControls(false);

                bool isConvertOk = File.Exists($"{Mp3PathTxtBox.Text}") && File.Exists($"{XXXPathTxtBox.Text}") && File.Exists($"{PS3TOCPathTxtBox.Text}");

                if (isConvertOk)
                {
                    ProcessSCD.OutMp3File = Mp3PathTxtBox.Text;
                    ProcessSCD.Mp3SampleRate = SampleRateNumUpDown.Value;
                    ProcessSCD.Mp3ChannelCount = ChannelCountNumUpDown.Value;
                    ProcessSCD.Mp3LoopStart = LoopStartNumUpDown.Value;
                    ProcessSCD.Mp3LoopEnd = LoopEndNumUpDown.Value;
                    ProcessSCD.CustomVolumeButtonChecked = CustomVolRadioButton.Checked;
                    ProcessSCD.VolumeSliderValue = VolSlider.Value;

                    ProcessSCD.ConvertAudio(XXXPathTxtBox.Text);

                    if (SharedVariables.IsSCDvalid)
                    {
                        ProcessTOC.EditText(PS3TOCPathTxtBox.Text, Path.GetFileName(XXXPathTxtBox.Text));
                    }

                    ConvertAudiobtn.Text = "Convert Audio";
                    EnableDisableControls(true);
                }
                else
                {
                    ConvertAudiobtn.Text = "Convert Audio";
                    EnableDisableControls(true);
                    SharedMethods.AppMsgBox("One or more file paths aren't set correctly or the files themselves does not exist.\nPlease set the correct filepaths for all of the files before trying to convert them.", "Error", MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                SharedMethods.AppMsgBox("" + ex, "Error", MessageBoxIcon.Error);
                ConvertAudiobtn.Text = "Convert Audio";
                EnableDisableControls(true);
            }
        }


        private void DisableAudiotools()
        {
            SampleRateNumUpDown.Enabled = false;
            ChannelCountNumUpDown.Enabled = false;
            ConvertAudiobtn.Enabled = false;
            OgVolRadioButton.Enabled = false;
            CustomVolRadioButton.Enabled = false;
            VolSlider.Enabled = false;
            LoopStartNumUpDown.Enabled = false;
            LoopEndNumUpDown.Enabled = false;
        }


        private void EnableAudiotools()
        {
            if (!string.IsNullOrEmpty(Mp3PathTxtBox.Text) && !string.IsNullOrEmpty(XXXPathTxtBox.Text) && !string.IsNullOrEmpty(PS3TOCPathTxtBox.Text))
            {
                if (File.Exists($"{Mp3PathTxtBox.Text}") && File.Exists($"{XXXPathTxtBox.Text}") && File.Exists($"{PS3TOCPathTxtBox.Text}"))
                {
                    SampleRateNumUpDown.Enabled = true;
                    ChannelCountNumUpDown.Enabled = true;
                    ConvertAudiobtn.Enabled = true;
                    OgVolRadioButton.Enabled = true;
                    CustomVolRadioButton.Enabled = true;
                    LoopStartNumUpDown.Enabled = true;
                    LoopEndNumUpDown.Enabled = true;

                    if (CustomVolRadioButton.Checked.Equals(true))
                    {
                        VolSlider.Enabled = true;
                    }
                }
                else
                {
                    DisableAudiotools();
                }
            }
        }


        private void OFDInitializer(string ofdDesc, out OpenFileDialog ofdVar, string ofdFilter)
        {
            ofdVar = new OpenFileDialog
            {
                Filter = ofdDesc + ofdFilter,
                RestoreDirectory = true
            };
        }


        private void OgVolRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            VolSlider.Enabled = false;
        }
        private void CustomVolRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            VolSlider.Enabled = true;
        }


        private void EnableDisableControls(bool isEnabled)
        {
            Mp3PathTxtBox.Enabled = isEnabled;
            Mp3BrowseBtn.Enabled = isEnabled;
            XXXPathTxtBox.Enabled = isEnabled;
            XXXBrowseBtn.Enabled = isEnabled;
            PS3TOCPathTxtBox.Enabled = isEnabled;
            PS3TOCBrowseBtn.Enabled = isEnabled;
            SampleRateNumUpDown.Enabled = isEnabled;
            ChannelCountNumUpDown.Enabled = isEnabled;
            ConvertAudiobtn.Enabled = isEnabled;
            OgVolRadioButton.Enabled = isEnabled;
            CustomVolRadioButton.Enabled = isEnabled;
            if (CustomVolRadioButton.Checked)
            {
                VolSlider.Enabled = isEnabled;
            }
            LoopStartNumUpDown.Enabled = isEnabled;
            LoopEndNumUpDown.Enabled = isEnabled;
            AboutLinkLabel.Enabled = isEnabled;
            HelpLinkLabel.Enabled = isEnabled;
        }


        private void AboutlinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var aboutBox = new AboutWindow();
            aboutBox.ShowDialog();
        }


        private void HelpLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (File.Exists("AppHelp.txt"))
            {
                try
                {
                    System.Diagnostics.Process.Start("AppHelp.txt");
                }
                catch (Exception ex)
                {
                    SharedMethods.AppMsgBox("Error: " + ex, "Error", MessageBoxIcon.Error);
                }
            }
            else
            {
                SharedMethods.AppMsgBox("Unable to locate the help text file.\nPlease ensure that this text file is present next to the app before using this option.", "Error", MessageBoxIcon.Error);
            }
        }
    }
}