/*
 * Kangaroo Punch MultiPlayer Game Server Mark II
 * Copyright (C) 2020-2021  Scott Duensing
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */


#include "widget.h"
#include "window.h"


uint8_t widgetEnableGet(WidgetT *widget) {
	return GUI_GET_FLAG(widget, WIDGET_FLAG_DISABLED) ? 0 : 1;
}


void widgetEnableSet(WidgetT *widget, uint8_t enabled) {
	// ***TODO*** Should also set children.
	// We did "DISABLED" for the flag so that "flags = 0" (the default) would enable widgets.
	if (enabled) {
		GUI_CLEAR_FLAG(widget, WIDGET_FLAG_DISABLED);
	} else {
		GUI_SET_FLAG(widget, WIDGET_FLAG_DISABLED);
	}
	// This needs to mark itself, it's parent, and all it's parent's children dirty.
	guiParentAndChildrenDirtySet(widget);
}


uint16_t widgetHeightGet(WidgetT *widget) {
	return widget->pos.h;
}


WidgetT *widgetInit(WidgetT *widget, uint8_t magic, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t marginX, uint16_t marginY, uint16_t marginX2, uint16_t marginY2) {
	widget->magic               = magic;
	widget->flags               = 0;
	widget->pos.x               = x;
	widget->pos.y               = y;
	widget->pos.w               = w;
	widget->pos.h               = h;
	widget->margin.x            = marginX;
	widget->margin.y            = marginY;
	widget->margin.w            = marginX2;
	widget->margin.h            = marginY2;
	widget->clip.x              = 0;
	widget->clip.y              = 0;
	widget->clip.w              = 0;
	widget->clip.h              = 0;
	widget->surface             = NULL;
	widget->children            = NULL;
	widget->parent              = NULL;
	widget->window              = NULL;
	widget->delMethod           = NULL;
	widget->focusMethod         = NULL;
	widget->keyboardEventMethod = NULL;
	widget->paintMethod         = NULL;
	widget->mouseEventMethod    = NULL;
	widget->userData            = NULL;

	GUI_SET_FLAG(widget, WIDGET_FLAG_DIRTY);

	return widget;
}


uint16_t  widgetMarginBottomGet(WidgetT *widget) {
	return widget->margin.h;
}


uint16_t  widgetMarginLeftGet(WidgetT *widget) {
	return widget->margin.x;
}


uint16_t  widgetMarginRightGet(WidgetT *widget){
	return widget->margin.w;
}


uint16_t  widgetMarginTopGet(WidgetT *widget) {
	return widget->margin.y;
}


WidgetT *widgetNew(uint8_t magic, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t marginX, uint16_t marginY, uint16_t marginX2, uint16_t marginY2) {
	WidgetT *widget = (WidgetT *)malloc(sizeof(WidgetT));

	if (!widget) return NULL;

	widget = widgetInit(widget, magic, x, y, w, h, marginX, marginY, marginX2, marginY2);

	return widget;
}


uint8_t widgetVisibleGet(WidgetT *widget) {
	return GUI_GET_FLAG(widget, WIDGET_FLAG_HIDDEN) ? 0 : 1;
}


void widgetVisibleSet(WidgetT *widget, uint8_t enabled) {
	// We did "HIDDEN" for the flag so that "flags = 0" (the default) would make widgets visible.
	if (enabled) {
		GUI_CLEAR_FLAG(widget, WIDGET_FLAG_HIDDEN);
	} else {
		GUI_SET_FLAG(widget, WIDGET_FLAG_HIDDEN);
	}
	// This needs to mark itself, it's parent, and all it's parent's children dirty.
	guiParentAndChildrenDirtySet(widget);
}


uint16_t widgetWidthGet(WidgetT *widget) {
	return widget->pos.w;
}
