/**
 * @license
 * Visual Blocks Editor
 *
 * Copyright 2012 Google Inc.
 * https://developers.google.com/blockly/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Colour blocks for Blockly.
 * @author fraser@google.com (Neil Fraser)
 */
'use strict';

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

goog.require('Blockly.Blocks');


/**
 * Common HSV hue for all blocks in this category.
 */
Blockly.Blocks.voicekit.HUE = "#3382f5";

Blockly.Blocks["voicekit_from"] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendValueInput("INPUT")
            .appendField(Blockly.Msg.VOICEKIT_FROM)
            .appendField(new Blockly.FieldDropdown([
                ["aiy.voice","aiy.vocie"],
                ["aiy.assistant","aiy.assistant"],
                ["aiy.assistant.library","aiy.assistant.library"],
                ["google.assistant.library.event","google.assistant.library.event"]
            ]),"FROM")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks["voicekit_import"] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_IMPORT_AUTH)
        this.setOutput(true);
    }
}

Blockly.Blocks["voicekit_import_tts"] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_IMPORT_TTS)
        // this.setOutput(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks["voicekit_tts_say"] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendValueInput("TEXT")
            .appendField(Blockly.Msg.VOICEKIT_IMPORT_TTS_SAY)
        this.setInputsInline(true)
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks["voicekit_set_credential"] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendValueInput("INPUT")
            .appendField(Blockly.Msg.VOICEKIT_SET_CREDENTIAL)
            .appendField(new Blockly.FieldTextInput("assistant"),"ASSISTANT")
            .appendField(Blockly.Msg.VOICEKIT_SET_FROM)
        this.appendStatementInput("STACK")
            .appendField(Blockly.Msg.VOICEKIT_SET_DO)
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_get_credential'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_GET_CREDENTIAL)
        this.setOutput(true);
    }
}

Blockly.Blocks['voicekit_import_assistant'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_IMPORT_ASSISTANT)
        this.setOutput(true);
    }
}

Blockly.Blocks['voicekit_assistant_preNext'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendValueInput("INPUT")
            .appendField(Blockly.Msg.VOICEKIT_ASSISTANT)
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["stop conversation","stop_conversation()"],
                ["start conversation","start_conversation()"]
            ]),"OPERATION")
        this.setInputsInline(true);
        this.setPreviousStatement(true)
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_assistant_output'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendValueInput("INPUT")
            .appendField(Blockly.Msg.VOICEKIT_ASSISTANT)
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["stop conversation","stop_conversation()"],
                ["start conversation","start_conversation()"]
            ]),"OPERATION")
        this.setInputsInline(true);
        // this.setPreviousStatement(true)
        // this.setNextStatement(true);
        this.setOutput(true);
    }
}

Blockly.Blocks['voicekit_EventType_import'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_EVENTTYPE_IMPORT)
        // this.setInputsInline(true);
        // this.setPreviousStatement(true)
        // this.setNextStatement(true);
        this.setOutput(true);
    }
}



Blockly.Blocks['voicekit_event_of'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendValueInput("INPUT")
            .appendField(Blockly.Msg.VOICEKIT_EVENT_TYPE_OF)
        this.setInputsInline(true);
        this.setOutput(true);
    }
}


Blockly.Blocks['voicekit_run_task'] = {
    init:function(){
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("_run_task")
        this.setOutput(true)
    }
}

Blockly.Blocks['voicekit_process_event'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("_process_event")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_assistant_start_conversation'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("self_assistant.start_conversation()")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_send_instructions'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("send_instructions")
        this.appendValueInput("TEXT")
            .appendField("text")
            .setAlign(Blockly.ALIGN_RIGHT)
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_on_button_pressed'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("on_button_pressed")
        this.setOutput(true)
    }
}




/**************************
*        simplify         *
***************************/
/**
* voice kit 
* import EventType / auth_helpers / Assistant
 */
Blockly.Blocks['voicekit_import_aiy'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("import AIY Voice Moudles")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
  },
}

/**
* voice kit init
* 
 */
