///////////////////////////////////////////
//Test_MediaPlayer.cs - MediaPlayer Sample
///////////////////////////////////////////
using System;
using System.Windows;			//PresentationFramework.dllにある
using System.Windows.Controls;	//PresentationFramework.dllにある
using System.Windows.Media;		//PresentationCore.dllにある（MediaElement, Brushes構造体使用の為）
/* 参照DLL
RefFile=C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationFramework.dll
RefFile01=C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationCore.dll
RefFile02=C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\WindowsBase.dll
RefFile03=C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Xaml.dll
*/

namespace Test_MediaPlayer
{
	///////////////////////////
	//エントリーポイントクラス
	///////////////////////////
	class MainApp
	{
		[STAThread]
		public static void Main()
		{
			DlgWindow dlg = new DlgWindow();
			Application ap = new Application();	//明示的にインスタンスを作らなければならない
			ap.Run(dlg);
		}
	}

	public partial class DlgWindow : Window	//派生元が"Form"から"Window"となる
	{
		//コントロール
		//private MediaElement me;
		private MediaPlayer mp = null;
		private Canvas canvas = null;
		private VideoDrawing vd = null;
		private Button Btn1, Btn2, Btn3, Btn4, extBtn;
		//ファイル名
		string fName;
		//MediaPlayerコントロール用変数
		bool toggle = true;

		public DlgWindow()
		{
			this.Title = "MediaPlayer サンプル";
			this.Width = 640;
			this.Height = 480;
			this.WindowStyle = WindowStyle.ThreeDBorderWindow;
			this.ResizeMode = ResizeMode.NoResize;
			this.WindowState = WindowState.Normal;
			this.WindowStartupLocation = WindowStartupLocation.CenterScreen;	//Manual, CenterOwnerもある（Manyualの場合の設定方法不明）
			this.Background = SystemColors.ControlBrush;
			InitControls();
		}

		public void InitControls()
		{
			//グリッド（Grid）と縦二列を作成
			Grid grid = new Grid();
			ColumnDefinition colL = new ColumnDefinition();
			ColumnDefinition colR = new ColumnDefinition();
			//列を定義
			//colL.Width = GridLength.Auto;
			colL.Width = new GridLength(540, GridUnitType.Pixel);
			colR.Width = GridLength.Auto;
			//colR.Width = new GridLength(80, GridUnitType.Pixel);
			grid.ColumnDefinitions.Add(colL);
			grid.ColumnDefinitions.Add(colR);

			//スタックパネル（StackPanel）を作成
			StackPanel stackPanel = new StackPanel();
			stackPanel.Width = 80;
			stackPanel.Height = 420;
			stackPanel.HorizontalAlignment = HorizontalAlignment.Right;

			//Canvas
			canvas = new Canvas();
			canvas.Margin = new Thickness(10, 10, 10, 10);	//構造体 Thickness(左, 上, 右, 下)

			//ボタン1
			Btn1 = new Button();
			Btn1.Width = 60;
			Btn1.Height = 24;
			Btn1.HorizontalAlignment = HorizontalAlignment.Right;
			Btn1.VerticalAlignment = VerticalAlignment.Top;
			Btn1.Margin = new Thickness(10, 10, 10, 0);	//構造体 Thickness(左, 上, 右, 下)
			Btn1.Content = "Open";
			Btn1.Click += Button1_Click;
			stackPanel.Children.Add(Btn1);

			//ボタン2ボタン
			Btn2 = new Button();
			Btn2.Width = 60;
			Btn2.Height = 24;
			Btn2.HorizontalAlignment = HorizontalAlignment.Right;
			Btn2.VerticalAlignment = VerticalAlignment.Top;
			Btn2.Margin = new Thickness(10, 10, 10, 0);	//構造体 Thickness(左, 上, 右, 下)
			Btn2.Content = "Play";
			Btn2.Click += Button2_Click;
			stackPanel.Children.Add(Btn2);

			//ボタン3ボタン
			Btn3 = new Button();
			Btn3.Width = 60;
			Btn3.Height = 24;
			Btn3.HorizontalAlignment = HorizontalAlignment.Right;
			Btn3.VerticalAlignment = VerticalAlignment.Top;
			Btn3.Margin = new Thickness(10, 10, 10, 0);	//構造体 Thickness(左, 上, 右, 下)
			Btn3.Content = "Pause";
			Btn3.Click += Button3_Click;
			stackPanel.Children.Add(Btn3);

			//ボタン4ボタン
			Btn4 = new Button();
			Btn4.Width = 60;
			Btn4.Height = 24;
			Btn4.HorizontalAlignment = HorizontalAlignment.Right;
			Btn4.VerticalAlignment = VerticalAlignment.Top;
			Btn4.Margin = new Thickness(10, 10, 10, 0);	//構造体 Thickness(左, 上, 右, 下)
			Btn4.Content = "Stop";
			Btn4.Click += Button4_Click;
			stackPanel.Children.Add(Btn4);

			//終了ボタン
			extBtn = new Button();
			extBtn.Width = 60;
			extBtn.Height = 24;
			extBtn.HorizontalAlignment = HorizontalAlignment.Right;
			extBtn.VerticalAlignment = VerticalAlignment.Bottom;
			extBtn.Margin = new Thickness(10, 10, 10, 10);	//構造体 Thickness(左, 上, 右, 下)
			extBtn.Content = "終了";
			extBtn.Click += extBtn_Click;
			stackPanel.Children.Add(extBtn);

			//グリッド左列にMediaElementを登録
			Grid.SetColumn(canvas, 0);
			grid.Children.Add(canvas);

			//グリッド右列にスタックパネルを登録
			Grid.SetColumn(stackPanel, 1);
			grid.Children.Add(stackPanel);

			//ウィンドウにグリッドを登録
			this.Content = grid;

			//MediaPlayerとVideoDrawing
			mp = new MediaPlayer();
			vd = new VideoDrawing();
		}

		void Button1_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
			ofd.AddExtension = true;
			ofd.DefaultExt = "*.*";
			ofd.Filter = "Media(*.*)|*.*";
			ofd.ShowDialog();
			fName = ofd.FileName;
			//ofd.Dispose();	//WPFでは不要（https://learn.microsoft.com/ja-jp/dotnet/desktop/wpf/windows/how-to-open-common-system-dialog-box?view=netdesktop-8.0）
		}

		void Button2_Click(object sender, RoutedEventArgs e)
		{
			if(fName == "")
				MessageBox.Show("メディアファイルを選択してください。", "エラー", MessageBoxButton.OK, MessageBoxImage.Exclamation);
			else
			{
				mp.Open(new System.Uri(fName, UriKind.Absolute));	//Uri：URI(Uniform Resource Identifier) のオブジェクト表現を可能にし、URI の一部へ簡単にアクセスできるようにします。
				vd = new VideoDrawing();
				vd.Rect = new System.Windows.Rect(0, 0, 200, 200);
				vd.Player = mp;
				DrawingBrush br = new DrawingBrush(vd);
				canvas.Background = br;
				mp.Play();
				Btn3.Content = "Pause";
				toggle = true;
			}
		}

		void Button3_Click(object sender, RoutedEventArgs e)
		{
			if(toggle)
			{
				mp.Pause();
				Btn3.Content = "Resume";
			}
			else
			{
				mp.Play();
				Btn3.Content = "Pause";
			}
			toggle = !toggle;
		}

		void Button4_Click(object sender, RoutedEventArgs e)
		{
			mp.Stop();
			Btn3.Content = "Pause";
			toggle = true;
		}

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