using NAudio.Wave;
using System;
using System.Threading.Tasks;
using System.Speech.Recognition;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace SterowanieGłosemChatGPT
{
    public partial class Form1 : Form
    {
        public ChatGPT chat;
        private bool isRecording = false;
        private WaveInEvent waveEvent;
        private WaveFileWriter waveFileWriter;

        private bool IsRecording
        {
            get
            {
                return isRecording;
            }
            set
            {
                isRecording = value;
            }
        }
        static string odczytajMoveZPliku(string filePath)
        {
            try
            {
                using (var waveFileReader = new WaveFileReader(filePath))
                {
                    var speechRecognitionEngine = new SpeechRecognitionEngine();
                    var grammar = new DictationGrammar();
                    speechRecognitionEngine.LoadGrammar(grammar);
                    speechRecognitionEngine.SetInputToWaveFile(filePath);
                    RecognitionResult result = speechRecognitionEngine.Recognize();
                    speechRecognitionEngine.Dispose(); //zwalnianie pliku
                    waveFileReader.Dispose();
                    if (result != null)
                        return result.Text;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Console.WriteLine("Błąd: " + ex.Message);
            }
            return null;
        }
        public async void prepareChat()
        {
            chat = new ChatGPT("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");
            await chat.createConversation();
            IsRecording = false;
        }
        public Form1()
        {
            InitializeComponent();
            prepareChat();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (!IsRecording)
            {
                waveEvent = new WaveInEvent();
                waveFileWriter = new WaveFileWriter("C:/Users/micha/Studia 2022 - 2023/Rok 2/.Net/SterowanieGłosemChatGPT/SterowanieGłosemChatGPT/mowa.wav", waveEvent.WaveFormat);
                waveEvent.DataAvailable += (s, a) =>
                {
                    waveFileWriter.Write(a.Buffer, 0, a.BytesRecorded);
                };
                waveEvent.RecordingStopped += (s, a) =>
                {
                    waveFileWriter.Dispose(); 
                    waveEvent.Dispose();
                };
                waveEvent.StartRecording();
                recordButton.Text = "Stop recording";
            }
            else
            {
                waveEvent.StopRecording();
                waveFileWriter.Dispose(); 
                string question = odczytajMoveZPliku("C:/Users/micha/Studia 2022 - 2023/Rok 2/.Net/SterowanieGłosemChatGPT/SterowanieGłosemChatGPT/mowa.wav");
                zapytanieUzytkownika.Text = "Zapytanie użytkownika: " + question;
                if (question != null)
                {
                    string answer = await chat.askGPT(question);
                    odpowiedzGPT.Text = "Odpowiedź chatu GPT: " + answer;
                }
                else
                {
                    odpowiedzGPT.Text = "Błąd zapytania...";
                }
                recordButton.Text = "Record";
            }
            IsRecording = !IsRecording;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void odpowiedz_Click(object sender, EventArgs e)
        {

        }

        private void zapytanie_Click(object sender, EventArgs e)
        {

        }
    }
}
