using Amazon;
using Amazon.Polly;
using Amazon.Polly.Model;
using Amazon.Runtime;
using Amazon.Translate;
using Amazon.Translate.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

namespace TranslateMobile
{
	public partial class MainPage : ContentPage
    {
        private static RegionEndpoint regAWS = RegionEndpoint.EUWest1;
        private static BasicAWSCredentials AWSCred = new BasicAWSCredentials("AKIAJNJIHRALDFTPD3CA", "zIojTh7hY8d8B7qgLxqU6sulHT6gmgZ3bUU8FAAb");
        private Dictionary<string, string> langues;
        private AmazonTranslateClient translateClient;
        private List<string> terminologie;
        private string NomTerminologie = "nomspropres";
        private TerminologyDataFormat FormatTerminologie = TerminologyDataFormat.CSV;
        
        public MainPage()
        {
            InitializeComponent();
            langues = new Dictionary<string, string>();
            langues["Automatique"] = "auto";
            langues["Allemand"] = "de";
            langues["Anglais"] = "en";
            langues["Arabe"] = "ar";
            langues["Chinois (simplifié)"] = "zh";
            langues["Chinois (traditionnel)"] = "zh-TW";
            langues["Coréen"] = "ko";
            langues["Danois"] = "da";
            langues["Espagnol"] = "es";
            langues["Finnois"] = "fi";
            langues["Français"] = "fr";
            langues["Hébreux"] = "he";
            langues["Indonésien"] = "id";
            langues["Italien"] = "it";
            langues["Japonais"] = "ja";
            langues["Néerlandais"] = "nl";
            langues["Polonais"] = "pl";
            langues["Portugais"] = "pt";
            langues["Russe"] = "ru";
            langues["Suédois"] = "sv";
            langues["Tchèque"] = "cs";
            langues["Turc"] = "tr";
            terminologie = new List<string>();
            terminologie.Add(NomTerminologie);
            translateClient = new AmazonTranslateClient(AWSCred, regAWS);
            
            

            foreach (KeyValuePair<string, string> langue in langues)
            {
                pkrSource.Items.Add(langue.Key);
            }

            if(NomTerminologie!="")
            {
                ListTerm();
            }
            else
            {
                foreach (KeyValuePair<string, string> langue in langues)
                {
                    if(langue.Value!="auto") pkrDest.Items.Add(langue.Key);
                }
            }
            
            btnTrad.Clicked += OnButtonClicked;
            pkrDest.SelectedIndexChanged += ListDestSelected;
            pkrSource.SelectedIndexChanged += ListSourceSelected;

        }

        private async void ListTerm()
        {
            GetTerminologyRequest termRequest = new GetTerminologyRequest
            {
                Name = NomTerminologie,
                TerminologyDataFormat = FormatTerminologie
            };
            GetTerminologyResponse termResponse = await translateClient.GetTerminologyAsync(termRequest);
            foreach (string lang in termResponse.TerminologyProperties.TargetLanguageCodes)
            {
                var langDest = langues.First(code => code.Value == lang);
                if (langDest.Value == lang)
                {
                    pkrDest.Items.Add(langDest.Key);
                }
            }
        }

        private void ListDestSelected(object sender, EventArgs e)
        {
            if(pkrSource.SelectedItem!=null)
            {
                btnTrad.IsEnabled = true;
            }
        }

        private void ListSourceSelected(object sender, EventArgs e)
        {
            if (pkrDest.SelectedItem != null)
            {
                btnTrad.IsEnabled = true;
            }
        }

        private void OnButtonClicked(object sender, EventArgs e)
        {
            Traduire();
        }

        private async void Traduire()
        {
            var langSource = langues.First(code => code.Key == pkrSource.SelectedItem.ToString());
            var langDest = langues.First(code => code.Key == pkrDest.SelectedItem.ToString());
                        
            TranslateTextRequest translateRequest = new TranslateTextRequest
            {
                SourceLanguageCode = langSource.Value,
                TargetLanguageCode = langDest.Value,
                Text = txtSource.Text,
                TerminologyNames = terminologie
            };

            TranslateTextResponse textResponse = await translateClient.TranslateTextAsync(translateRequest);

            txtDest.Text = textResponse.TranslatedText;
            LanguageCode languageCode;
            VoiceId voiceId;
            switch (langDest.Value)
            {
                case "fr" :
                    languageCode = LanguageCode.FrFR;
                    voiceId = VoiceId.Mathieu;
                    break;
                case "en" :
                    languageCode = LanguageCode.EnGB;
                    voiceId = VoiceId.Matthew;
                    break;
                case "de":
                    languageCode = LanguageCode.DeDE;
                    voiceId = VoiceId.Hans;
                    break;
                case "es":
                    languageCode = LanguageCode.EsES;
                    voiceId = VoiceId.Miguel;
                    break;
                default:
                    languageCode = LanguageCode.EnUS;
                    voiceId = VoiceId.Joey;
                    break;
            }

            string texteATraduire = textResponse.TranslatedText;
            string outputFormat = "mp3";
            TextType typeText = TextType.Text;

            //Création de la requête de lecture
            SynthesizeSpeechRequest speechRequest = new SynthesizeSpeechRequest
            {
                OutputFormat = outputFormat,
                LanguageCode = languageCode,
                VoiceId = voiceId,
                Text = texteATraduire,
                TextType = typeText
            };
            
            string strURL = SynthesizeSpeechUtil.GeneratePresignedUrl(AWSCred, regAWS, speechRequest);
            DependencyService.Get<ITextToSpeech>().Lire(strURL);

        }
        public interface ITextToSpeech
        {
            void Lire(string strURL);
        }

    }
}
