'use strict';

goog.provide('Blockly.Python.wifinetwork');
goog.require('Blockly.Python');

Blockly.Python['wifinetwork_connect_setup'] = function(block) {
    var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
    var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
    Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
    Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
    var ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC) || "";
    var pw = Blockly.Python.valueToCode(block, 'PW', Blockly.Python.ORDER_ATOMIC) || "";
    var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti;\n";
    Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
    var code = "  delay(10);\n" +
        // Start by connecting to a WiFi network
        "  WiFiMulti.addAP(" + ssid + "," + pw + ");\n" +
        "  Serial.print(\"Wait for WiFi... \");\n" +
        "  while(WiFiMulti.run() != WL_CONNECTED) {\n" +
        "    Serial.print(\".\");\n" +
        "    delay(500);\n" +
        "  }\n" +
        "  Serial.println(\"\");\n" +
        "  Serial.println(\"WiFi connected\");";
    Blockly.Python.addSetup("wifinetwork_connect", code);
    return '';
};

Blockly.Python['wifinetwork_getMacAddr'] = function(block) {
    var code = "WiFi.macAddress()";
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['wifinetwork_getGatewayIp'] = function(block) {
    var code = "WiFi.gatewayIP()";
    return [code, Blockly.Python.ORDER_ATOMIC];
};



Blockly.Python['wifinetwork_AP_setup'] = function(block) {
    var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
    Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
    var wifi_client_declaration = "#include <WiFiClient.h>\n";
    Blockly.Python.addInclude("wifi_client_declaration", wifi_client_declaration);
    var wifi_esp8266_webserver_declaration = "#include <ESP8266WebServer.h>\n";
    Blockly.Python.addInclude("wifi_esp8266_webserver_declaration", wifi_esp8266_webserver_declaration);
    var ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC) || "";
    var pw = Blockly.Python.valueToCode(block, 'PW', Blockly.Python.ORDER_ATOMIC) || "";

    // var handleRoot = 'void handleRoot() {\n'+
    //                  'server.send(200, "text/html", "<h1>You are connected</h1>");\n'+
    //                  '}\n';
    // Blockly.Python.addFunction("handleRoot",handleRoot);

    var AP_setup = 'delay(1000);\n' +
        'Serial.begin(9600);\n' +
        'Serial.println();\n' +
        'Serial.print("Configuring access point...");\n' +
        /* You can remove the password parameter if you want the AP to be open. */
        'WiFi.softAP(' + ssid + ',' + pw + ');\n' +
        'IPAddress myIP = WiFi.softAPIP();\n' +
        'Serial.print("AP IP address: ");\n' +
        'Serial.println(myIP);\n';
    // 'server.on("/", handleRoot);\n'+
    // 'server.begin();\n'+
    // 'Serial.println("HTTP server started");';
    Blockly.Python.addSetup("AP_setup", AP_setup);
    // var code = "server.handleClient();\n";
    return '';
}

/*************************
 *       version 2       *
 *       eaysmode        *
 *************************/

Blockly.Python['wifiNetwork_network'] = function(block) {
    var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
    var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
    Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
    Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
    var ssid = block.getFieldValue("wifi_ssid");
    var pw = block.getFieldValue("wifi_pw");

    var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti;\n";
    Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
    let serial_setup = "Serial.begin(9600);";
    Blockly.Python.addSetup('serial_9600', serial_setup);
    var code = "  WiFi.mode(WIFI_STA);\n  delay(10);\n" +
        // Start by connecting to a WiFi network
        "  WiFiMulti.addAP(\"" + ssid + "\",\"" + pw + "\");\n" +
        "  Serial.print(\"Wait for WiFi... \");\n" +
        "  while(WiFiMulti.run() != WL_CONNECTED) {\n" +
        "    Serial.print(\".\");\n" +
        "    delay(500);\n" +
        "  }\n" +
        "  Serial.println(\"\");\n" +
        "  Serial.println(\"WiFi connected\");";
    Blockly.Python.addSetup("wifinetwork_connect", code);
    return '';
}

Blockly.Python['wifiNetwork_AP'] = function(block) {
    var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
    Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
    var wifi_client_declaration = "#include <WiFiClient.h>\n";
    Blockly.Python.addInclude("wifi_client_declaration", wifi_client_declaration);
    var wifi_esp8266_webserver_declaration = "#include <ESP8266WebServer.h>\n";
    Blockly.Python.addInclude("wifi_esp8266_webserver_declaration", wifi_esp8266_webserver_declaration);
    let serial_setup = "Serial.begin(9600);";
    Blockly.Python.addSetup('serial_9600', serial_setup);
    var ssid = block.getFieldValue('SSID') || "";
    var pw = block.getFieldValue('PW') || "";
    var AP_setup = 'delay(1000);\n' +
        'Serial.println();\n' +
        'Serial.print("Configuring access point...");\n' +
        /* You can remove the password parameter if you want the AP to be open. */
        'WiFi.softAP("' + ssid + '","' + pw + '");\n' +
        'IPAddress myAPIP = WiFi.softAPIP();\n' +
        'Serial.print("AP IP address: ");\n' +
        'Serial.println(myAPIP);\n';
    Blockly.Python.addSetup("AP_setup", AP_setup);
    return '';
}

Blockly.Python['wifiNetwork_getLocalIp'] = function(block) {
    Blockly.Python.addDeclaration("myWiFiModuleIp","String myWiFiModuleIp;")
    Blockly.Python.addSetup("getlocalIP",'IPAddress myIP = WiFi.localIP();\n'+
        '  myWiFiModuleIp = String(myIP[0]) + "." + String(myIP[1]) + "." + String(myIP[2]) + "." + String(myIP[3]);\n')
    var code = "myWiFiModuleIp";
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['wifiNetwork_isConnected'] = function(block) {
    var code = "(WiFiMulti.run() == WL_CONNECTED)";
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['wifiNetwork_getStationNum'] = function(block) {
    var code = "WiFi.softAPgetStationNum()";
    return [code, Blockly.Python.ORDER_ATOMIC];
}