///////////////////////////
// Speech.Synthesizer04.cs
///////////////////////////
using System;
using System.Windows;			//PresentationFramework.dllにある
using System.Windows.Controls;	//PresentationFramework.dllにある
using System.Collections.Generic;
using System.Globalization;
using System.Speech.Synthesis;	//System.Speech.dll（Ref:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Speech\v4.0_4.0.0.0__31bf3856ad364e35\System.Speech.dll）

namespace GuiTTS
{
	public partial class MainWindow : Window
	{
		private const string en = "en-US";
		//private const string ru = "ru-RU";
		private const string jp = "jp-JP";
		private readonly IDictionary<string, string> _messagesByCulture = new Dictionary<string, string>();

		public MainWindow()
		{
			this.Title = "SpeechSynthesizer サンプル";
			this.Width = 320;			//ウィンドウ幅
			this.Height = 280;			//ウィンドウ高
			this.MinWidth = 320;		//ウィンドウ最小幅
			this.MinHeight = 280;		//ウィンドウ最小高
			/* ダイアログタイプ */
			//this.WindowStyle = WindowStyle.ThreeDBorderWindow;
			//this.ResizeMode = ResizeMode.NoResize;
			/* サイズ変更ウィンドウタイプ */
			this.WindowStyle = WindowStyle.SingleBorderWindow;
			this.ResizeMode = ResizeMode.CanResizeWithGrip;
			this.WindowState = WindowState.Normal;
			this.WindowStartupLocation = WindowStartupLocation.CenterScreen;	//Manual, CenterOwnerもある（Manyualの場合の設定方法不明）
			this.Background = SystemColors.ControlBrush;
			InitControls();
			PopulateMessages();
			this.Closing += Window_Closing;
		}

		public void InitControls()
		{
			//スタックパネル（StackPanel）を作成
			StackPanel stackPanel = new StackPanel();
			stackPanel.Width = 300;
			stackPanel.Height = 260;
			//ボタン1
			Button Btn1 = new Button();
			Btn1.Width = 240;
			Btn1.Height = 60;
			Btn1.HorizontalAlignment = HorizontalAlignment.Center;
			Btn1.VerticalAlignment = VerticalAlignment.Center;
			//Btn1.HorizontalContentAlignment = HorizontalAlignment.Left;	//ボタン内のコンテントの位置
			//Btn1.VerticalContentAlignment = VerticalAlignment.Bottom;		//ボタン内のコンテントの位置
			Btn1.Margin = new Thickness(10, 10, 10, 10);	//構造体 Thickness(左, 上, 右, 下)
			Btn1.Content = "Englich";
			Btn1.Click += PromptInEnglish;
			stackPanel.Children.Add(Btn1);
			//ボタン2ボタン
			Button Btn2 = new Button();
			Btn2.Width = 240;
			Btn2.Height = 60;
			Btn2.HorizontalAlignment = HorizontalAlignment.Center;
			Btn2.VerticalAlignment = VerticalAlignment.Center;
			Btn2.Margin = new Thickness(10, 10, 10, 10);	//構造体 Thickness(左, 上, 右, 下)
			Btn2.Content = "Japanese";
			Btn2.Click += PromptInJapanese;
			stackPanel.Children.Add(Btn2);
			//終了ボタン
			Button extBtn = new Button();
			extBtn.Width = 240;
			extBtn.Height = 60;
			extBtn.HorizontalAlignment = HorizontalAlignment.Center;
			extBtn.VerticalAlignment = VerticalAlignment.Center;
			extBtn.Margin = new Thickness(10, 10, 10, 10);	//構造体 Thickness(左, 上, 右, 下)
			extBtn.Content = "Exit";
			extBtn.Click += ExitBtn;
			stackPanel.Children.Add(extBtn);
			//ウィンドウにグリッドを登録
			this.Content = stackPanel;
		}

		[STAThread]
		public static void Main()
		{
			MainWindow mw = new MainWindow();
			Application ap = new Application();	//明示的にインスタンスを作らなければならない
			ap.Run(mw);
		}

		//終了処理
		void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			_messagesByCulture[en] = "Thank you for flying with us today.";
			DoPrompt(en);
			_messagesByCulture[jp] = "ご搭乗、誠に有難うございました。";
			DoPrompt(jp);
		}

		private void PromptInEnglish(object sender, RoutedEventArgs e)
		{
			DoPrompt(en);
		}

/*		private void PromptInRussian(object sender, RoutedEventArgs e)
		{
			DoPrompt(ru);
		}
*/
		private void PromptInJapanese(object sender, RoutedEventArgs e)
		{
			DoPrompt(jp);
		}

		private void ExitBtn(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void DoPrompt(string culture)
		{
			SpeechSynthesizer synthesizer = new SpeechSynthesizer();
			synthesizer.SetOutputToDefaultAudioDevice();
			var builder = new PromptBuilder();
			builder.StartVoice(new CultureInfo(culture));
			builder.AppendText(_messagesByCulture[culture]);
			builder.EndVoice();
			synthesizer.Speak(builder);
			synthesizer.Dispose();
		}

		private void PopulateMessages()
		{
			_messagesByCulture[en] = "For the connection flight 123 to Saint Petersburg, please, proceed to gate A1";
			//_messagesByCulture[ru] = "Для пересадки на рейс 123 в	Санкт-Петербург, пожалуйста, пройдите к выходу A1";
			_messagesByCulture[jp] = "スペインのピーターズバーグへのお乗り継ぎは、Ａ１番ゲートへお進みください。";
		}
	}
}
