using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageProcessor
{
	public partial class Main : Form
	{
		Bitmap frameInjectorOriginalBitmap;
		Color [ , ] frameInjectorCachedBitmapData;
		int frameInjectorBitmapWidth, frameInjectorBitmapHeight;
		List<string> filenames = new List<string> ();

		public Main ()
		{
			InitializeComponent ();
		}

		private void buttonLoadImageInFrameInjector_Click ( object sender, EventArgs e )
		{
			if ( openFileDialog.ShowDialog () == System.Windows.Forms.DialogResult.Cancel ) return;

			if ( frameInjectorOriginalBitmap != null )
				frameInjectorOriginalBitmap.Dispose ();

			frameInjectorOriginalBitmap = new Bitmap ( openFileDialog.FileName );
			FrameInjector.GetBitmapPixels ( frameInjectorOriginalBitmap, out frameInjectorCachedBitmapData,
				out frameInjectorBitmapWidth, out frameInjectorBitmapHeight );

			groupBoxInjectorProperties.Enabled = true;
			buttonExportInjected.Enabled = true;

			SetPreviewImage ();
		}

		private void numFrameBorder_ValueChanged ( object sender, EventArgs e )
		{
			SetPreviewImage ();
		}

		private async void SetPreviewImage ()
		{
			if ( frameInjectorOriginalBitmap == null ) return;

			Image lastBitmap = pictureBoxInjectorPreview.Image;
			/*
			pictureBoxInjectorPreview.Image = await FrameInjector.Injection ( frameInjectorOriginalBitmap,
				( int ) numFrameBorder.Value, labelBackground.BackColor, labelFrameColor.BackColor );
			*/
			pictureBoxInjectorPreview.Image = await FrameInjector.Injection ( frameInjectorCachedBitmapData,
				frameInjectorBitmapWidth, frameInjectorBitmapHeight, ( int ) numFrameBorder.Value,
				labelBackground.BackColor, labelFrameColor.BackColor );
			if ( lastBitmap != null ) lastBitmap.Dispose ();
		}

		private void buttonPickBackground_Click ( object sender, EventArgs e )
		{
			if ( frameInjectorOriginalBitmap == null ) return;

			colorDialog.Color = labelBackground.BackColor;
			if ( colorDialog.ShowDialog () == System.Windows.Forms.DialogResult.Cancel ) return;
			trackBarBackgroundAlpha.Value = 255;
			labelBackground.BackColor = colorDialog.Color;
			labelBackground.Text = labelBackground.BackColor.ToString ();

			SetPreviewImage ();
		}

		private void buttonPickFrameColor_Click ( object sender, EventArgs e )
		{
			if ( frameInjectorOriginalBitmap == null ) return;

			colorDialog.Color = labelFrameColor.BackColor;
			if ( colorDialog.ShowDialog () == System.Windows.Forms.DialogResult.Cancel ) return;
			trackBarFrameColorAlpha.Value = 255;
			labelFrameColor.BackColor = colorDialog.Color;
			labelFrameColor.Text = labelFrameColor.BackColor.ToString ();

			SetPreviewImage ();
		}

		private void trackBarBackgroundAlpha_Scroll ( object sender, EventArgs e )
		{
			if ( frameInjectorOriginalBitmap == null ) return;

			labelBackground.BackColor = Color.FromArgb ( trackBarBackgroundAlpha.Value, labelBackground.BackColor );
			labelBackground.Text = labelBackground.BackColor.ToString ();

			SetPreviewImage ();
		}

		private void trackBarFrameColorAlpha_Scroll ( object sender, EventArgs e )
		{
			if ( frameInjectorOriginalBitmap == null ) return;

			labelFrameColor.BackColor = Color.FromArgb ( trackBarFrameColorAlpha.Value, labelFrameColor.BackColor );
			labelFrameColor.Text = labelFrameColor.BackColor.ToString ();

			SetPreviewImage ();
		}

		private async void buttonExportInjected_Click ( object sender, EventArgs e )
		{
			if ( saveFileDialog.ShowDialog () == System.Windows.Forms.DialogResult.Cancel ) return;

			groupBoxInjectorProperties.Enabled = false;

			Bitmap bitmap = await FrameInjector.Injection ( frameInjectorOriginalBitmap,
				( int ) numFrameBorder.Value, labelBackground.BackColor, labelFrameColor.BackColor );
			bitmap.Save ( saveFileDialog.FileName );

			groupBoxInjectorProperties.Enabled = true;

			bitmap.Dispose ();
		}

		private void listViewFiles_SelectedIndexChanged ( object sender, EventArgs e )
		{
			if ( pictureBoxArrangeItem.Image != null )
			{
				pictureBoxArrangeItem.Image.Dispose ();
				pictureBoxArrangeItem.Image = null;
			}
			if ( listViewFiles.SelectedIndices.Count <= 0 ) return;
			pictureBoxArrangeItem.Image = Image.FromFile ( filenames [ listViewFiles.SelectedIndices [ 0 ] ] );
		}

		private void buttonAdd_Click ( object sender, EventArgs e )
		{
			openFileDialog.Multiselect = true;
			if ( openFileDialog.ShowDialog () != System.Windows.Forms.DialogResult.Cancel )
			{
				foreach ( string filename in openFileDialog.FileNames )
				{
					filenames.Add ( filename );
					listViewFiles.Items.Add ( System.IO.Path.GetFileName ( filename ) );
				}
			}
			openFileDialog.Multiselect = false;
		}

		private void buttonRemove_Click ( object sender, EventArgs e )
		{
			if ( listViewFiles.SelectedIndices.Count <= 0 ) return;
			filenames.RemoveAt ( listViewFiles.SelectedIndices [ 0 ] );
			listViewFiles.Items.RemoveAt ( listViewFiles.SelectedIndices [ 0 ] );
		}

		private void listViewFiles_KeyUp ( object sender, KeyEventArgs e )
		{
			if ( e.KeyCode == Keys.Delete )
			{
				buttonRemove_Click ( listViewFiles, new EventArgs () );
			}
		}

		private void buttonExport_Click ( object sender, EventArgs e )
		{
			if ( saveFileDialog.ShowDialog () == System.Windows.Forms.DialogResult.Cancel ) return;

			Bitmap bitmap = Appender.Append ( radioVertical.Checked, filenames.ToArray () );
			bitmap.Save ( saveFileDialog.FileName );
			bitmap.Dispose ();
		}
	}
}
