import * as Blockly from 'blockly';

const timeColor = '#fabe23';
Blockly.Blocks['time_delay'] = {
    init: function () {
        this.appendValueInput('DELAY_TIME_MILI')
            .setCheck('Number')
            .appendField(Blockly.Msg.ARD_TIME_DELAY);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_TIME_MS);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_TIP);
        this.setColour(timeColor);
    }
}

Blockly.Blocks['time_delayseconds'] = {
    /**
     * delayMicroseconds block definition
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl('');
        this.setColour(timeColor);
        this.appendValueInput('DELAY_TIME_MICRO')
            //.setCheck(Blockly.Types.NUMBER.checkList)
            .appendField(Blockly.Msg.ARD_TIME_DELAY);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_TIME_DELAY_seconds);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_seconds_TIP);
    }
};
Blockly.Blocks['esp32_get_current_date'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.time_get_current_date_title)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.time_get_current_date_y, "Y"],
                [Blockly.Msg.time_get_current_date_m, "m"],
                [Blockly.Msg.time_get_current_date_d, "d"],
                [Blockly.Msg.time_get_current_date_hour, "H"],
                [Blockly.Msg.time_get_current_date_min, "M"],
                [Blockly.Msg.time_get_current_date_sec, "S"]
            ]), "type");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour(timeColor);
        this.setHelpUrl("");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                '0': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_y),
                '1': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_m),
                '2': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_d),
                '3': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_hour),
                '4': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_min),
                '5': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_sec)
            };
            return TOOLTIPS[mode];
        });
    }
};

Blockly.Blocks['esp32_main_controller_time_timer_init'] = {
    init: function () {
        this.appendDummyInput().appendField(Blockly.Msg.time_init_timer);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(timeColor);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
                'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Blocks['esp32_main_controller_time_timer_get_current'] = {
    init: function () {
        this.appendDummyInput().appendField(Blockly.Msg.time_timer_get_time);
        this.setOutput(true, null);
        this.setColour(timeColor);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
                'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};
Blockly.Blocks['esp32_main_controller_time_timer_clear'] = {
    init: function () {
        this.appendDummyInput().appendField(Blockly.Msg.time_timer_clear);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(timeColor);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
                'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};
Blockly.Blocks['esp32_main_controller_time_period_timer'] = {
    init: function () {

        this.appendDummyInput()
            .appendField(Blockly.Msg.time_timer_periodically);
        this.appendDummyInput()
            .appendField(Blockly.Msg.time_timer_every)
            .appendField(new Blockly.FieldTextInput("500"), "period_timer_count")
            .appendField(Blockly.Msg.time_timer_ms);
        this.appendStatementInput("exec_period_timer")
            .setCheck(null)
            .appendField(Blockly.Msg.time_execution);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(timeColor);
        this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', "CocoPi"));
        this.setHelpUrl("");
    }
};
Blockly.Blocks['esp32_main_controller_time_period_timer_clear'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.time_timer_periodically_clear);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(timeColor);
        this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', "CocoPi"));
        this.setHelpUrl("");
    }
};

export default Blockly;