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

namespace pcd_g1
{
	class MessageDialogWidghet : IWidghet
	{
		public int X { get; private set; }
		public int Y { get; private set; }
		public int Width { get; private set; }
		public int Height { get; private set; }
		public bool IsModal { get; private set; }

		public bool IsSleeping
		{
			get
			{
				return false;
			}
		}

		public bool Visibility
		{
			get
			{
				return true;
			}
		}

		const string pressSign = "▼";
		string message;

		public MessageDialogWidghet(string mes, bool showModal = true, int x = 10, int y = 300, int width = 620, int height = 170)
		{
			message = mes; IsModal = showModal; X = x; Y = y; Width = width; Height = height;
			backBrush = new SolidBrush(Color.FromArgb(150, 0, 0, 0));
			font = new Font("MS Gothic", 30);
			lastTime = Environment.TickCount;
		}

		int animIndex = 0;
		int animCnt = 0;
		bool isEndOfLine = false;
		long lastTime = 0;
		public bool Update(int keyStatus)
		{
			if (++animCnt > 60) animCnt = 0;
			int time = Environment.TickCount;
			if (lastTime + 50 > time) return false;
			lastTime = time;
			if (animIndex < message.Length)
			{
				if (isEndOfLine)
				{
					if ((keyStatus & (int) MainForm.KEYBOARD.Z) != 0)
					{
						isEndOfLine = false;
						animIndex++;
						MainForm.PlayEffectSound("focus");
					}
				}
				else
				{
					if (message[animIndex] == '\n')
					{
						isEndOfLine = true;
					}
					else
					{
						animIndex++;
					}
					//MainForm.PlayEffectSound("walk");
				}
			}
			else
			{
				animIndex = 0;
				animCnt = 0;
				MainForm.form.ResetKey();
				return true;
			}
			
			return false;
		}

		Font font;
		Brush backBrush;
		public void Draw(Graphics g, int width, int height)
		{
			string m = message.Substring(0, animIndex);
			while (g.MeasureString(m + pressSign, font, width).Height > height)
			{
				if (m.Contains("\n"))
				{
					m = m.Substring(m.IndexOf("\n") + 1);
				}
				else
				{
					m = m.Substring(1);
				}
			}
			g.FillRectangle(backBrush, 0, 0, width, height);
			g.DrawString(m + ((isEndOfLine && animCnt < 30) ? pressSign : ""), font, Brushes.White,
				new Rectangle(0, 0, width, height));
		}

	}
}
