/*
 * 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 "label.h"


static void labelDel(WidgetT **widget);
static void labelMouseEvent(WidgetT *widget, MouseT *mouse, uint16_t x, uint16_t y, uint8_t event);
static void labelPaint(WidgetT *widget, uint8_t enabled, RectT pos);


void labelClickHandlerSet(LabelT *label, widgetCallback callback) {
	label->clicked = callback;
}


void labelColorActiveSet(LabelT *label, ColorT color) {
	label->active = color;
	GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}


void labelColorBackgroundSet(LabelT *label, ColorT color) {
	label->background = color;
	GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}


void labelColorForegroundSet(LabelT *label, ColorT color) {
	label->foreground = color;
	GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}


static void labelDel(WidgetT **widget) {
	LabelT *l = (LabelT *)*widget;

	if (l->title) free(l->title);
}


WidgetT *labelInit(WidgetT *widget, char *title) {
	LabelT *l = (LabelT *)widget;

	l->base.delMethod        = labelDel;
	l->base.paintMethod      = labelPaint;
	l->base.mouseEventMethod = labelMouseEvent;
	l->title                 = NULL;
	l->background            = _guiColor[COLOR_WINDOW_BACKGROUND];
	l->foreground            = _guiColor[COLOR_LABEL_TEXT_INACTIVE];
	l->clicked               = NULL;

	labelTitleSet(l, title);

	return widget;
}


static void labelMouseEvent(WidgetT *widget, MouseT *mouse, uint16_t x, uint16_t y, uint8_t event) {
	LabelT *l = (LabelT *)widget;

	(void)x;
	(void)y;
	(void)mouse;

	// Label pressed?
	if (event == MOUSE_EVENT_LEFT_HOLD) {
		if (!GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE)) {
			GUI_SET_FLAG(widget, WIDGET_FLAG_ACTIVE);
			GUI_SET_FLAG(widget, WIDGET_FLAG_DIRTY);
		}
	} else {
		if (GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE)) {
			GUI_CLEAR_FLAG(widget, WIDGET_FLAG_ACTIVE);
			GUI_SET_FLAG(widget, WIDGET_FLAG_DIRTY);
		}
	}

	// Fire callback on mouse up.
	if (event == MOUSE_EVENT_LEFT_UP) {
		if (l->clicked) guiPendingEventAdd(widget, l->clicked);
	}
}


LabelT *labelNew(uint16_t x, uint16_t y, char *title) {
	LabelT   *label  = (LabelT *)malloc(sizeof(LabelT));
	WidgetT  *widget = NULL;
	uint16_t  h      = fontHeightGet(_guiFont);

	if (!label) return NULL;

	// Width is set in Init
	widget = widgetInit(W(label), MAGIC_LABEL, x, y, 0, h, 0, 0, 0, 0);
	if (!widget) {
		free(label);
		return NULL;
	}

	label = (LabelT *)labelInit((WidgetT *)label, title);

	return label;
}


static void labelPaint(WidgetT *widget, uint8_t enabled, RectT pos) {
	LabelT   *l    = (LabelT *)widget;
	ColorT    text = GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE) ? l->active : l->foreground;

	// Draw title.
	fontRender(_guiFont, l->title, text, l->background, pos.x, pos.y);
}


void labelTitleSet(LabelT *label, char *title) {
	if (label->title) free(label->title);
	label->title = strdup(title);
	label->base.pos.w = (strlen(title) * fontWidthGet(_guiFont));
	GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}
