/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include <eCxx/eCxx_Server.h>

#include "NodeMCU_Common.h"


//
// The web server
//
static eCxx::WebServer webServer(80);


//
// The init function
//
void eCxx_user_init()
{
    // Initialize application
    initBasicNodeMCUApp();

    // There is no need to enable the 74LVC541 buffers on the eCxx NodeMCU development board because
    // the UART is only used for communication with the host PC

    // Initialize UART
    App::uart().begin(9600);

    // Intialize web server
    webServer.onNotFound( []() {
        // Send response
        webServer.send(404, "text/plain", "Not found");
        // Write to UART
        App::uart().writeStringln( PGM_STR("\nwebServer.onNotFound()") );
        App::uart().writeStringln( webServer.uri()                     );
    } );

    // >>> Made based on Arduino example "PostServer.ino" <<<
#if __GNUC__ >= 5
    #define WPSTR PGM_STR
#else
    // Workaround for compile error in older versions of GCC
    #define WPSTR
#endif

    webServer.on( "/", []() {
        // Send response
        webServer.send(200, WPSTR("text/html"), WPSTR(
            R"EOF(
                <!DOCTYPE html>
                <html>
                    <head>
                        <title>ESP8266 Web Server POST handling</title>
                        <style>
                            body {
                                font-family      : Arial, Helvetica, Sans-Serif;
                                color            : #000088;
                                background-color : #CCCCCC;
                            }
                        </style>
                    </head>
                    <body>
                        <h1>POST plain text to /postplain</h1>
                        <form method='post' enctype='text/plain' action='/postplain'>
                            <input type='text' name='MyTextField' value='My Value'>
                            <br>
                            <input type='submit' value='Submit'>
                        </form>
                        <br>
                        <h1>POST form data to /postform</h1>
                            <form method='post' enctype='application/x-www-form-urlencoded' action='/postform'>
                            <input type='text' name='MyTextField' value='My Value'>
                            <br>
                            <input type='submit' value='Submit'>
                        </form>
                    </body>
                </html>
            )EOF"
        ) );
        // Write to UART
        App::uart().writeStringln( PGM_STR("\nwebServer.on(\"/\")") );
    } );

    webServer.on( "/postplain", []() {
        // Send response
        if(webServer.method() != eCxx::HTTPRequestMethod::Post)
            webServer.send( 405, "text/plain", "Method Not Allowed" );
        else
            webServer.send( 200, "text/plain", "POST Body\n\n" + webServer.argValue("plain") );
        // Write to UART
        App::uart().writeStringln( PGM_STR("\nwebServer.on(\"/postplain\")") );
    } );

    webServer.on( "/postform", []() {
        // Send response
        if(webServer.method() != eCxx::HTTPRequestMethod::Post) {
            webServer.send(405, "text/plain", "Method Not Allowed");
        }
        else {
            eCxx::NodeMCU::WString message = "POST Form\n\n";
            for(eCxx::uint8_t i = 0; i < webServer.argCount(); ++i) {
                message += webServer.argName(i) + " : " + webServer.argValue(i) + "\n";
            }
            webServer.send(200, "text/plain", message);
        }

        // Write to UART
        App::uart().writeStringln( PGM_STR("\nwebServer.on(\"/postform\")") );
    } );

    webServer.begin();
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event
    eCxx::Application::processEvent();

    // Handle the web server client
    webServer.handleClient();
}
