using System;
using System.Drawing;
using Decal.Adapter.Support;
using Decal.Interop.Controls;

namespace Decal.Adapter.Wrappers;

public class TextBoxWrapper : ControlWrapperBase<EditClass>
{
	private EventHandler<ControlEventArgs> evtDestroy;

	private EventHandler<ControlEventArgs> evtBegin;

	private EventHandler<TextBoxChangeEventArgs> evtChange;

	private EventHandler<TextBoxEndEventArgs> evtEnd;

	public int Caret
	{
		get
		{
			return base.Control.Caret;
		}
		set
		{
			base.Control.Caret = value;
		}
	}

	public string SelectedText
	{
		get
		{
			return base.Control.SelectedText;
		}
		set
		{
			base.Control.SelectedText = value;
		}
	}

	public string Text
	{
		get
		{
			return base.Control.Text;
		}
		set
		{
			base.Control.Text = value;
		}
	}

	public Color TextColor
	{
		get
		{
			return Util.ColorFromBGR(base.Control.TextColor);
		}
		set
		{
			base.Control.TextColor = Util.ColorToBGR(value);
		}
	}

	public event EventHandler<ControlEventArgs> Destroy
	{
		add
		{
			if (evtDestroy == null)
			{
				base.Control.Destroy += DestroyEvent;
			}
			evtDestroy = (EventHandler<ControlEventArgs>)Delegate.Combine(evtDestroy, value);
		}
		remove
		{
			evtDestroy = (EventHandler<ControlEventArgs>)Delegate.Remove(evtDestroy, value);
			if (evtDestroy == null)
			{
				base.Control.Destroy -= DestroyEvent;
			}
		}
	}

	public event EventHandler<ControlEventArgs> Begin
	{
		add
		{
			if (evtBegin == null)
			{
				base.Control.Begin += BeginEvent;
			}
			evtBegin = (EventHandler<ControlEventArgs>)Delegate.Combine(evtBegin, value);
		}
		remove
		{
			evtBegin = (EventHandler<ControlEventArgs>)Delegate.Remove(evtBegin, value);
			if (evtBegin == null)
			{
				base.Control.Begin -= BeginEvent;
			}
		}
	}

	public event EventHandler<TextBoxChangeEventArgs> Change
	{
		add
		{
			if (evtChange == null)
			{
				base.Control.Change += ChangeEvent;
			}
			evtChange = (EventHandler<TextBoxChangeEventArgs>)Delegate.Combine(evtChange, value);
		}
		remove
		{
			evtChange = (EventHandler<TextBoxChangeEventArgs>)Delegate.Remove(evtChange, value);
			if (evtChange == null)
			{
				base.Control.Change -= ChangeEvent;
			}
		}
	}

	public event EventHandler<TextBoxEndEventArgs> End
	{
		add
		{
			if (evtEnd == null)
			{
				base.Control.End += EndEvent;
			}
			evtEnd = (EventHandler<TextBoxEndEventArgs>)Delegate.Combine(evtEnd, value);
		}
		remove
		{
			evtEnd = (EventHandler<TextBoxEndEventArgs>)Delegate.Remove(evtEnd, value);
			if (evtEnd == null)
			{
				base.Control.End -= EndEvent;
			}
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing)
		{
			if (evtBegin != null)
			{
				evtBegin = (EventHandler<ControlEventArgs>)Delegate.Remove(evtBegin, evtBegin);
				base.Control.Begin -= BeginEvent;
			}
			if (evtDestroy != null)
			{
				evtDestroy = (EventHandler<ControlEventArgs>)Delegate.Remove(evtDestroy, evtDestroy);
				base.Control.Destroy -= DestroyEvent;
			}
			if (evtChange != null)
			{
				evtChange = (EventHandler<TextBoxChangeEventArgs>)Delegate.Remove(evtChange, evtChange);
				base.Control.Change -= ChangeEvent;
			}
			if (evtEnd != null)
			{
				evtEnd = (EventHandler<TextBoxEndEventArgs>)Delegate.Remove(evtEnd, evtEnd);
				base.Control.End -= EndEvent;
			}
		}
		base.Dispose(disposing);
	}

	public void Capture()
	{
		base.Control.Capture();
	}

	public void Select(int start, int end)
	{
		base.Control.Select(start, end);
	}

	public void SetMargins(int x, int y)
	{
		base.Control.SetMargins(x, y);
	}

	private void DestroyEvent(int ID)
	{
		if (evtDestroy != null)
		{
			evtDestroy(this, new ControlEventArgs(ID));
		}
	}

	private void BeginEvent(int ID)
	{
		if (evtBegin != null)
		{
			evtBegin(this, new ControlEventArgs(ID));
		}
	}

	private void ChangeEvent(int ID, string text)
	{
		if (evtChange != null)
		{
			evtChange(this, new TextBoxChangeEventArgs(ID, text));
		}
	}

	private void EndEvent(int ID, bool success)
	{
		if (evtEnd != null)
		{
			evtEnd(this, new TextBoxEndEventArgs(ID, success));
		}
	}
}
