import * as Blockly from 'blockly';
import CategoryColors from './define_color'

const ESP32_IO_COLOR = "#ff8027";
Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
    init: function () {
        this.appendDummyInput().appendField(Blockly.Msg.digital_get)
        this.appendValueInput("digital_pin_input")
            .setCheck(null);
        this.appendDummyInput()
            .appendField(Blockly.Msg.digital_get_value);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(ESP32_IO_COLOR);
        this.setHelpUrl("");
        this.setTooltip(Blockly.Msg.digital_get_value_title);
    }
};
Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
    init: function () {
        this.appendDummyInput()
            .appendField("")
            .appendField(Blockly.Msg.digital_set);
        this.appendValueInput("digital_pin_input")
            .setCheck(null);
        this.appendDummyInput()
            .appendField(Blockly.Msg.digital_set_as);
        this.appendValueInput("high_low")
            .setCheck(null);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(ESP32_IO_COLOR);
        this.setHelpUrl("");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('digital_set_type');
            var TOOLTIPS = {
                '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
                '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
            };
            return TOOLTIPS[mode];
        });
    }
};

Blockly.Blocks['esp32_main_controller_io_high_low'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.io_high, "1"],
                [Blockly.Msg.io_low, "0"]
            ]), "high_low");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(ESP32_IO_COLOR);
        this.setHelpUrl("");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('high_low');
            var TOOLTIPS = {
                '1': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_high),
                '0': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_low)
            };
            return TOOLTIPS[mode];
        });
    }
};
Blockly.Blocks['esp32_main_controller_io_from_analog_pin'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.analog_get_moudule)
            .appendField(new Blockly.FieldDropdown([
                ["34", "34"],
                ["35", "35"],
                ["36", "36"]
            ]), "gpio");
        this.appendDummyInput()
            .appendField(Blockly.Msg.analog_get_value);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(ESP32_IO_COLOR);
        this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Io_From_Analog_Pin_TOOLTIP);
        this.setHelpUrl("");
    }
};


export default Blockly;