/*
 * 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/>.
 *
 */


#ifndef TERMINAL_H
#define TERMINAL_H


#include "gui.h"
#include "widget.h"


enum TerminalColorsE {
	TERMINAL_COLOR_BLACK = 0,
	TERMINAL_COLOR_BLUE,
	TERMINAL_COLOR_GREEN,
	TERMINAL_COLOR_CYAN,
	TERMINAL_COLOR_RED,
	TERMINAL_COLOR_MAGENTA,
	TERMINAL_COLOR_BROWN,
	TERMINAL_COLOR_LIGHT_GRAY,
	TERMINAL_COLOR_DARK_GRAY,
	TERMINAL_COLOR_BRIGHT_BLUE,
	TERMINAL_COLOR_BRIGHT_GREEN,
	TERMINAL_COLOR_BRIGHT_CYAN,
	TERMINAL_COLOR_BRIGHT_RED,
	TERMINAL_COLOR_BRIGHT_MAGENTA,
	TERMINAL_COLOR_YELLOW,
	TERMINAL_COLOR_WHITE,
	TERMINAL_COLOR_COUNT
};

enum TerminalFlagsE {
	TERMINAL_FLAG_DIRTY = 0,
	TERMINAL_FLAG_BLINK_FAST,  // Must be = 1, don't move!
	TERMINAL_FLAG_BLINK_SLOW   // Must be = 2, don't move!
};


typedef struct CellS {
	uint8_t character;
	ColorT  foreground;
	ColorT  background;
	uint8_t flags;
} CellT;

typedef struct TerminalS {
	WidgetT    base;    // Must be first in every widget
	FontT     *font;
	uint8_t    foreground;
	uint8_t    background;
	uint8_t    bold;
	uint8_t    blink;
	uint8_t    reverse;
	uint16_t   cursorX;  // Cursor positions are 1 based.
	uint16_t   cursorY;  // Cursor positions are 1 based.
	uint16_t   saveX;    // Cursor positions are 1 based.
	uint16_t   saveY;    // Cursor positions are 1 based.
	uint16_t   cols;
	uint16_t   rows;
	ColorT     palette[TERMINAL_COLOR_COUNT];
	CellT    **cells;
	uint8_t    escFound;
	uint8_t    inEscape;
	char       number[7];  // Maximum 6 digits / sign.
	uint8_t    numberIndex;
	char     **parameters;
} TerminalT;


void       terminalBackgroundSet(TerminalT *terminal, uint8_t color);
void       terminalCharPrint(TerminalT *terminal, uint8_t c);
void       terminalCursorMove(TerminalT *terminal, uint16_t col, uint16_t row);
void       terminalForegroundSet(TerminalT *terminal, uint8_t color);
WidgetT   *terminalInit(WidgetT *widget, uint16_t cols, uint16_t rows);
TerminalT *terminalNew(uint16_t x, uint16_t y, uint16_t cols, uint16_t rows);
void       terminalScreenClear(TerminalT *terminal);
void       terminalStringPrint(TerminalT *terminal, char *string);


#endif // TERMINAL_H
