'use strict';

goog.provide('Blockly.Blocks.wifinetwork');

goog.require('Blockly.Blocks');
goog.require('Blockly.Types');

Blockly.Blocks.wifinetwork.HUE = "#572b7a";

/*************************
 *       version 1       *
 *************************/
Blockly.Blocks['wifiNetwork_connect_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_wifi.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/wifi-network.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_CONNECT)
        this.appendValueInput("SSID")
            .setCheck("Text")
            .appendField(Blockly.Msg.WIFINETWORK_CONNECT_SSID)
        this.appendValueInput("PW")
            .setCheck("Text")
            .appendField(Blockly.Msg.WIFINETWORK_CONNECT_PW);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        // this.setPreviousStatement(true, null);
        // this.setNextStatement(true, null);
        // this.setInputsInline(true);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}

/*get device mac*/
Blockly.Blocks['wifiNetwork_getMacAddr'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_GETMACADDR);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};

/*get gateway ip*/
Blockly.Blocks['wifiNetwork_getGatewayIp'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_GETGATEWAYIP);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};



/*
 *ESP8266 wifi Access Point
 *provide a LAN with SSID and PW set in advance
 */
Blockly.Blocks['wifiNetwork_AP_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_wifi.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/wifi-network.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_AP_SETUP)
        this.appendValueInput("SSID")
            .setCheck("Text")
            .appendField(Blockly.Msg.WIFINETWORK_AP_SSID)
        this.appendValueInput("PW")
            .setCheck("Text")
            .appendField(Blockly.Msg.WIFINETWORK_AP_PW);
        this.setColour(50);
        // this.setPreviousStatement(true, null);
        // this.setNextStatement(true, null);
        // this.setInputsInline(true);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};


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

/*wifi easymode
 *setup for WiFi network
 */
Blockly.Blocks['wifiNetwork_network'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_wifi.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-wifi.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(" " + Blockly.Msg.WIFIEASYMODE_NETWORK_TITLE)
        this.appendDummyInput()
            .appendField(" " + Blockly.Msg.WIFIEASYMODE_NETWORK_SSID)
            .appendField(new Blockly.FieldTextInput("ENTER_YOUR_SSID"), "wifi_ssid")
        this.appendDummyInput()
            .appendField(" " + Blockly.Msg.WIFIEASYMODE_NETWORK_PW)
            .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PASSWORD"), "wifi_pw")
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};

Blockly.Blocks['wifiNetwork_AP'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_wifi.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/wifi-network.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_AP_SETUP)
        this.appendDummyInput()
            .appendField(" " + Blockly.Msg.WIFINETWORK_AP_SSID)
            .appendField(new Blockly.FieldTextInput("ENTER_YOUR_SSID"), "SSID")
        this.appendDummyInput()
            .appendField(" " + Blockly.Msg.WIFINETWORK_AP_PW)
            .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PASSWORD"), "PW")
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    /**
     * Called whenever anything on the workspace changes.
     * It checks the number of password and warn if the number is less than eight
     * @this Blockly.Block
     */
    onchange: function() {
        let len = this.getFieldValue("PW").length;
        if (len > 0) {
            if (len < 8) {
                this.setWarningText(Blockly.Msg.WIFINETWORK_PW_WARN, 'network_pw');
            } else {
                this.setWarningText(null, 'network_pw');
            }
        } else if (len == 0) {
            if (len == 0) this.setFieldValue("ENTER_YOUR_PASSWORD", "PW");
        }
    }
};

/*get device ip*/
Blockly.Blocks['wifiNetwork_getLocalIp'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_GETLOCALIP);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};

/*check if wifi is connected */
Blockly.Blocks['wifiNetwork_isConnected'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_ISCONNECTED);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setOutput(true, Blockly.Types.BOOLEAN.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};

/**wifi 
 * number of connected client
 */
 Blockly.Blocks['wifiNetwork_getStationNum'] = {
     init:function() {
         this.appendDummyInput()
            .appendField(Blockly.Msg.WIFINETWORK_GETSTATIONNUM);
        this.setColour(Blockly.Blocks.wifinetwork.HUE);
        this.setOutput(true);
        this.setTooltip("");
        this.setHelpUrl("");
     }
 }