using System;
using Decal.Interop.Controls;

namespace Decal.Adapter.Wrappers;

public class ChoiceWrapper : ControlWrapperBase<ChoiceClass>
{
	private EventHandler<ControlEventArgs> evtDropDown;

	private EventHandler<ControlEventArgs> evtDestroy;

	private EventHandler<IndexChangeEventArgs> evtChange;

	private ChoiceTextIndexer myTextIndex;

	private ChoiceDataIndexer myDataIndex;

	public int Count => base.Control.ChoiceCount;

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

	public bool Dropped
	{
		get
		{
			return base.Control.Dropped;
		}
		set
		{
			base.Control.Dropped = value;
		}
	}

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

	public ChoiceTextIndexer Text => myTextIndex;

	public ChoiceDataIndexer Data => myDataIndex;

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

	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<IndexChangeEventArgs> Change
	{
		add
		{
			if (evtChange == null)
			{
				base.Control.Change += ChangeEvent;
			}
			evtChange = (EventHandler<IndexChangeEventArgs>)Delegate.Combine(evtChange, value);
		}
		remove
		{
			evtChange = (EventHandler<IndexChangeEventArgs>)Delegate.Remove(evtChange, value);
			if (evtChange == null)
			{
				base.Control.Change -= ChangeEvent;
			}
		}
	}

	[CLSCompliant(false)]
	public override void Initialize(object control)
	{
		base.Initialize(control);
		myTextIndex = new ChoiceTextIndexer(base.Control);
		myDataIndex = new ChoiceDataIndexer(base.Control);
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing)
		{
			if (evtDropDown != null)
			{
				evtDropDown = (EventHandler<ControlEventArgs>)Delegate.Remove(evtDropDown, evtDropDown);
				base.Control.DropDown -= DropDownEvent;
			}
			if (evtDestroy != null)
			{
				evtDestroy = (EventHandler<ControlEventArgs>)Delegate.Remove(evtDestroy, evtDestroy);
				base.Control.Destroy -= DestroyEvent;
			}
			if (evtChange != null)
			{
				evtChange = (EventHandler<IndexChangeEventArgs>)Delegate.Remove(evtChange, evtChange);
				base.Control.Change -= ChangeEvent;
			}
			myTextIndex.Dispose();
			myDataIndex.Dispose();
		}
		base.Dispose(disposing);
	}

	public void Add(string display, object data)
	{
		base.Control.AddChoice(display, data);
	}

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

	public void Remove(int index)
	{
		base.Control.RemoveChoice(index);
	}

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

	private void ChangeEvent(int ID, int Index)
	{
		if (evtChange != null)
		{
			evtChange(this, new IndexChangeEventArgs(ID, Index));
		}
	}

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