#include <Arduino.h>
#include "utils.h"
#include "readers.h"
#include "vars.h"

volatile int PN532Reader::irq = 0;
volatile bool PN532Reader::toggleMode = false;
uint8_t PN532Reader::mode = 0;


PN532Reader::PN532Reader(uint8_t ss) {
	pn532 = new Adafruit_PN532(ss, new SPIClass(HSPI));
}

void PN532Reader::begin() {
	pn532->begin();
	irq = 0;
	mode = PN532_MIFARE_ISO14443A;
	pn532->startPassiveTargetIDDetection(mode);
}

void PN532Reader::cycleMode() {
	this->end();
	pn532->begin();
	irq = 0;

	if (mode == PN532_MIFARE_ISO14443A) {
		mode = PN532_FELICA_424;
		Serial.println("Mode is now FELICA");
	}
	else {
		mode = PN532_MIFARE_ISO14443A;
		Serial.println("Mode is now NFC-A");
	}

	pn532->startPassiveTargetIDDetection(mode);
}

void PN532Reader::end() {
	pn532->sendCommandCheckAck(new byte[1]{PN532_COMMAND_POWERDOWN}, 1, 1);
}

bool PN532Reader::isNewCardPresent() {
	if (irq > 1)
		return true;

	if (toggleMode) {
		if (millis() - timer > 500) {
			cycleMode();
			timer = millis();
		}

		toggleMode = false;
	}
	return false;
}

String PN532Reader::getCardUid() {
	if (mode == PN532_MIFARE_ISO14443A) {
		Serial.println("Reading NFC-A UID");
		pn532->readDetectedPassiveTargetID(uid, &uidLength);
	}
	else {
		Serial.println("Reading FeliCa IDm");
		pn532->readDetectedPassiveTargetIDFeliCa(uid, &uidLength);
	}

	Serial.println(uidLength);
	Serial.println(byteArrayAsHexString(uid, uidLength));
	return byteArrayAsHexString(uid, uidLength);
}

void PN532Reader::reset() {
	this->end();
	this->begin();
}

inline bool PN532Reader::canHaveUnstableIdentifier() {
	return true;
}

void PN532Reader::init() {
	pn532->begin();
	uint32_t version = pn532->getFirmwareVersion();
	if (!version) {
		Serial.print("Couldn't find PN53x board");
		return;
		//esp_restart();
	}

	Serial.print("Found chip PN5"); Serial.println((version>>24) & 0xFF, HEX);
	Serial.print("Firmware ver. "); Serial.print((version>>16) & 0xFF, DEC);
	Serial.print('.'); Serial.println((version>>8) & 0xFF, DEC);
}
