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

namespace pcd_g1
{
	delegate void StoryEndedEventHandler(IStoryboard story);

	interface IStoryboard 
	{
		void Run();
		event StoryEndedEventHandler OnStoryEnded;
	}

	class Storyboard : IStoryboard
	{
		public string Temp;

		private List<IStoryboard> contents;
		private int index = 0;
		public Storyboard(params IStoryboard[] content)
		{
			contents = new List<IStoryboard>();
			contents.AddRange(content);
			clear();
		}

		public void Add(IStoryboard item)
		{
			if (!isRunning)
			{
				contents.Add(item);
			}
		}

		public event StoryEndedEventHandler OnStoryEnded;

		private bool isRunning = false;
		public void Run()
		{
			if (!isRunning)
			{
				isRunning = true;
				next();
			}
		}

		private bool next()
		{
			if (index >= contents.Count)
			{
				if (this.OnStoryEnded != null)
					this.OnStoryEnded(this);
				clear();
				isRunning = false;
				return true;
			}

			IStoryboard current = contents[index];
			index++;
			current.OnStoryEnded += StoryBoard_StoryEnded;
			current.Run();
			
			return false;
		}

		private void clear()
		{
			isRunning = false;
			index = 0;
		}

		private void StoryBoard_StoryEnded(IStoryboard story)
		{
			story.OnStoryEnded -= StoryBoard_StoryEnded;
			next();
		}
	}

	class MessageStoryBoard : IStoryboard
	{
		private MessageDialogWidghet widghet;
		public MessageStoryBoard(string message)
		{
			widghet = new MessageDialogWidghet(message, true);
		}

		public void Run()
		{
			isRunning = true;
			MainForm.form.Widghets.OnWidghetsEnded += WidghetsManager_WidghetEnded;
			MainForm.form.Widghets.Add(widghet);
		}

		public event StoryEndedEventHandler OnStoryEnded;

		private bool isRunning = false;

		private void WidghetsManager_WidghetEnded(IWidghet instance)
		{
			if (isRunning)
			{
				if (instance == widghet)
				{
					MainForm.form.Widghets.OnWidghetsEnded -= WidghetsManager_WidghetEnded;
					if (OnStoryEnded != null)
						OnStoryEnded(this);
					isRunning = false;
				}
			}
		}
	}

	class CharacterMessageStoryBoard : IStoryboard
	{
		MessageDialogWidghet msgWidghet;
		PictureWidghet picWidghet;

		public CharacterMessageStoryBoard(string message, Bitmap img)
		{
			msgWidghet = new MessageDialogWidghet(message, true);
			picWidghet = new PictureWidghet(20, 231, 64, 64, img);
		}

		public void Run()
		{
			isRunning = true;
			MainForm.form.Widghets.OnWidghetsEnded += WidghetsManager_WidghetEnded;
			MainForm.form.Widghets.Add(msgWidghet);
			MainForm.form.Widghets.Add(picWidghet);
		}

		public event StoryEndedEventHandler OnStoryEnded;

		private bool isRunning = false;

		private void WidghetsManager_WidghetEnded(IWidghet instance)
		{
			if (isRunning)
			{
				if (instance == msgWidghet)
				{
					MainForm.form.Widghets.OnWidghetsEnded -= WidghetsManager_WidghetEnded;
					if (OnStoryEnded != null)
						OnStoryEnded(this);
					MainForm.form.Widghets.Remove(picWidghet);
					isRunning = false;
				}
			}
		}
	}

	class ExecuteStoryBoard<T> : IStoryboard
	{
		public int Temp;
		Action<T> _cmd;
		T _arg;
		public ExecuteStoryBoard(Action<T> cmd, T arg)
		{
			_cmd = cmd;
			_arg = arg;
		}

		bool isRunning = false;
		public void Run()
		{
			if (!isRunning)
			{
				isRunning = true;
				_cmd(_arg);
				if (OnStoryEnded != null) OnStoryEnded(this);
				isRunning = false;
			}
		}

		public event StoryEndedEventHandler OnStoryEnded;
	}

	class SelectStoryBoard : IStoryboard
	{
		public string Temp;
		private InformDialogWidghet mwidghet;
		private SelectDialogWidghet swidghet;
		private IStoryboard okStory;
		private IStoryboard ngStory;

		public SelectStoryBoard(string message, int sx = 400, int sy = 150, int swid = 230, int shgh = 140, 
			IStoryboard okStoryBoard = null, IStoryboard ngStoryBoard = null, params string[] ways)
		{
			mwidghet = new InformDialogWidghet(message);
			swidghet = new SelectDialogWidghet(sx, sy, swid, shgh, ways);
			okStory = okStoryBoard;
			ngStory = ngStoryBoard;
		}

		public int Result { get; private set; }

		bool isRunning = false;
		public void Run()
		{
			isRunning = true;
			MainForm.form.Widghets.OnWidghetsEnded += WidghetsManager_WidghetEnded;
			MainForm.form.Widghets.Add(mwidghet);
			MainForm.form.Widghets.Add(swidghet);
		}

		public event StoryEndedEventHandler OnStoryEnded;

		//bool afetrEventCalled = false;
		public void WidghetsManager_WidghetEnded(IWidghet widghet)
		{
			if (isRunning && widghet == swidghet)
			{
				MainForm.form.Widghets.OnWidghetsEnded -= WidghetsManager_WidghetEnded;
				MainForm.form.Widghets.Remove(mwidghet);
				Result = swidghet.Result;
				if (swidghet.Result == 0 && okStory != null)
				{
					okStory.OnStoryEnded += Widghet_OnStoryEnded;
					okStory.Run();
				}
				else if (swidghet.Result == 1 && ngStory != null)
				{
					ngStory.OnStoryEnded += Widghet_OnStoryEnded;
					ngStory.Run();
				}
				else
				{
					if (OnStoryEnded != null)
						OnStoryEnded(this);
					isRunning = false;
					return;
				}
				//afetrEventCalled = true;
			}
		}

		public void Widghet_OnStoryEnded(IStoryboard story)
		{
			story.OnStoryEnded -= Widghet_OnStoryEnded;
			if (OnStoryEnded != null)
				OnStoryEnded(this);
			isRunning = false;
			//afetrEventCalled = false;
		}
	}
}
