/*
Copyright 2019 Dr. Thomas Benjamin Senior, Michael Zoeller 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;

namespace EyeSkills.Editor
{
    public class CreateAudioFiles
    {
        /// <summary>
        /// Converts text files in a directory to speech file in the format m4a using OSX's "say" command.
        /// </summary>
        /// <param name="dirPath">Dir path.</param>
        public void ConvertTextFilesToSpeech(string dirPath, bool onlyTextFilesMissingAudio)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(dirPath);

            FileInfo[] fileInf = dirInfo.GetFiles("*.txt");

            foreach (FileInfo fileInfo in fileInf)
            {
                //UnityEngine.Debug.Log("File found " + fileInfo.Name);
                string inputFilePath = fileInfo.FullName;
                string key = Path.GetFileNameWithoutExtension(inputFilePath);
                string outputDirPath = Path.GetDirectoryName(inputFilePath);
                string outputFilePath = outputDirPath + "/" + key + ".aiff";

                if ((onlyTextFilesMissingAudio && !System.IO.File.Exists(outputFilePath)) || !onlyTextFilesMissingAudio)
                {
                    UnityEngine.Debug.Log("Creating audio file for " + fileInfo.Name);
                    StreamReader reader = new StreamReader(inputFilePath);
                    string audioText = reader.ReadToEnd();
                    reader.Close();

                    SpeakToFile3("Daniel", audioText, outputFilePath, outputDirPath);
                    //coroutine = SpeakToFile3("Daniel", audioText, outputFilePath, outputDirPath);
                    //StartCoroutine(coroutine);    
                }
                else
                {
                    //UnityEngine.Debug.Log("Ignoring as audio file already exists");
                }
            }
        }

        /// <summary>
        /// Speaks to file3.  Third attempt to get this osx specific call to "say" working.
        /// </summary>
        /// <returns>The to file3.</returns>
        /// <param name="voice">Voice.</param>
        /// <param name="inputText">Input text.</param>
        /// <param name="outputFile">Output file.</param>
        /// <param name="outputDirPath">Output dir path.</param>
        public void SpeakToFile3(string voice, string inputText, string outputFile, string outputDirPath)
        {
            string arguments = "-v " + voice + " '" + inputText + "' -o " + outputFile;

            ProcessStartInfo processInfo = new ProcessStartInfo("/usr/bin/say", arguments);
            processInfo.UseShellExecute = false;

            var process = Process.Start(processInfo);
            process.WaitForExit();
            process.Close();
        }
    }
}