using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using QCRPG.Framework;
using QCRPG.TSL.BT.Properties;

namespace QCRPG.TSL.BT;

public class BTScriptUserControl : UserControlBase, QCRPG.Framework.IObserver<ScriptStartedInfo>, QCRPG.Framework.IObserver<ScriptFinishedInfo>, IConfigurable
{
	protected CombinedScripter combinedScripter;

	private Rectangle dragBoxFromMouseDown;

	private int rowIndexFromMouseDown;

	private int rowIndexOfItemUnderMouseToDrop;

	private int rowIndexofCurrentScript = -1;

	private int columnIndexofCurrentScript = -1;

	private bool scriptRunning;

	private IContainer components;

	private Button addScriptButton;

	private Button deleteScriptButton;

	private DataGridView btScriptDataGridView;

	private DataGridViewImageColumn dataGridViewImageColumn1;

	private DataGridViewImageColumn dataGridViewImageColumn2;

	private DataGridViewTextBoxColumn FileNameColumn;

	private DataGridViewTextBoxColumn FileLocationColumn;

	private DataGridViewTextBoxColumn DescriptionColumn;

	private DataGridViewImageColumn StartColumn;

	public bool ScriptRunning => scriptRunning;

	public BTScriptUserControl()
		: base("BT Script List")
	{
		InitializeComponent();
	}

	~BTScriptUserControl()
	{
		if (combinedScripter != null)
		{
			combinedScripter.UnregisterScriptStartedObserver(this);
			combinedScripter.UnregisterScriptFinishedObserver(this);
		}
	}

