using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Wordinator;

namespace Clue_CS2022
{
	public partial class formInstructions : Form
	{
		enum enuInstructions
		{ 
			Introduction, 
			Equipment, 
			Preparation, 
			Arranging_The_Cards, 
			Dealing_The_Cards, 
			To_Begin, 
			Movement_Of_Tokens, 
			Moving_Into_A_Room, 
			Getting_Out_Of_A_Room, 
			The_Suggestion, 
			Proving_The_Suggestion_True_Or_False, 
			The_Accusation, 
			Winning_The_Game, 
			SecretPassages, 
			Interesting_Notes_And_Hints, 
			User_Interface,			
			About,
			_numInstructions };

		string strDirectory = "";
		enuInstructions eCurrentFile = (enuInstructions)0;
		Font fnt = new Font("blackadder itc", 14);
		Font fntDescribblated = new Font("ink free", 14, FontStyle.Bold | FontStyle.Italic);
		//Timer tmrInit = new Timer();
		classEventController.classEventTicket cEventTicket_Init = null;
		bool bolDeScribblate = false;
		string[] strScribblate = { "Scribblate", "Scribblify", "Scribblificate", "Scribblize", "Run Scribblator", "Override describlator", "run anti describblation device" };
		string[] strDeScribblate = {"Describblate", "deScribblify", "deScribblificate", "deScribblize", "run describblator",
									 "unscribblate", "unScribblify", "unScribblificate", "unScribblize", "run unscribblator",
									 "override scriblator", "run emergency anti describle subroutine", "reset scribblation device"
								   };
		Random rnd = new Random();
		public formInstructions()
		{
			InitializeComponent();
			eCurrentFile = enuInstructions._numInstructions;

			SizeChanged += new EventHandler(formInstructions_SizeChanged);
			lstHeadings.SelectedIndexChanged += new EventHandler(lstHeadings_SelectedIndexChanged);
			Font = fnt;

			findFiles();
			initListBox();

			LoadInstructions((enuInstructions)0);
			cEventTicket_Init = formClue_2022.cEC.Ticket("Instructions_Init", 5, tmrInit_Tick);
			cEventTicket_Init.AutoRepeat = true;
			//tmrInit.Interval = 5;
			//tmrInit.Tick += new EventHandler(tmrInit_Tick);
			//tmrInit.Enabled = true;
		}

		void tmrInit_Tick(object sender, EventArgs e)
		{
			//tmrInit.Enabled = false;
			cEventTicket_Init.AutoRepeat = false;
			Width = (int)(Screen.PrimaryScreen.WorkingArea.Width * .7);
			Height = (int)(Screen.PrimaryScreen.WorkingArea.Height * .7);
			Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
			Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
		}

		void lstHeadings_SelectedIndexChanged(object sender, EventArgs e)
		{
			LoadInstructions((enuInstructions)lstHeadings.SelectedIndex);
		}

		void initListBox()
		{
			lstHeadings.Font = (bolDeScribblate ? fntDescribblated : fnt);
			lstHeadings.Items.Clear();
			for (enuInstructions eInstructionsCounter =(enuInstructions)0; eInstructionsCounter< enuInstructions._numInstructions; eInstructionsCounter++)
			{
				lstHeadings.Items.Add(eInstructionsCounter.ToString().Replace("_", " "));
			}
		}

		void LoadInstructions(enuInstructions eInstructions) { LoadInstructions(eInstructions, false); }
		void LoadInstructions(enuInstructions eInstructions, bool bolForceLoad)
		{
			if (eCurrentFile != eInstructions || bolForceLoad)
			{
				eCurrentFile = eInstructions;
				Text = eCurrentFile.ToString().Replace("_", " ");
				string strFilenameAndDirectory = strDirectory + "\\" + eInstructions.ToString().Replace("_", " ") + ".rtf";
				if (System.IO.File.Exists(strFilenameAndDirectory))
				{
					rtxInstructions.LoadFile(strFilenameAndDirectory);
					runScribblator();
				}
			}
		}

		void runScribblator()
		{
			if (bolDeScribblate)
			{
				rtxInstructions.SelectAll();
				rtxInstructions.SelectionFont = fntDescribblated;
				rtxInstructions.SelectionLength = 0;

				lstHeadings.Font = fntDescribblated;
			}
		}

		void findFiles()
		{
			strDirectory = System.IO.Directory.GetCurrentDirectory() + "\\instructions";
			string strFilename = ((enuInstructions)0).ToString() + ".rtf";
			string strFilenameAndDirectory = strDirectory + "\\" + strFilename;
			while (strDirectory.Length > 0 && !System.IO.File.Exists(strFilenameAndDirectory ))
			{
				int intCutChar = strDirectory.LastIndexOf("\\");
				if (intCutChar > 0)
					strDirectory = strDirectory.Substring(0, intCutChar);
				else
					strDirectory = "";
				strFilenameAndDirectory = strDirectory + "\\" + strFilename;
			}
		}

		void formInstructions_SizeChanged(object sender, EventArgs e)
		{
			lstHeadings.Top = 15;
			lstHeadings.Left = 5;
			if (Width < 400)
				lstHeadings.Width = Width / 2;
			else if (Width < 600)
				lstHeadings.Width = 200;
			else
				lstHeadings.Width = 290;
			lstHeadings.Height = Height - lstHeadings.Top - 25;

			btnScribblate.Top = Height - 40 - btnScribblate.Height;
			btnScribblate.Left = Width - 20 - btnScribblate.Width;

			rtxInstructions.Left = lstHeadings.Left + lstHeadings.Width + 5;
			rtxInstructions.Width = Width - rtxInstructions.Left - 25;
			rtxInstructions.Top = lstHeadings.Top;
			rtxInstructions.Height = btnScribblate.Top - rtxInstructions.Top -5;
		}

		private void btnScribblate_Click(object sender, EventArgs e)
		{
			bolDeScribblate = !bolDeScribblate;
			if (bolDeScribblate)
			{
				runScribblator();
				btnScribblate.Text = strScribblate[(int)(rnd.NextDouble() * 10000) % strScribblate.Length];
			}
			else
			{
				LoadInstructions(eCurrentFile, true);
				btnScribblate.Text = strDeScribblate[(int)(rnd.NextDouble() * 10000) % strDeScribblate.Length];
			}
			initListBox();
			formInstructions_SizeChanged((object)this, new EventArgs());
		}

        //private void InitializeComponent()
        //{
        //    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formInstructions));
        //    this.SuspendLayout();
        //    // 
        //    // formInstructions
        //    // 
        //    this.ClientSize = new System.Drawing.Size(284, 261);
        //    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        //    this.Name = "formInstructions";
        //    this.ResumeLayout(false);

        //}
    }
}