Blockly.Blocks['voicekit_init'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Voicekit Kit")
        this.appendDummyInput()
            .appendField("Assistant Initialization")
        this.itemCount_ = 1;
        this.updateShape_();
        this.setMutator(new Blockly.Mutator(['voicekit_init_item']));
        this.setPreviousStatement(true);
        this.setNextStatement(true);
  },
    /**
     * Create XML to represent print inputs.
     * @return {Element} XML storage element.
     * @this Blockly.Block
     */
    mutationToDom: function(workspace) {
        var container = document.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
    },
    /**
     * Parse XML to restore the list inputs.
     * @param {!Element} xmlElement XML storage element.
     * @this Blockly.Block
     */
    domToMutation: function(xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },
    /**
     * Populate the mutator's dialog with this block's components.
     * @param {!Blockly.Workspace} workspace Mutator's workspace.
     * @return {!Blockly.Block} Root block in mutator.
     * @this Blockly.Block
     */
    decompose: function(workspace) {
        var containerBlock = Blockly.Block.obtain(workspace,
                                 'voicekit_init_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var x = 0; x < this.itemCount_; x++) {
          var itemBlock = Blockly.Block.obtain(workspace, 'voicekit_init_item');
          itemBlock.initSvg();
          connection.connect(itemBlock.previousConnection);
          connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },
    /**
     * Reconfigure this block based on the mutator dialog's components.
     * @param {!Blockly.Block} containerBlock Root block in mutator.
     * @this Blockly.Block
     */
    compose: function(containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        // Count number of inputs.
        var connections = [];
        var i = 0;
        while (itemBlock) {
            connections[i] = itemBlock.valueConnection_;
            itemBlock = itemBlock.nextConnection &&
                        itemBlock.nextConnection.targetBlock();
            i++;
        }
        this.itemCount_ = i;
        this.updateShape_();
        // Reconnect any child blocks.
        for (var i = 0; i < this.itemCount_; i++) {
            if (connections[i]) {
                this.getInput('ADD' + i).connection.connect(connections[i]);
            }
        }
    },
    /**
     * Store pointers to any connected child blocks.
     * @param {!Blockly.Block} containerBlock Root block in mutator.
     * @this Blockly.Block
     */
    saveConnections: function(containerBlock) {
        // Store a pointer to any connected child blocks.
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        var x = 0;
        while (itemBlock) {
            var input = this.getInput('ADD' + x);
            itemBlock.valueConnection_ = input && input.connection.targetConnection;
            x++;
            itemBlock = itemBlock.nextConnection &&
                        itemBlock.nextConnection.targetBlock();
        }
    },
    /**
     * Modify this block to have the correct number of inputs.
     * @private
     * @this Blockly.Block
     */
    updateShape_: function() {
        // Delete everything.
        if (this.getInput('EMPTY')) {
            this.removeInput('EMPTY');
        } else {
            var i = 0;
            while (this.getInput('ADD' + i)) {
                this.removeInput('ADD' + i);
                i++;
            }
        }
    
        // Rebuild block.
        for (var i = 0; i < this.itemCount_; i++) {
            var input = this.appendValueInput('ADD' + i);
        }
        
    }
};
Blockly.Blocks['voicekit_init_container'] = {
  // Container.
  init: function() {
    this.setColour(Blockly.Blocks.voicekit.HUE);
    this.appendDummyInput()
        .appendField('field');
    this.appendStatementInput('STACK');
    this.contextMenu = false;
  }
};
Blockly.Blocks['voicekit_init_item'] = {
  // Add items.
  init: function() {
    this.setColour(Blockly.Blocks.voicekit.HUE);
    this.appendDummyInput()
        .appendField('item');
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.contextMenu = false;
  }
};


Blockly.Blocks['voicekit_task'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Task Tread")
        this.setOutput(true)
    }
}

Blockly.Blocks['voicekit_can_start'] = {
        init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Converstation")
        this.setOutput(true)
    }
}

Blockly.Blocks['voicekit_asssistant'] = {
        init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Assistant")
        this.setOutput(true)
    }
}

/**
* voice kit 
* start thread
 */
Blockly.Blocks['voicekit_start_thread'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Start task")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

/**
* voice kit 
* run task
 */
Blockly.Blocks['voicekit_runTask'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Run task")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}






Blockly.Blocks['voicekit_logging_config'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("logging.basicConfig(level = logging.INFO)")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_myassistant_start'] = {
    init:function() {
        this.setColour(230)
        this.appendDummyInput()
            .appendField(new Blockly.FieldTextInput("MyAssistant"),"INPUT")
            .appendField(".start()")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_process_event'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Process Event")
        this.appendStatementInput("STACK");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
    }
}

Blockly.Blocks['voicekit_process_eventType'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField("Event.type")
        this.setOutput(true);
    }
}

Blockly.Blocks['voicekit_EventType_select'] = {
    init: function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_EVENTTYPE_ASSISTANT)
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["ON_START_FINISHED","ON_START_FINISHED"],
                ["ON_CONVERSATION_TURN_STARTED","ON_CONVERSATION_TURN_STARTED"],
                ["ON_RECOGNIZING_SPEECH_FINISHED","ON_RECOGNIZING_SPEECH_FINISHED"],
                ["ON_END_OF_UTTERANCE","ON_END_OF_UTTERANCE"],
                ["ON_CONVERSATION_TURN_FINISHED","ON_CONVERSATION_TURN_FINISHED"],
                ["ON_ASSISTANT_ERROR","ON_ASSISTANT_ERROR"], 
                ["ON_CONVERSATION_TURN_TIMEOUT","ON_CONVERSATION_TURN_TIMEOUT"],
                ["ON_NO_RESPONSE","ON_NO_RESPONSE"],      
            ]),"TYPE")
        this.setOutput(true)
    }
}



Blockly.Blocks['voicekit_can_start_conversation_self'] = {
    init: function() {
        this.setColour("#ff6381")
        this.appendDummyInput()
            .appendField("Can start conversation")
        this.setOutput(true);
    }
}

Blockly.Blocks['voicekit_can_start_conversation'] = {
    init: function() {
        this.setColour("#ff6381")
        this.appendDummyInput()
            .appendField("Can start conversation =")
            .appendField(new Blockly.FieldDropdown([
                ["True","True"],
                ["False","False"],         
            ]),"SETTING")
        this.setPreviousStatement(true);
        this.setNextStatement(true)
    }
}

Blockly.Blocks['voicekit_assistant_conversation'] = {
    init: function() {
        this.setColour("#ff6381")
        this.appendDummyInput()
            .appendField("Assistant.")
            .appendField(new Blockly.FieldDropdown([
                ["stop_conversation","stop_conversation()"],
                ["start_conversation","start_conversation()"]      
            ]),"SETTING")
        this.setPreviousStatement(true);
        this.setNextStatement(true)
    }
}

Blockly.Blocks['voicekit_event_args'] = {
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_EVENT_ARGS)
        this.setInputsInline(true);
        this.setOutput(true)
    }
}

Blockly.Blocks['voicekit_event_args_of'] ={
    init:function() {
        this.setColour(Blockly.Blocks.voicekit.HUE)
        this.appendDummyInput()
            .appendField(Blockly.Msg.VOICEKIT_EVENT_ARGS)
            .appendField(Blockly.Msg.VOICEKIT_EVENT_OF)
            .appendField(new Blockly.FieldTextInput("text"),"TEXT")
        this.setInputsInline(true);
        this.setOutput(true)
    }
}

