using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMSecurity;

namespace QC.QTMApplication;

public class TTDNotes : UserControl
{
	public delegate void OnNotesChangedDelegate(string newNotes);

	private string originalNotes = "";

	private bool appTextChanged;

	private BaseTest test;

	private IContainer components;

	private RichTextBox richTextBox1;

	private ToolStrip toolStripNotes;

	private ToolStripButton Cut;

	private ToolStripButton Copy;

	private ToolStripButton Paste;

	private ToolStripButton Undo;

	private Panel panel1;

	private Panel panel2;

	public BaseTest CurrentNode
	{
		set
		{
			test = value;
			appTextChanged = true;
			richTextFormat(test.Properties.Notes);
			appTextChanged = false;
			if (!QSecurity.IsEngineer || !QSecurity.IsTechnician || (test.Properties.LinkNode && test.Properties.IsLinkActive == true))
			{
				richTextBox1.ReadOnly = true;
				richTextBox1.Enabled = false;
			}
			else
			{
				richTextBox1.ReadOnly = false;
				richTextBox1.Enabled = true;
			}
		}
	}

	public event OnNotesChangedDelegate OnNotesChanged;

	public TTDNotes()
	{
		InitializeComponent();
	}

	private void richTextFormat(string notes)
	{
		string value = "{\\rtf";
		if (!string.IsNullOrEmpty(notes) && notes.Contains(value))
		{
			richTextBox1.Rtf = (originalNotes = notes);
		}
		else
		{
			richTextBox1.Text = (originalNotes = notes);
		}
	}

	private void richTextBox1_TextChanged(object sender, EventArgs e)
	{
		test.Properties.Notes = richTextBox1.Rtf;
		if (!appTextChanged && this.OnNotesChanged != null)
		{
			this.OnNotesChanged(richTextBox1.Rtf);
		}
	}

	private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
	{
		try
		{
			Process.Start(e.LinkText);
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show("Failed to open , " + ex.Message, DialogResult.OK, MessageDialogType.Information);
		}
	}

	private void toolStripNotes_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
	{
		try
		{
			if (!test.Properties.LinkNode || test.Properties.IsLinkActive != true)
			{
				switch (e.ClickedItem.Name)
				{
				case "Undo":
					richTextFormat(originalNotes);
					richTextBox1.Update();
					break;
				case "Copy":
					richTextBox1.Copy();
					richTextBox1.Update();
					break;
				case "Cut":
					richTextBox1.Cut();
					richTextBox1.Update();
					break;
				case "Paste":
					richTextBox1.Paste();
					richTextBox1.Update();
					break;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ultraToolbarsManager1_ToolClick failed, " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void EnableControls(bool enable)
	{
		richTextBox1.ReadOnly = !enable;
		richTextBox1.Enabled = enable;
	}

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

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QC.QTMApplication.TTDNotes));
		this.richTextBox1 = new System.Windows.Forms.RichTextBox();
		this.toolStripNotes = new System.Windows.Forms.ToolStrip();
		this.Cut = new System.Windows.Forms.ToolStripButton();
		this.Copy = new System.Windows.Forms.ToolStripButton();
		this.Paste = new System.Windows.Forms.ToolStripButton();
		this.Undo = new System.Windows.Forms.ToolStripButton();
		this.panel1 = new System.Windows.Forms.Panel();
		this.panel2 = new System.Windows.Forms.Panel();
		this.toolStripNotes.SuspendLayout();
		this.panel1.SuspendLayout();
		this.panel2.SuspendLayout();
		base.SuspendLayout();
		this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.richTextBox1.Location = new System.Drawing.Point(0, 0);
		this.richTextBox1.Name = "richTextBox1";
		this.richTextBox1.Size = new System.Drawing.Size(275, 249);
		this.richTextBox1.TabIndex = 0;
		this.richTextBox1.Text = "";
		this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(richTextBox1_LinkClicked);
		this.richTextBox1.TextChanged += new System.EventHandler(richTextBox1_TextChanged);
		this.toolStripNotes.Dock = System.Windows.Forms.DockStyle.Fill;
		this.toolStripNotes.Items.AddRange(new System.Windows.Forms.ToolStripItem[4] { this.Cut, this.Copy, this.Paste, this.Undo });
		this.toolStripNotes.Location = new System.Drawing.Point(0, 0);
		this.toolStripNotes.Name = "toolStripNotes";
		this.toolStripNotes.Size = new System.Drawing.Size(275, 24);
		this.toolStripNotes.TabIndex = 1;
		this.toolStripNotes.Text = "toolStrip1";
		this.toolStripNotes.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(toolStripNotes_ItemClicked);
		this.Cut.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
		this.Cut.Image = (System.Drawing.Image)resources.GetObject("Cut.Image");
		this.Cut.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.Cut.Name = "Cut";
		this.Cut.Size = new System.Drawing.Size(23, 21);
		this.Cut.Text = "toolStripButton1";
		this.Cut.ToolTipText = "Cut";
		this.Copy.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
		this.Copy.Image = (System.Drawing.Image)resources.GetObject("Copy.Image");
		this.Copy.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.Copy.Name = "Copy";
		this.Copy.Size = new System.Drawing.Size(23, 21);
		this.Copy.Text = "toolStripButton1";
		this.Copy.ToolTipText = "Copy";
		this.Paste.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
		this.Paste.Image = (System.Drawing.Image)resources.GetObject("Paste.Image");
		this.Paste.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.Paste.Name = "Paste";
		this.Paste.Size = new System.Drawing.Size(23, 21);
		this.Paste.Text = "toolStripButton2";
		this.Paste.ToolTipText = "Paste";
		this.Undo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
		this.Undo.Image = (System.Drawing.Image)resources.GetObject("Undo.Image");
		this.Undo.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.Undo.Name = "Undo";
		this.Undo.Size = new System.Drawing.Size(23, 21);
		this.Undo.Text = "toolStripButton3";
		this.Undo.ToolTipText = "Undo";
		this.panel1.Controls.Add(this.toolStripNotes);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
		this.panel1.Location = new System.Drawing.Point(0, 0);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(275, 24);
		this.panel1.TabIndex = 2;
		this.panel2.Controls.Add(this.richTextBox1);
		this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel2.Location = new System.Drawing.Point(0, 24);
		this.panel2.Name = "panel2";
		this.panel2.Size = new System.Drawing.Size(275, 249);
		this.panel2.TabIndex = 3;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
		base.Controls.Add(this.panel2);
		base.Controls.Add(this.panel1);
		base.Name = "TTDNotes";
		base.Size = new System.Drawing.Size(275, 273);
		this.toolStripNotes.ResumeLayout(false);
		this.toolStripNotes.PerformLayout();
		this.panel1.ResumeLayout(false);
		this.panel1.PerformLayout();
		this.panel2.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
