#include <Adafruit_Fingerprint.h>


uint8_t getFingerprintID(Adafruit_Fingerprint finger) {
    uint8_t p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
        //Serial.println("Image taken");
        break;
    case FINGERPRINT_NOFINGER:
        //Serial.println("No finger detected");
        return 0;
    case FINGERPRINT_PACKETRECIEVEERR:
        //Serial.println("Communication error");
        return 0;
    case FINGERPRINT_IMAGEFAIL:
        //Serial.println("Imaging error");
        return 0;
    default:
        //Serial.println("Unknown error");
        return 0;
    }

    // OK success!

    p = finger.image2Tz();
    switch (p) {
    case FINGERPRINT_OK:
        //Serial.println("Image converted");
        break;
    case FINGERPRINT_IMAGEMESS:
        //Serial.println("Image too messy");
        return 0;
    case FINGERPRINT_PACKETRECIEVEERR:
        //Serial.println("Communication error");
        return 0;
    case FINGERPRINT_FEATUREFAIL:
        //Serial.println("Could not find fingerprint features");
        return 0;
    case FINGERPRINT_INVALIDIMAGE:
        //Serial.println("Could not find fingerprint features");
        return 0;
    default:
        //Serial.println("Unknown error");
        return 0;
    }

    // OK converted!

    p = finger.fingerSearch();
    if (p == FINGERPRINT_OK) {
        //Serial.println("Found a print match!");
    }
    else if (p == FINGERPRINT_PACKETRECIEVEERR) {
        //Serial.println("Communication error");
        return 0;
    }
    else if (p == FINGERPRINT_NOTFOUND) {
        //Serial.println("Did not find a match");
        return 0;
    }
    else {
        //Serial.println("Unknown error");
        return 0;
    }

    // found a match!
    //Serial.print("Found ID #"); Serial.print(finger.fingerID);
    //Serial.print(" with confidence of "); Serial.println(finger.confidence);
    
    return finger.fingerID;
}
uint8_t getFingerprintEnroll(Adafruit_Fingerprint finger) {

    int p = -1;
    //Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
    while (p != FINGERPRINT_OK) {
        p = finger.getImage();
        switch (p) {
        case FINGERPRINT_OK:
            //Serial.println("Image taken");
            break;
        case FINGERPRINT_NOFINGER:
            Serial.println(".");
            break;
        case FINGERPRINT_PACKETRECIEVEERR:
            //Serial.println("Communication error");
            break;
        case FINGERPRINT_IMAGEFAIL:
            //Serial.println("Imaging error");
            break;
        default:
            //Serial.println("Unknown error");
            break;
        }
    }

    // OK success!

    p = finger.image2Tz(1);
    switch (p) {
    case FINGERPRINT_OK:
        //Serial.println("Image converted");
        return 2;
    case FINGERPRINT_IMAGEMESS:
        //Serial.println("Image too messy");
        return 0;
    case FINGERPRINT_PACKETRECIEVEERR:
        //Serial.println("Communication error");
        return 0;
    case FINGERPRINT_FEATUREFAIL:
        //Serial.println("Could not find fingerprint features");
        return 0;
    case FINGERPRINT_INVALIDIMAGE:
        //Serial.println("Could not find fingerprint features");
        return 0;
    default:
        //Serial.println("Unknown error");
        return 0;
    }
}

uint8_t getFingerprintEnroll1(Adafruit_Fingerprint finger) {

    int p = 0;
    while (p != FINGERPRINT_NOFINGER) {
        p = finger.getImage();
    }
    //Serial.print("ID "); Serial.println(id);
    p = -1;
    //Serial.println("Place same finger again");
    while (p != FINGERPRINT_OK) {
        p = finger.getImage();
        switch (p) {
        case FINGERPRINT_OK:
            //Serial.println("Image taken");
            break;
        case FINGERPRINT_NOFINGER:
            //Serial.print(".");
            break;
        case FINGERPRINT_PACKETRECIEVEERR:
            //Serial.println("Communication error");
            break;
        case FINGERPRINT_IMAGEFAIL:
            //Serial.println("Imaging error");
            break;
        default:
            //Serial.println("Unknown error");
            break;
        }
    }

    // OK success!

    p = finger.image2Tz(2);
    switch (p) {
    case FINGERPRINT_OK:
        //Serial.println("Image converted");
        break;
    case FINGERPRINT_IMAGEMESS:
        //Serial.println("Image too messy");
        return 0;
    case FINGERPRINT_PACKETRECIEVEERR:
        //Serial.println("Communication error");
        return 0;
    case FINGERPRINT_FEATUREFAIL:
        //Serial.println("Could not find fingerprint features");
        return 0;
    case FINGERPRINT_INVALIDIMAGE:
        //Serial.println("Could not find fingerprint features");
        return 0;
    default:
        //Serial.println("Unknown error");
        return 0;
    }

    // OK converted!
    //Serial.print("Creating model for #");  Serial.println(id);

    p = finger.createModel();
    if (p == FINGERPRINT_OK) {
        //Serial.println("Prints matched!");
    }
    else if (p == FINGERPRINT_PACKETRECIEVEERR) {
        //Serial.println("Communication error");
        return 0;
    }
    else if (p == FINGERPRINT_ENROLLMISMATCH) {
        //Serial.println("Fingerprints did not match");
        return 0;
    }
    else {
        //Serial.println("Unknown error");
        return 0;
    }

    //Serial.print("ID "); Serial.println(id);
    p = finger.storeModel(9);
    if (p == FINGERPRINT_OK) {
        //Serial.println("Stored!");
    }
    else if (p == FINGERPRINT_PACKETRECIEVEERR) {
        //Serial.println("Communication error");
        return 0;
    }
    else if (p == FINGERPRINT_BADLOCATION) {
        //Serial.println("Could not store in that location");
        return 0;
    }
    else if (p == FINGERPRINT_FLASHERR) {
        //Serial.println("Error writing to flash");
        return 0;
    }
    else {
        //Serial.println("Unknown error");
        return 0;
    }

    return 3;
}