using System ;
using System.Collections.Generic ;
using System.Runtime.InteropServices ;
using System.Text ;


namespace DxLibDLL
{


	public static partial class DX
	{


		/// <summary> 動画ファイルの再生 </summary>
		public static void PlayMovie( string FileName , int ExRate , DX_PLAYTYPE PlayType )
		{
			lock( locker )
			{
				int result = dx_PlayMovie( FileName , ExRate , PlayType ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_PlayMovie( string FileName , int ExRate , DX_PLAYTYPE PlayType ) ;


		/// <summary> ムービーを開く </summary>
		public static Movie OpenMovieToGraph( string FileName )
		{
			lock( locker )
			{
				return OpenMovieToGraph( FileName , true ) ;
			}
		}

		/// <summary> ムービーを開く </summary>
		public static Movie OpenMovieToGraph( string FileName , bool FullColor )
		{
			lock( locker )
			{
				int result = dx_OpenMovieToGraph( FileName , FullColor ) ;
				if( !( result != -1 ) ) { throw new DX.Exception(); }
				return Movie.FromHandle( result ) ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_OpenMovieToGraph( string FileName , bool FullColor ) ;


		/// <summary> ムービーグラフィックに含まれるムービーの再生を開始する </summary>
		public static void PlayMovieToGraph( Movie GraphHandle )
		{
			PlayMovieToGraph( GraphHandle , DX_PLAYTYPE.BACK ) ;
		}

		/// <summary> ムービーグラフィックに含まれるムービーの再生を開始する </summary>
		public static void PlayMovieToGraph( Movie GraphHandle , DX_PLAYTYPE PlayType )
		{
			PlayMovieToGraph( GraphHandle , PlayType , false ) ;
		}

		/// <summary> ムービーグラフィックに含まれるムービーの再生を開始する </summary>
		public static void PlayMovieToGraph( Movie GraphHandle , DX_PLAYTYPE PlayType , bool SysPlay )
		{
			lock( locker )
			{
				int result = dx_PlayMovieToGraph( Movie.ToHandle( GraphHandle ) , PlayType , SysPlay ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_PlayMovieToGraph( int GraphHandle , DX_PLAYTYPE PlayType , bool SysPlay ) ;


		/// <summary> ムービーグラフィックに含まれるムービーの再生をストップする </summary>
		public static void PauseMovieToGraph( Movie GraphHandle )
		{
			PauseMovieToGraph( GraphHandle , false ) ;
		}

		/// <summary> ムービーグラフィックに含まれるムービーの再生をストップする </summary>
		public static void PauseMovieToGraph( Movie GraphHandle , bool SysPause )
		{
			lock( locker )
			{
				int result = dx_PauseMovieToGraph( Movie.ToHandle( GraphHandle ) , SysPause ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_PauseMovieToGraph( int GraphHandle , bool SysPause ) ;


		/// <summary> ムービーのフレームを進める、戻すことは出来ない( ムービーが停止状態で、且つ Ogg Theora のみ有効 ) </summary>
		public static void AddMovieFrameToGraph( Movie GraphHandle , int FrameNum )
		{
			lock( locker )
			{
				int result = dx_AddMovieFrameToGraph( Movie.ToHandle( GraphHandle ) , FrameNum ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_AddMovieFrameToGraph( int GraphHandle , int FrameNum ) ;


		/// <summary> ムービーの再生位置を設定する(ミリ秒単位) </summary>
		public static void SeekMovieToGraph( Movie GraphHandle , int Time )
		{
			lock( locker )
			{
				int result = dx_SeekMovieToGraph( Movie.ToHandle( GraphHandle ) , Time ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_SeekMovieToGraph( int GraphHandle , int Time ) ;


		/// <summary> ムービーの再生状態を得る </summary>
		public static bool GetMovieStateToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_GetMovieStateToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( result == 0 ) { return false ; }
				else if( result == 1 ) { return true ; }
				else { throw new DX.Exception() ; }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_GetMovieStateToGraph( int GraphHandle ) ;


		/// <summary> ムービーのボリュームをセットする(0〜10000) </summary>
		public static void SetMovieVolumeToGraph( int Volume , Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_SetMovieVolumeToGraph( Volume , Movie.ToHandle( GraphHandle ) ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_SetMovieVolumeToGraph( int Volume , int GraphHandle ) ;


		/// <summary> ムービーの基本イメージデータを取得する </summary>
		public static BaseImage GetMovieBaseImageToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				IntPtr result = dx_GetMovieBaseImageToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result != IntPtr.Zero ) ) { throw new DX.Exception(); }
				BASEIMAGE BaseImage_ = ( BASEIMAGE )( Marshal.PtrToStructure( result , typeof( BASEIMAGE ) ) ) ;
				int GraphDataSize_ = BaseImage_.Height * BaseImage_.Pitch ;
				IntPtr GraphData_ = DxAlloc( GraphDataSize_ ) ;
				CopyMemory( GraphData_ , BaseImage_.GraphData , ( IntPtr )( GraphDataSize_ ) ) ;
				BaseImage_.GraphData = GraphData_ ;
				return new BaseImage( BaseImage_ ) ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern IntPtr dx_GetMovieBaseImageToGraph( int GraphHandle ) ;

	#if UNICODE
		[ DllImport( "user32.dll" , CharSet = CharSet.Unicode ) ]
	#else
		[ DllImport( "user32.dll" , CharSet = CharSet.Ansi ) ]
	#endif
		private static extern void CopyMemory( IntPtr Destination , IntPtr Source , IntPtr Length ) ;


		/// <summary> ムービーの総フレーム数を得る( Ogg Theora でのみ有効 ) </summary>
		public static int GetMovieTotalFrameToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_GetMovieTotalFrameToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_GetMovieTotalFrameToGraph( int GraphHandle ) ;


		/// <summary> ムービーの再生位置を取得する(ミリ秒単位) </summary>
		public static int TellMovieToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_TellMovieToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_TellMovieToGraph( int GraphHandle ) ;


		/// <summary> ムービーの再生位置を取得する(フレーム単位) </summary>
		public static int TellMovieToGraphToFrame( Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_TellMovieToGraphToFrame( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_TellMovieToGraphToFrame( int GraphHandle ) ;


		/// <summary> ムービーの再生位置を設定する(フレーム単位) </summary>
		public static int SeekMovieToGraphToFrame( Movie GraphHandle , int Frame )
		{
			lock( locker )
			{
				int result = dx_SeekMovieToGraphToFrame( Movie.ToHandle( GraphHandle ) , Frame ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_SeekMovieToGraphToFrame( int GraphHandle , int Frame ) ;


		/// <summary> ムービーの１フレームあたりの時間を得る </summary>
		public static long GetOneFrameTimeMovieToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				long result = dx_GetOneFrameTimeMovieToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern long dx_GetOneFrameTimeMovieToGraph( int GraphHandle ) ;


		/// <summary> ムービーのイメージを最後に更新した時間を得る(ミリ秒単位) </summary>
		public static int GetLastUpdateTimeMovieToGraph( Movie GraphHandle )
		{
			lock( locker )
			{
				int result = dx_GetLastUpdateTimeMovieToGraph( Movie.ToHandle( GraphHandle ) ) ;
				if( !( result >= 0 ) ) { throw new DX.Exception(); }
				return result ;
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_GetLastUpdateTimeMovieToGraph( int GraphHandle ) ;


		/// <summary> 動画ファイルの右半分をアルファ情報として扱うかどうかをセットする( TRUE:アルファ情報として扱う ) </summary>
		public static void SetMovieRightImageAlphaFlag( bool Flag )
		{
			lock( locker )
			{
				int result = dx_SetMovieRightImageAlphaFlag( Flag ) ;
				if( !( result == 0 ) ) { throw new DX.Exception(); }
			}
		}

	#if UNICODE
		[ DllImport( "DxLibW.dll" , CharSet = CharSet.Unicode , ExactSpelling = true ) ]
	#else
		[ DllImport( "DxLib.dll" , CharSet = CharSet.Ansi , ExactSpelling = true ) ]
	#endif
		private static extern int dx_SetMovieRightImageAlphaFlag( bool Flag ) ;
	}
}
