import * as Blockly from "blockly";

const serialColor = "#22b845";
Blockly.Blocks['serial_print'] = {
    init: function () {
        this.appendValueInput("serial_comm_input")
            .setCheck("String")
            .appendField(Blockly.Msg.serialcomm_print);
        this.setInputsInline(false);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(serialColor);
        this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
        this.setHelpUrl("");
    }
}

Blockly.Blocks['serial_comm_print'] = {
    init: function () {
        this.appendValueInput("serial_comm_input")
            .setCheck("String")
            .appendField(Blockly.Msg.serialcomm_print);
        this.setInputsInline(false);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(serialColor);
        this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
        this.setHelpUrl("");
    }
};  

Blockly.Blocks['serial_write_data_coco'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/cocorobo.png", 65, 65, { alt: "*", flipRtl: "FALSE" }));
        // this.appendDummyInput()
        //     .appendField(Blockly.Msg.serialcomm_write_title_text_coco);
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
            .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps");
        this.appendDummyInput()
            .appendField(Blockly.Msg.serial_send_data_on_send_data_cocorobo);
        /*this.appendValueInput("first_input")
            .setCheck(null)
            .appendField("Field 0");*/
        this.itemCount_ = 1;
        this.updateShape_();
        this.setMutator(new Blockly.icons.MutatorIcon(['serial_write_data_create_with_item'],this));
        this.setColour("#22b845");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
                'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    },
    mutationToDom: function () {
        var container = document.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
    },
    domToMutation: function (xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },
    decompose: function (workspace) {
        var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },
    compose: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        // Count number of inputs.
        var connections = [];
        while (itemBlock) {
            connections.push(itemBlock.valueConnection_);
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
        // Disconnect any children that don't belong.
        for (var i = 0; i < this.itemCount_; i++) {
            var connection = this.getInput('ADD' + i).connection.targetConnection;
            if (connection && connections.indexOf(connection) == -1) {
                connection.disconnect();
            }
        }
        this.itemCount_ = connections.length;
        this.updateShape_();
        // Reconnect any child blocks.
        for (var i = 0; i < this.itemCount_; i++) {
            Blockly.icons.MutatorIcon.reconnect(connections[i], this, 'ADD' + i);
        }
    },
    saveConnections: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        var i = 0;
        while (itemBlock) {
            var input = this.getInput('ADD' + i);
            itemBlock.valueConnection_ = input && input.connection.targetConnection;
            i++;
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
    },
    updateShape_: function () {
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input = this.appendValueInput('ADD' + i);
                input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last);
                //input.appendField("資料 " + (i + 1) + ":");
                //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
                // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
};

Blockly.Blocks['serial_write_data_create_with_item'] = {
    init: function () {
        this.setColour(serialColor);
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
        this.contextMenu = false;
    }
};

Blockly.Blocks['serial_write_data_create_with_container'] = {
    init: function () {
        this.setColour(serialColor);
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_item);
        this.appendStatementInput('STACK');
        this.setTooltip('');
        this.contextMenu = false;
    }
};
Blockly.Blocks['serial_write_data'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_title_text);
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
            .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
            .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
        this.appendValueInput("ADD1")
            .setCheck(null)
        // .appendField("Field 0");
        this.itemCount_ = 1;
        this.setColour(serialColor);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
                'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    },
};

Blockly.Blocks['serial_read_data_setup'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_read_setup_title_text + Blockly.Msg.serialcomm_read_setup_title_text_1);
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
            .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
            .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
        this.setColour(serialColor);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('type');
            var TOOLTIPS = {
                'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
                'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Blocks['serial_read_data_all'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.Serial_Read_Data_all)
        // .appendField(new Blockly.FieldDropdown([
        //     ["serial_data", "serial_data"]
        // ]), "TYPE");
        this.setOutput(true, null);
        this.setColour("#22b845");
        this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
        this.setHelpUrl("");
    }
}
Blockly.Blocks['serial_read_data'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_GET_TEXT)
            .appendField(new Blockly.FieldDropdown([
                ["0", "0"],
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
                ["7", "7"],
                ["8", "8"],
                ["9", "9"],
                ["10", "10"],
                ["11", "11"],
            ]), "INDEX")
            .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_TEXT)
        this.setOutput(true, null);
        this.setColour(serialColor);
        this.setTooltip(Blockly.Msg.Serial_Read_Data_TOOLTIP);
        this.setHelpUrl("");
    }
}

Blockly.Blocks['serial_send_data_to_microbit'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
            .appendField("115200")
            .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
        this.appendValueInput("NAME")
            .appendField(Blockly.Msg.serial_send_data_on_send_data);
        this.setInputsInline(false);
        this.setColour(serialColor);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setHelpUrl("");
        this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
    }
};
Blockly.Blocks['serial_send_data_to_control_panel'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/control.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
        this.appendDummyInput()
            .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
            .appendField("9600")
            .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
        this.appendValueInput("NAME")
            .appendField(Blockly.Msg.serial_send_data_on_send_data_control_panel);
        this.setInputsInline(false);
        this.setColour("#22b845");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setHelpUrl("");
        this.setTooltip(Blockly.Msg.serial_send_data_on_send_data_control_panel);
    }
};

Blockly.Blocks['serial_read_data_clear'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.serial_clear_data);
        this.setColour("#22b845");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip(Blockly.Msg.serial_clear_data);
        this.setHelpUrl("");
    }
};
export default Blockly;