/**
 **************************************************
 * @file        Inkplate2_HTTPS_With_Certificate.ino
 * @brief       HTTPS image download with certificate validation: securely fetch
 *              a BMP over WiFi and display it on Inkplate 2.
 *
 * @details     This example demonstrates secure HTTP (HTTPS/TLS) downloads on
 *              Inkplate 2 by applying a trusted X.509 certificate before
 *              connecting to a host. After connecting to WiFi, the sketch
 *              installs a PEM certificate using applyHttpsCertificate(), then
 *              downloads and draws a BMP image from an HTTPS URL.
 *
 *              To demonstrate host validation, the sketch then attempts to load
 *              an image from a different domain. That request fails because the
 *              configured certificate does not match the second host.
 *
 *              The display runs in 1-bit (black/white) mode. The drawn image is
 *              a Windows BMP and must use a supported bit depth and no
 *              compression.
 *
 * Requirements:
 * - Board:      Soldered Inkplate 2
 * - Hardware:   Inkplate 2, USB cable
 * - Extra:      WiFi connection + Internet access
 *
 * Configuration:
 * - Boards Manager -> Inkplate Boards -> Soldered Inkplate2
 * - WiFi:           set ssid/password
 * - Certificate:    set the PEM certificate string for the target host
 * - URLs:           update the HTTPS image URL(s) as needed
 *
 * Don't have Inkplate Boards in Arduino Boards Manager?
 * See https://docs.soldered.com/inkplate/10/quick-start-guide/
 *
 * How to use:
 * 1) Enter your WiFi SSID and password.
 * 2) Provide the correct PEM certificate for the HTTPS host you want to access.
 * 3) Upload the sketch.
 * 4) The display shows WiFi connection progress, then downloads and renders the
 *    first BMP image over HTTPS.
 * 5) The second download attempt from a different host is expected to fail due
 *    to certificate mismatch.
 *
 * Expected output:
 * - Display: connection status messages, then the downloaded BMP image.
 * - Display: after clearing, a message indicating the second image will not
 *   load due to an invalid/mismatched certificate.
 *
 * Notes:
 * - Display mode is 1-bit (BW). This example uses full refresh (display()).
 * - BMP requirements: Windows BMP, 1/4/8/24-bit, uncompressed (no RLE).
 * - TLS certificates are host-specific. The certificate you apply must match
 *   the domain you connect to; otherwise the connection or download will fail.
 * - Certificates can expire or change; if downloads stop working, refresh the
 *   certificate for the target host.
 * - For demos only: using setInsecure() would bypass validation, but this
 *   example intentionally validates the host via certificate configuration.
 *
 * Docs:         https://docs.soldered.com/inkplate
 * Support:      https://forum.soldered.com/
 *
 * @author      Soldered
 * @date        2024-03-15
 * @license     GNU GPL V3
 **************************************************/

// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#ifndef ARDUINO_INKPLATE2
#error "Wrong board selection for this example, please select Inkplate 2  in the boards menu."
#endif

#include "Inkplate.h"            //Include Inkplate library to the sketch
Inkplate display; // Create an object on Inkplate library and also set library into 1 Bit mode (BW)

const char ssid[] = "";    // Your WiFi SSID
const char *password = ""; // Your WiFi password

//This is the certificate for the website https://varipass.org
const char* certificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \
"WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \
"ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n" \
"MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n" \
"h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n" \
"0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U\n" \
"A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW\n" \
"T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH\n" \
"B5T0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CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n" \
"4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n" \
"mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n" \
"emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" \
"-----END CERTIFICATE-----\n";

void setup()
{
    display.begin();        // Init Inkplate library (you should call this function ONLY ONCE)
    display.clearDisplay(); // Clear frame buffer of display
    display.display();      // Put clear image on display

    display.print("Connecting to WiFi...");
    display.display();
    display.connectWiFi(ssid, password);
    
    while (!display.isConnected())
    {
        delay(500);
        display.print(".");
        display.display();
    }
    display.println("\nWiFi OK! Downloading...");
    display.display();

    //Apply the certificate previously defined
    display.applyHttpsCertificate(certificate);
    //Here we will draw the image using a valid certificate. Photo taken by: Roberto Fernandez
    if (!display.image.draw("https://varipass.org/neowise_mono.bmp", 0, 0, false, true))
    {
        // If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
        // REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no compression!
        display.println("Image open error");
        display.display();
    }
    display.display();
    delay(3000);
    display.clearDisplay();
    //Next we will try to load an image from a different website, which will not work as the certificate is 
    //not valid for this page
    if (!display.image.draw("https://raw.githubusercontent.com/SolderedElectronics/Inkplate-Arduino-library/master/examples/Inkplate5V2/Advanced/WEB_WiFi/Inkplate5V2_Show_JPG_With_HTTPClient/image.jpg", 0, 100, true, false))
    {
        display.println("This image wont load as the certificate is invalid");
        display.display();
    }
    display.display();

    display.disconnect();
}

void loop()
{
    // Nothing...
}