/**
 * @license Licensed under the Apache License, Version 2.0 (the "License"):
 *          http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @fileoverview Ardublockly JavaScript for the Blockly resources and bindings.
 */
'use strict';

goog.provide('Blockly.Blocks.env');
goog.require('Blockly.Blocks');

Blockly.Blocks.env.HUE = 200;
var colorENV = 200;

Blockly.Blocks['env_init'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/dev/img/blocks/cocomod_blockly_envSensor.png", 140, 40, "15"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-env.png", 50, 40, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SETUP);
        // this.appendDummyInput()
        //     .appendField(Blockly.Msg.ENV_RED_PIN)
        //     .appendField(new Blockly.FieldNumber(9),"env_red_pin")
        //     .appendField(Blockly.Msg.ENV_GREEN_PIN)
        //     .appendField(new Blockly.FieldNumber(10),"env_green_pin")
        //     .appendField(Blockly.Msg.ENV_BLUE_PIN)
        //     .appendField(new Blockly.FieldNumber(11),"env_blue_pin")
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

// Blockly.Blocks['iot_temp_unit'] = {
//   init: function() {
//     this.appendDummyInput()
//         .appendField("Set temperature unit as ")
//         .appendField(new Blockly.FieldDropdown([["Fahrenheit","f"], ["Celsius","c"]]), "temp_unit");
//     this.setPreviousStatement(true, null);
//     this.setNextStatement(true, null);
//     this.setColour(230);
//  this.setTooltip("");
//  this.setHelpUrl("");
//   }
// };

Blockly.Blocks['iot_read_temp'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_READTEMP)
            //.appendField(new Blockly.FieldTextInput("4"),"env_sensor_temp");
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), "env_sensor_temp");
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_TEMPUNIT)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ENV_SENSOR_UNITC, "c"],
                [Blockly.Msg.ENV_SENSOR_UNITF, "f"]
            ]), "temp_unit");
        this.setOutput(true, "Number");
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.identifyNumber('1.1');
    }
};

// Blockly.Blocks['iot_buzzer_alarm'] = {
//   init: function() {
//     this.appendDummyInput()
//         .appendField("Buzzer Alarm -")
//         .appendField(new Blockly.FieldNumber(1000, 0, Infinity, 1), "alarm_time")
//         .appendField("millisecond");
//     this.setPreviousStatement(true, null);
//     this.setNextStatement(true, null);
//     this.setColour(135);
//  this.setTooltip("");
//  this.setHelpUrl("");
//   }
// };

Blockly.Blocks['iot_read_val'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_READ)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ENV_SENSOR_SOUND, "mic"],
                [Blockly.Msg.ENV_SENSOR_LIGHT, "light"],
                [Blockly.Msg.ENV_SENSOR_HUMIDITY, "humid"]
            ]), "val_type");
        this.setOutput(true, "Number");
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        var numString;
        if (this.getFieldValue('val_type') === "humid") {
            numString = '1.1';
        } else {
            numString = '1';
        }
        return Blockly.Types.identifyNumber(numString);
    }
};

Blockly.Blocks['iot_read_mic'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_MIC)
            //.appendField(new Blockly.FieldTextInput("A2"),"env_sensor_mic");  
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.analogPins), 'env_sensor_mic');
        this.setOutput(true, "Number");
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    updateFields: function() {
        Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_mic', 'pwmPins');
    },
    getBlockType: function() {
        var numString = '1';
        return Blockly.Types.identifyNumber(numString);
    }
};

Blockly.Blocks['iot_read_light'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_LIGHT)
            // .appendField(new Blockly.FieldTextInput("A1"),"env_sensor_light"); 
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.analogPins), "env_sensor_light");
        this.setOutput(true, "Number");
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    updateFields: function() {
        Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_light', 'pwmPins');
    },
    getBlockType: function() {
        var numString = '1';
        return Blockly.Types.identifyNumber(numString);
    }
};
Blockly.Blocks['iot_read_humid'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_HUMID)
            //.appendField(new Blockly.FieldTextInput("4"),"env_sensor_humid");
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), "env_sensor_humid");
        this.setOutput(true, "Number");
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    updateFields: function() {
        Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_humid', 'pwmPins');
    },
    getBlockType: function() {
        var numString = '1.1';
        return Blockly.Types.identifyNumber(numString);
    }
};

