import * as Blockly from 'blockly';
import { pythonGenerator } from 'blockly/python';

Blockly.Python = pythonGenerator || { Msg: Object.create(null) };

const color = '#5fcd8e';

Blockly.Blocks['ai_mphone_record_init'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('Microphone Init');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('Initialize the microphone');
        this.setHelpUrl('');
    }
};

Blockly.Python['ai_mphone_record_init'] = function (block) {
    var code = 'microphone.init()\n';
    return code;
}

Blockly.Blocks['ai_mphone_record_setting'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('Microphone Start');
        this.appendValueInput("PATH")
            .setCheck(null)
            .appendField('音频文件路径');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('Start recording');
        this.setHelpUrl('');
    }
};

Blockly.Python['ai_mphone_record_setting'] = function (block) {
    let _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
    var code = _path + '\n';
    return code;
};

Blockly.Blocks['ai_mphone_Record_audio_start'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('Microphone Stop');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('Stop recording');
        this.setHelpUrl('');
    }
};

Blockly.Python['ai_mphone_Record_audio_start'] = function (block) {
    var code = 'microphone.stop()\n';
    return code;
};

Blockly.Blocks['ai_mphone_Record_audio_sound_loudness_analysis'] = { 
    init: function() {
        this.appendDummyInput()
            .appendField('停止录音');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('停止录音');
        this.setHelpUrl('');
    }
};
Blockly.Python['ai_mphone_Record_audio_sound_loudness_analysis'] = function (block) {
    var code = 'microphone.start()\n';
    return code;
};

Blockly.Blocks['mphone_audio_spectrum_init'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('录音');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('录音');
        this.setHelpUrl('');
    }
};

Blockly.Python['mphone_audio_spectrum_init'] = function (block) {
    var code = '录音\n';
    return code;
};

Blockly.Blocks['mphone_audio_spectrum_setting'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('录音设置');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('设置录音参数');    
        this.setHelpUrl('');
    }
    
};

Blockly.Python['mphone_audio_spectrum_setting'] = function (block) {
    var code = '设置录音参数\n';    
    return code;
    
};

Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('开始录音');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip('开始录音');
        this.setHelpUrl('');
    }
    
};

Blockly.Python['ai_mphone_read_sound_sensitivity'] = function (block) {
    var code = '开始录音\n';
    return code;
}

const speakerColor = '#603ea0';
// 扬声器
Blockly.Blocks['ai_speaker_play_wav'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('播放音频文件');
        this.appendValueInput("PATH")
            .setCheck(null)
            .appendField('音频文件路径');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setInputsInline(true);
        this.setColour(speakerColor);
        this.setTooltip('初始化扬声器');
        this.setHelpUrl('');
    }
}

Blockly.Python['ai_speaker_play_wav'] = function (block) {
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
    var code = _path + '\n';
    return code;
}

Blockly.Blocks['set_volume'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('设置音量')
            .appendField(new Blockly.FieldNumber(50, 0, 100), 'VOLUME');
        this.setNextStatement(true, null);
        this.setColour(speakerColor);
        this.setPreviousStatement(true, null);
    }
}

Blockly.Python['set_volume'] = function (block) { 
    var volume = block.getFieldValue('VOLUME');
    var code = 'speaker.set_volume(' + volume + ')\n';
    return code;
}

export default Blockly;