/// \file
/// \author Matthew Allen (fret@memecode.com)
/// \brief A checkbox for a list item

#ifndef __LList_ITEM_CHECKBOX_H
#define __LList_ITEM_CHECKBOX_H

#include "lgi/common/List.h"

/// A checkbox suitable for a LListItem.
class LListItemCheckBox : public LListItemColumn
{
public:
	/// Constructor
	LListItemCheckBox
	(
		/// The hosting list item.
		LListItem *host,
		/// The column to draw in.
		int column,
		// The initial value.
		bool value = false
	) : LListItemColumn(host, column)
	{
		Value(value);
	}

	void OnPaintColumn(LItem::ItemPaintCtx &Ctx, int i, LItemColumn *Col)
	{
		LSurface *pDC = Ctx.pDC;
		LRect c(0, 0, 10, 10);
		c.Offset(Ctx.x1 + ((Ctx.X()-c.X())/2), Ctx.y1 + ((Ctx.Y()-c.Y())/2));

		// Box
		pDC->Colour(LColour(L_TEXT));
		pDC->Box(&c);
		c.Inset(1, 1);
		pDC->Colour(LColour(L_WORKSPACE));
		pDC->Rectangle(&c);

		// Value
		if (Value())
		{
			pDC->Colour(LColour(L_TEXT));

			pDC->Line(c.x1+1, c.y1+1, c.x2-1, c.y2-1);
			pDC->Line(c.x1+1, c.y1+2, c.x2-2, c.y2-1);
			pDC->Line(c.x1+2, c.y1+1, c.x2-1, c.y2-2);

			pDC->Line(c.x1+1, c.y2-1, c.x2-1, c.y1+1);
			pDC->Line(c.x1+1, c.y2-2, c.x2-2, c.y1+1);
			pDC->Line(c.x1+2, c.y2-1, c.x2-1, c.y1+2);
		}
	}

	void OnMouseClick(LMouse &m)
	{
		if (m.Down() && m.Left())
		{
			LList *l = GetList();
			List<LListItem> Sel;
			if (l->GetSelection(Sel) && Sel.Length())
			{
				bool v = !Value();
				Sel.Delete(GetItem());

				for (auto i: Sel)
				{
					LListItemColumn *c = GetItemCol(i, GetColumn());
					if (c)
					{
						c->Value(v);
					}
				}

				Value(v);
				GetItem()->Update();
			}
		}
	}
};

#endif