// Blockly.Blocks['iot_threshold_controller'] = {
//   init: function() {
//     this.appendDummyInput()
//         .appendField("When the critical value of ")
//         .appendField(new Blockly.FieldDropdown([["UV","uv"], ["Light","light"], ["Temperature","temp"], ["Humidity","humid"]]), "threshold_types")
//         .appendField("reaches")
//         .appendField(new Blockly.FieldTextInput("60"), "threshold_val");
//     this.appendStatementInput("Do")
//         .setCheck(null)
//         .appendField("Do");
//     this.setPreviousStatement(true, null);
//     this.setNextStatement(true, null);
//     this.setColour(135);
//  this.setTooltip("");
//  this.setHelpUrl("");
//   }
// };


Blockly.Blocks['iot_buzzer'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_SETTONE, [{
                "type": "field_dropdown",
                "name": "PINSNUM",
                "options": [
                    ["7", "7"]
                ]
            },
            {
                "type": "input_value",
                "name": "FREQUENCY",
                "check": Blockly.Types.NUMBER.checkList
            }
        ]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip(Blockly.Msg.BASIC_TONE_TIP);
        this.setHelpUrl('https://www.arduino.cc/en/Reference/tone');
    },
    onchange: function() {
        var freq = Blockly.Python.valueToCode(this, "FREQUENCY", Blockly.Python.ORDER_ATOMIC)
        if (freq < 31 || freq > 65535) {
            this.setWarningText(Blockly.Msg.BASIC_TONE_WARNING, 'io_tone');
        } else {
            this.setWarningText(null, 'io_tone');
        }
    }
};

Blockly.Blocks['iot_nobuzzer'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_NOTONE, [{
            "type": "field_dropdown",
            "name": "PINSNUM",
            "options": [
                ["7", "7"]
            ]
        }]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip(Blockly.Msg.BASIC_NOTONE_TIP);
        this.setHelpUrl('https://www.arduino.cc/en/Reference/noTone');
    }
};

// Blockly.Blocks['iot_setup_rgb'] = {
//   init: function() {
//     this.appendValueInput('RED')
//         .setCheck(Blockly.Types.NUMBER.checkList)
//         .setAlign(Blockly.ALIGN_RIGHT)
//         .appendField(Blockly.Msg.COLOUR_RGB_TITLE)
//         .appendField(Blockly.Msg.COLOUR_RGB_RED);
//     this.appendValueInput('GREEN')
//         .setCheck(Blockly.Types.NUMBER.checkList)
//         .setAlign(Blockly.ALIGN_RIGHT)
//         .appendField(Blockly.Msg.COLOUR_RGB_GREEN);
//     this.appendValueInput('BLUE')
//         .setCheck(Blockly.Types.NUMBER.checkList)
//         .setAlign(Blockly.ALIGN_RIGHT)
//         .appendField(Blockly.Msg.COLOUR_RGB_BLUE);
//     this.setOutput(true, 'Colour');
//     this.setTooltip(Blockly.Msg.COLOUR_RGB_TOOLTIP);
//     this.setHelpUrl(Blockly.Msg.COLOUR_RGB_HELPURL);
//     this.setColour(135);
//   }
// };

Blockly.Blocks['iot_set_rgb'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENV_SENSOR_SET_RGB);
        this.appendValueInput("RED_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.ENV_SETRGB_R)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_r');
        this.appendValueInput("GREEN_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.ENV_SETRGB_G)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_g');
        this.appendValueInput("BLUE_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.ENV_SETRGB_B)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_b');
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.env.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};