#include <WiFi.h>
#include <ESPDateTime.h>
#include "Arduino.h"
#include "U8g2lib.h"
#include "logos.h"
#include "utils.h"

void drawCurvedLineV(U8G2 u8g2, int xStart, int yStart, int vert, int horiz) {
	u8g2.drawVLine(xStart, yStart + 1, vert);
	u8g2.drawVLine(xStart + 1, yStart, vert);

	u8g2.drawHLine(xStart, yStart + vert, horiz);
	u8g2.drawHLine(xStart + 1, yStart + vert + 1, horiz);
}

void drawCurvedLine(U8G2 u8g2, int xStart, int yStart, int horiz, int vert) {
	u8g2.drawHLine(xStart, yStart, horiz);
	u8g2.drawHLine(xStart, yStart + 1, horiz);

	u8g2.drawVLine(xStart + horiz, yStart, vert);
	u8g2.drawVLine(xStart + horiz + 1, yStart + 1, vert);
}

void drawLogoAfraPay(U8G2 u8g2) {
	u8g2.setFontMode(1);
	u8g2.setFontDirection(0);

	int xOffset = 12;
	int yOffset = 23;

	u8g2.setFont(u8g2_font_maniac_tr);
	u8g2.drawStr(xOffset, yOffset, "AfRA");

	u8g2.setFont(u8g2_font_tenthinguys_tr);
	u8g2.drawStr(xOffset + 73, yOffset + 3, "pay");

	// uncomment to verify distance to screen gap is equal on both sides
	//u8g2.drawHLine(0, yOffset-12, xOffset);
	//u8g2.drawHLine(127-xOffset-1, yOffset, xOffset);

	drawCurvedLineV(u8g2, xOffset + 69, yOffset, 7, 32);
	drawCurvedLine(u8g2, xOffset + 73, yOffset - 8, 28, 15);
	u8g2.drawPixel(xOffset + 101, yOffset + 7);
}

void drawLogoBitmap(U8G2 u8g2, int width, int height, unsigned char bits[], int yOffset = 0) {
	int offset = (128 - width) / 2;
	u8g2.drawXBM(offset, yOffset, width, height, bits);
}

void drawLogo(U8G2 u8g2, e_logo logo) {
	switch (logo) {
	case LOGO_AFRAPAY:
		drawLogoAfraPay(u8g2);
		break;
	case LOGO_CONTACTLESS:
		drawLogoBitmap(u8g2, contactless_width, contactless_height, contactless_bits);
		break;
	case LOGO_MATECARD:
		drawLogoBitmap(u8g2, matecard_width, matecard_height, matecard_bits, 5);
		u8g2.setFont(u8g2_font_bpixeldouble_tr);
		u8g2.drawStr((126 - u8g2.getStrWidth("matecard")) / 2, 57, "matecard");
		break;
	case LOGO_PENDING:
		drawLogoBitmap(u8g2, pending_width, pending_height, pending_bits, 5);
		break;
	case LOGO_SUCCESS:
		drawLogoBitmap(u8g2, success_width, success_height, success_bits, 5);
		break;
	case LOGO_FAILURE:
		drawLogoBitmap(u8g2, failure_width, failure_height, failure_bits, 5);
		break;
	}
}

void drawStatusText(U8G2 u8g2, String status, String statusRightAligned = "") {
	u8g2.setFont(u8g2_font_bpixel_te);
	u8g2.drawUTF8(0, 61, status.c_str());

	if (!statusRightAligned.isEmpty()) {
		int textWidth = u8g2.getUTF8Width(statusRightAligned.c_str());
		u8g2.drawUTF8(126 - textWidth, 61, statusRightAligned.c_str());
	}
}

void drawFullScreenText(U8G2 u8g2, String textTop, String textBottom) {
	u8g2.setFont(u8g2_font_chargen_92_tr);
	u8g2.drawUTF8((127-u8g2.getUTF8Width(textTop.c_str()))/2, 13, textTop.c_str());
	u8g2.setFont(u8g2_font_maniac_te);
	u8g2.drawUTF8((127-u8g2.getUTF8Width(textBottom.c_str()))/2, 45, textBottom.c_str());
}

void updateOLED(U8G2 u8g2, e_state state, String statusText, String statusTextRightAligned = "") {
	u8g2.clearBuffer();

	switch (state) {
	case STATE_IDLE:
		drawLogo(u8g2, LOGO_AFRAPAY);
		break;
	case STATE_TRANSACT_CARDSCAN:
	case STATE_BALANCE_CARDSCAN:
	case STATE_LINK_CARDSCAN:
		drawLogo(u8g2, LOGO_CONTACTLESS);
		break;
	case STATE_RESULT_SUCCESS:
		drawLogo(u8g2, LOGO_SUCCESS);
		break;
	case STATE_RESULT_FAILURE:
		drawLogo(u8g2, LOGO_FAILURE);
		break;
	case STATE_RESULT_DISPLAY:
		drawFullScreenText(u8g2, splitString(statusText, ':', 0), splitString(statusText, ':', 1) + "€");
		drawStatusText(u8g2, splitString(statusText, ':', 2), statusTextRightAligned);
		u8g2.sendBuffer();
		return;
	case STATE_TRANSACT_VERIFY:
	case STATE_BALANCE_VERIFY:
	case STATE_LINK_VERIFY:
		drawLogo(u8g2, LOGO_PENDING);
		break;
	}

	drawStatusText(u8g2, statusText, statusTextRightAligned);
	u8g2.sendBuffer();
}

void updateOLED(U8G2 u8g2, e_state state) {
	String time = DateTime.format(DateFormatter::TIME_ONLY);
	updateOLED(u8g2, state, time, WiFi.localIP().toString());
}
