using System;
using Decal.Interop.Controls;

namespace Decal.Adapter.Wrappers;

public class ListWrapper : ControlWrapperBase<ListClass>
{
	private EventHandler<ControlEventArgs> evtDestroy;

	private EventHandler<ListSelectEventArgs> evtSelect;

	public ListRow this[int row]
	{
		get
		{
			if (row >= 0 && row < base.Control.Count)
			{
				return new ListRow(this, row);
			}
			return null;
		}
	}

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

	public int RowEstimate
	{
		set
		{
			base.Control.RowEstimate = value;
		}
	}

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

	public int RowCount => base.Control.Count;

	public int ColCount => base.Control.CountCols;

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

	protected override void Dispose(bool disposing)
	{
		if (disposing)
		{
			if (evtDestroy != null)
			{
				evtDestroy = (EventHandler<ControlEventArgs>)Delegate.Remove(evtDestroy, evtDestroy);
				base.Control.Destroy -= DestroyEvent;
			}
			if (evtSelect != null)
			{
				evtSelect = (EventHandler<ListSelectEventArgs>)Delegate.Remove(evtSelect, evtSelect);
				base.Control.Change -= SelectEvent;
			}
		}
		base.Dispose(disposing);
	}

	internal int AddRow()
	{
		return base.Control.AddRow();
	}

	internal object get_Data(int row, int col, int subVal)
	{
		return base.Control.get_Data(col, row, subVal);
	}

	internal void set_Data(int row, int col, int subVal, ref object val)
	{
		base.Control.set_Data(col, row, subVal, ref val);
	}

	internal int get_Color(int row, int col)
	{
		return base.Control.get_Color(col, row);
	}

	internal void set_Color(int row, int col, int color)
	{
		base.Control.set_Color(col, row, color);
	}

	internal int get_ColWidth(int col)
	{
		return base.Control.get_ColumnWidth(col);
	}

	internal void set_ColWidth(int col, int width)
	{
		base.Control.set_ColumnWidth(col, width);
	}

	public ListRow Add()
	{
		return new ListRow(this, AddRow());
	}

	public ListRow Insert(int row)
	{
		base.Control.InsertRow(row);
		return new ListRow(this, row);
	}

	public void JumpToPosition(int row)
	{
		base.Control.JumpToPosition(row);
	}

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

	public void Delete(int index)
	{
		base.Control.DeleteRow(index);
	}

	private void SelectEvent(int ID, int Col, int Row)
	{
		if (evtSelect != null)
		{
			evtSelect(new ListColumn(this, Row, Col), new ListSelectEventArgs(ID, Row, Col));
		}
	}

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