#include "NFC.h"
#include <SPI.h>
#include <PubSubClient.h>
#include <MFRC522.h>

NFC::NFC(int pin_ss, int pin_rst)
{
    SPI.begin();

    rfid = new MFRC522(pin_ss, pin_rst);
    rfid->PCD_Init();
}

bool NFC::hasNewCard()
{
    return rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20;
}

void NFC::selectCard()
{
    rfid->PICC_Select(&(rfid->uid));
    uid = rfid->uid;
    cardSelected = true;
}
void NFC::deselectCard()
{
    rfid->PICC_HaltA();
    cardSelected = false;
}

bool NFC::isCardLost()
{
    return rfid->PICC_ReadCardSerial() && rfid->uid.uidByte != uid.uidByte;
}

MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte response_len)
{
    return rfid->PCD_TransceiveData(command, command_len, response, &response_len, NULL, 0, true);
}