using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageProcessor
{
	public static class Appender
	{
		public static Bitmap Append ( bool isVerticalMode, params string [] bitmapFilenames )
		{
			Bitmap retBitmap = null;
			List<Bitmap> imageList = new List<Bitmap> ();
			int maxWidth = 0, maxHeight = 0;
			foreach ( string filename in bitmapFilenames )
				imageList.Add ( new Bitmap ( filename ) );

			switch ( isVerticalMode )
			{
				case true:
					foreach ( Bitmap bitmap in imageList )
					{
						maxWidth = ( bitmap.Width > maxWidth ) ? bitmap.Width : maxWidth;
						maxHeight += bitmap.Height;
					}
					break;

				case false:
					foreach ( Bitmap bitmap in imageList )
					{
						maxWidth += bitmap.Width;
						maxHeight = ( bitmap.Height > maxHeight ) ? bitmap.Height : maxHeight;
					}
					break;
			}

			retBitmap = new Bitmap ( maxWidth, maxHeight );
			Graphics drawer = Graphics.FromImage ( retBitmap );
			drawer.Clear ( Color.Transparent );

			int totalUnit = 0;
			switch ( isVerticalMode )
			{
				case true:
					foreach ( Bitmap bitmap in imageList )
					{
						drawer.DrawImageUnscaledAndClipped ( bitmap,
							new Rectangle ( 0, totalUnit, bitmap.Width, bitmap.Height ) );
						totalUnit += bitmap.Height;
					}
					break;

				case false:
					foreach ( Bitmap bitmap in imageList )
					{
						drawer.DrawImageUnscaledAndClipped ( bitmap,
							new Rectangle ( totalUnit, 0, bitmap.Width, bitmap.Height ) );
						totalUnit += bitmap.Height;
					}
					break;
			}

			foreach ( Bitmap bitmap in imageList )
				bitmap.Dispose ();

			return retBitmap;
		}
	}
}
