using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageProcessor
{
	static class Program
	{
		/// <summary>
		/// 해당 응용 프로그램의 주 진입점입니다.
		/// </summary>
		[STAThread]
		static void Main ( string [] args )
		{
			string exportFilename;
			Bitmap result;

			Process myProcess = Process.GetCurrentProcess ();
			foreach ( ProcessThread thread in myProcess.Threads )
				thread.ProcessorAffinity = myProcess.ProcessorAffinity;

			if ( args.Length > 0 )
			{
				switch ( args [ 0 ].ToLower () )
				{
					case "--help":
					case "-h":
						Console.WriteLine ( "<< Image Frame Injector >>" );
						Console.WriteLine ( "ImageProcessor.exe --frame export.ext original.ext 1 backgroundcolor framecolor" );
						Console.WriteLine ( "<< Image Appender >>" );
						Console.WriteLine ( "ImageProcessor.exe --append -[v|h] export.ext original1.ext original2.ext ..." );
						break;

					case "--frame":
					case "-f":
						exportFilename = args [ 1 ];
						Bitmap originalBitmap = new Bitmap ( args [ 2 ] );
						int frameSize = int.Parse ( args [ 3 ] );
						Color backgroundColor = Color.FromArgb ( int.Parse ( args [ 4 ] ) );
						Color frameColor = Color.FromArgb ( int.Parse ( args [ 5 ] ) );
						result = FrameInjector.Injection ( originalBitmap, frameSize, backgroundColor, frameColor ).Result;
						result.Save ( exportFilename );
						result.Dispose ();
						break;

					case "--append":
					case "-a":
						bool isVerticalMode = false;
						if ( args [ 1 ].ToLower () == "-v" )
							isVerticalMode = true;
						else if ( args [ 1 ].ToLower () == "-h" )
							isVerticalMode = false;
						else
						{
							Console.WriteLine ( "Invalid append mode." );
							return;
						}

						exportFilename = args[2];

						List<string> bitmapFilenames = new List<string> ();
						for ( int i = 3; i < args.Length; i++ )
							bitmapFilenames.Add ( args [ i ] );

						result = Appender.Append ( isVerticalMode, bitmapFilenames.ToArray () );
						result.Save ( exportFilename );
						break;
				}
			}
			else
			{
				Console.WriteLine ( "[ Image Processor v1.0.0.0 ]" );
				Console.WriteLine ( "If you launched in GUI system, You will see Window." );
				Console.WriteLine ( "But you launched in CLI system, You must add arguments like next .line" );
				Console.WriteLine ( "EX) ImageProcessor.exe --help" );
				Application.EnableVisualStyles ();
				Application.SetCompatibleTextRenderingDefault ( false );
				Application.Run ( new Main () );
			}
		}
	}
}