	private void addScriptButton_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.Filter = "Tcl Files (*.tcl)|*.tcl|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			string fileName = openFileDialog.FileName;
			string safeFileName = openFileDialog.SafeFileName;
			string directoryName = Path.GetDirectoryName(fileName);
			string text = File.ReadAllText(fileName, Encoding.UTF8);
			int num = text.IndexOf("#");
			string text2 = null;
			string text3 = "No Description";
			if (num > 0)
			{
				text2 = text.Substring(num + 1);
				int num2 = text2.IndexOf("#");
				text3 = text.Substring(num + 1, num + num2);
			}
			btScriptDataGridView.Rows.Add(safeFileName, directoryName, text3);
		}
	}

	private void deleteScriptButton_Click(object sender, EventArgs e)
	{
		if (btScriptDataGridView.SelectedRows.Count > 0)
		{
			btScriptDataGridView.Rows.RemoveAt(btScriptDataGridView.SelectedRows[0].Index);
		}
	}

	private void BTScriptUserControl_Load(object sender, EventArgs e)
	{
	}

	private void btScriptDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
	{
		if (e.ColumnIndex == 3)
		{
			if (!scriptRunning)
			{
				string text = btScriptDataGridView.Rows[e.RowIndex].Cells["FileNameColumn"].Value.ToString();
				string text2 = btScriptDataGridView.Rows[e.RowIndex].Cells["FileLocationColumn"].Value.ToString();
				string filename = text2 + "\\" + text;
				text2 += "\\";
				rowIndexofCurrentScript = e.RowIndex;
				columnIndexofCurrentScript = e.ColumnIndex;
				combinedScripter.CombineOverrideVariableList();
				combinedScripter.RunScriptAsync(filename, text2);
			}
			else if (rowIndexofCurrentScript != -1 && columnIndexofCurrentScript != -1)
			{
				combinedScripter.StopScript();
			}
		}
	}

	private void btScriptDataGridView_MouseMove(object sender, MouseEventArgs e)
	{
		if ((e.Button & MouseButtons.Left) == MouseButtons.Left && dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
		{
			btScriptDataGridView.DoDragDrop(btScriptDataGridView.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
		}
	}

	private void btScriptDataGridView_MouseDown(object sender, MouseEventArgs e)
	{
		rowIndexFromMouseDown = btScriptDataGridView.HitTest(e.X, e.Y).RowIndex;
		if (rowIndexFromMouseDown != -1)
		{
			Size dragSize = SystemInformation.DragSize;
			dragBoxFromMouseDown = new Rectangle(new Point(e.X - dragSize.Width / 2, e.Y - dragSize.Height / 2), dragSize);
		}
		else
		{
			dragBoxFromMouseDown = Rectangle.Empty;
		}
	}

	private void btScriptDataGridView_DragOver(object sender, DragEventArgs e)
	{
		e.Effect = DragDropEffects.Move;
	}

	private void btScriptDataGridView_DragDrop(object sender, DragEventArgs e)
	{
		Point point = btScriptDataGridView.PointToClient(new Point(e.X, e.Y));
		rowIndexOfItemUnderMouseToDrop = btScriptDataGridView.HitTest(point.X, point.Y).RowIndex;
		if (e.Effect == DragDropEffects.Move)
		{
			DataGridViewRow dataGridViewRow = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
			btScriptDataGridView.Rows.RemoveAt(rowIndexFromMouseDown);
			btScriptDataGridView.Rows.Insert(rowIndexOfItemUnderMouseToDrop, dataGridViewRow);
		}
	}

	public void SetCombinedScripter(CombinedScripter CombinedScripter)
	{
		combinedScripter = CombinedScripter;
		combinedScripter.RegisterScriptStartedObserver(this);
		combinedScripter.RegisterScriptFinishedObserver(this);
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		int rowCount = btScriptDataGridView.RowCount;
		configuration.SetInt("BTScriptNumber", rowCount);
		if (rowCount != 0)
		{
			for (int i = 1; i <= rowCount; i++)
			{
				string name = $"BTScriptFileName{i}";
				string name2 = $"BTScriptFileLocation{i}";
				string name3 = $"BTScriptDescription{i}";
				configuration.SetString(name, btScriptDataGridView.Rows[i - 1].Cells["FileNameColumn"].Value.ToString());
				configuration.SetString(name2, btScriptDataGridView.Rows[i - 1].Cells["FileLocationColumn"].Value.ToString());
				configuration.SetString(name3, btScriptDataGridView.Rows[i - 1].Cells["DescriptionColumn"].Value.ToString());
			}
		}
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		int @int = configuration.GetInt("BTScriptNumber", 0);
		if (@int != 0)
		{
			for (int i = 1; i <= @int; i++)
			{
				string name = $"BTScriptFileName{i}";
				string @string = configuration.GetString(name, null);
				string name2 = $"BTScriptFileLocation{i}";
				string string2 = configuration.GetString(name2, null);
				string name3 = $"BTScriptDescription{i}";
				string string3 = configuration.GetString(name3, null);
				btScriptDataGridView.Rows.Add(@string, string2, string3);
			}
		}
	}

	public void NotifyObserver(ScriptStartedInfo scriptStartedInfo)
	{
		scriptRunning = true;
		if (rowIndexofCurrentScript != -1 && columnIndexofCurrentScript != -1)
		{
			btScriptDataGridView.Rows[rowIndexofCurrentScript].Cells[columnIndexofCurrentScript].Value = QCRPG.TSL.BT.Properties.Resources.Stop;
			for (int i = 0; i < btScriptDataGridView.RowCount; i++)
			{
				if (i != rowIndexofCurrentScript)
				{
					btScriptDataGridView.Rows[i].Cells[columnIndexofCurrentScript].Value = QCRPG.TSL.BT.Properties.Resources.Disallow;
				}
			}
		}
		else
		{
			for (int j = 0; j < btScriptDataGridView.RowCount; j++)
			{
				btScriptDataGridView.Rows[j].Cells[3].Value = QCRPG.TSL.BT.Properties.Resources.Disallow;
			}
		}
	}

	public void NotifyObserver(ScriptFinishedInfo scriptFinishedInfo)
	{
		scriptRunning = false;
		for (int i = 0; i < btScriptDataGridView.RowCount; i++)
		{
			btScriptDataGridView.Rows[i].Cells[3].Value = QCRPG.TSL.BT.Properties.Resources.Run;
		}
		rowIndexofCurrentScript = -1;
		columnIndexofCurrentScript = -1;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.addScriptButton = new System.Windows.Forms.Button();
		this.deleteScriptButton = new System.Windows.Forms.Button();
		this.btScriptDataGridView = new System.Windows.Forms.DataGridView();
		this.dataGridViewImageColumn1 = new System.Windows.Forms.DataGridViewImageColumn();
		this.dataGridViewImageColumn2 = new System.Windows.Forms.DataGridViewImageColumn();
		this.FileNameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.FileLocationColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.DescriptionColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.StartColumn = new System.Windows.Forms.DataGridViewImageColumn();
		((System.ComponentModel.ISupportInitialize)this.btScriptDataGridView).BeginInit();
		base.SuspendLayout();
		this.addScriptButton.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.addScriptButton.Location = new System.Drawing.Point(588, 22);
		this.addScriptButton.Name = "addScriptButton";
		this.addScriptButton.Size = new System.Drawing.Size(75, 23);
		this.addScriptButton.TabIndex = 1;
		this.addScriptButton.Text = "Add";
		this.addScriptButton.UseVisualStyleBackColor = true;
		this.addScriptButton.Click += new System.EventHandler(addScriptButton_Click);
		this.deleteScriptButton.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.deleteScriptButton.Location = new System.Drawing.Point(588, 109);
		this.deleteScriptButton.Name = "deleteScriptButton";
		this.deleteScriptButton.Size = new System.Drawing.Size(75, 23);
		this.deleteScriptButton.TabIndex = 2;
		this.deleteScriptButton.Text = "Remove";
		this.deleteScriptButton.UseVisualStyleBackColor = true;
		this.deleteScriptButton.Click += new System.EventHandler(deleteScriptButton_Click);
		this.btScriptDataGridView.AllowDrop = true;
		this.btScriptDataGridView.AllowUserToAddRows = false;
		this.btScriptDataGridView.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.btScriptDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
		this.btScriptDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.btScriptDataGridView.Columns.AddRange(this.FileNameColumn, this.FileLocationColumn, this.DescriptionColumn, this.StartColumn);
		this.btScriptDataGridView.Location = new System.Drawing.Point(4, 4);
		this.btScriptDataGridView.Name = "btScriptDataGridView";
		this.btScriptDataGridView.RowHeadersVisible = false;
		this.btScriptDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
		this.btScriptDataGridView.Size = new System.Drawing.Size(565, 332);
		this.btScriptDataGridView.TabIndex = 3;
		this.btScriptDataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(btScriptDataGridView_CellContentClick);
		this.btScriptDataGridView.DragDrop += new System.Windows.Forms.DragEventHandler(btScriptDataGridView_DragDrop);
		this.btScriptDataGridView.DragOver += new System.Windows.Forms.DragEventHandler(btScriptDataGridView_DragOver);
		this.btScriptDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(btScriptDataGridView_MouseDown);
		this.btScriptDataGridView.MouseMove += new System.Windows.Forms.MouseEventHandler(btScriptDataGridView_MouseMove);
		this.dataGridViewImageColumn1.HeaderText = "";
		this.dataGridViewImageColumn1.Image = QCRPG.TSL.BT.Properties.Resources.Run;
		this.dataGridViewImageColumn1.Name = "dataGridViewImageColumn1";
		this.dataGridViewImageColumn1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridViewImageColumn2.HeaderText = "";
		this.dataGridViewImageColumn2.Image = QCRPG.TSL.BT.Properties.Resources.Stop;
		this.dataGridViewImageColumn2.Name = "dataGridViewImageColumn2";
		this.dataGridViewImageColumn2.ReadOnly = true;
		this.FileNameColumn.FillWeight = 92.7242f;
		this.FileNameColumn.HeaderText = "File Name";
		this.FileNameColumn.Name = "FileNameColumn";
		this.FileNameColumn.ReadOnly = true;
		this.FileLocationColumn.FillWeight = 92.7242f;
		this.FileLocationColumn.HeaderText = "File Location";
		this.FileLocationColumn.Name = "FileLocationColumn";
		this.FileLocationColumn.ReadOnly = true;
		this.DescriptionColumn.FillWeight = 92.7242f;
		this.DescriptionColumn.HeaderText = "Description";
		this.DescriptionColumn.Name = "DescriptionColumn";
		this.DescriptionColumn.ReadOnly = true;
		this.StartColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
		this.StartColumn.FillWeight = 121.8274f;
		this.StartColumn.HeaderText = "";
		this.StartColumn.Image = QCRPG.TSL.BT.Properties.Resources.Run;
		this.StartColumn.Name = "StartColumn";
		this.StartColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
		this.StartColumn.Width = 60;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.btScriptDataGridView);
		base.Controls.Add(this.deleteScriptButton);
		base.Controls.Add(this.addScriptButton);
		base.Name = "BTScriptUserControl";
		base.Size = new System.Drawing.Size(673, 339);
		base.Load += new System.EventHandler(BTScriptUserControl_Load);
		((System.ComponentModel.ISupportInitialize)this.btScriptDataGridView).EndInit();
		base.ResumeLayout(false);
	}
}
