'use strict';

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

Blockly.Blocks['sonar_setup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage(
                // "http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_ultrasonicSensor.png", 140, 40, "15"));
                // "http://cocorobo.cn/cocoblockly/blockly/media/sensors-ultrasonic.png", 50, 40, "15"));
                "./../blockly/media/sensors-ultrasonic.png", 50, 40, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.SONAR)
            .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"]
            ]), "NUMBER")
            .appendField(Blockly.Msg.SONAR_SETUP);
        this.appendDummyInput()
            .appendField(Blockly.Msg.SONAR_SETUP_TRIGPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins.concat(Blockly.Python.Boards.selected.analogPins)), "TRIG_PIN")
            .appendField(Blockly.Msg.SONAR_SETUP_ECHOPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins.concat(Blockly.Python.Boards.selected.analogPins)), "ECHO_PIN");
        this.appendDummyInput()
            .appendField(Blockly.Msg.SONAR_SETUP_MAXDIST)
            .appendField(new Blockly.FieldNumber(50), "MAXDIST");
        this.setColour(300);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['sonar_read_dist'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.SONAR_READ_DIST)
            .appendField(Blockly.Msg.SONAR)
            .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"]
            ]), "NUMBER");
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(300);
        this.setTooltip('');
        this.setHelpUrl('');
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    },
};

Blockly.Blocks['infraredRay_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_infraredRay.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/sensors-ir_receiver.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.INFRAREDRAY_SETUP);
        // this.setNextStatement(true, null);
        this.setColour(300);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['infraredRay_receive_val'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.INFRAREDRAY_RECVAL);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setColour(300);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
};

Blockly.Blocks['infraredRay_send_val'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["↑", "KEYPAD_UP"],
                ["↓", "KEYPAD_DOWN"],
                ["←", "KEYPAD_LEFT"],
                ["→", "KEYPAD_RIGHT"],
                ["OK", "KEYPAD_OK"],
                ["1", "KEYPAD_1"],
                ["2", "KEYPAD_2"],
                ["3", "KEYPAD_3"],
                ["4", "KEYPAD_4"],
                ["5", "KEYPAD_5"],
                ["6", "KEYPAD_6"],
                ["7", "KEYPAD_7"],
                ["8", "KEYPAD_8"],
                ["9", "KEYPAD_9"],
                ["0", "KEYPAD_0"],
                ["*", "KEYPAD_STAR"],
                ["#", "KEYPAD_NUMSIGN"]
            ]), "VALTYPE");
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setColour(300);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
};

/* RTC 
 *  Setup 
 */
Blockly.Blocks['RTC_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_RTC.png", 40, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/sensors-rtc.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_SETUP);
        this.setColour(340);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};

/* RTC 
 *  Set Date/Time 
 */
Blockly.Blocks['RTC_setDateTime'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_SETDATETIME);
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_SETDATE)
            .appendField(new Blockly.FieldNumber(2018, 0, 2050), "year")
            .appendField("-")
            .appendField(new Blockly.FieldNumber(1, 0, 12), "month")
            .appendField("-")
            .appendField(new Blockly.FieldNumber(1, 0, 31), "day")
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_SETTIME)
            .appendField(new Blockly.FieldNumber(12, 0, 24), "hour")
            .appendField(":")
            .appendField(new Blockly.FieldNumber(30, 0, 60), "minute")
            .appendField(":")
            .appendField(new Blockly.FieldNumber(10, 0, 60), "second")
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(340);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    /**
     * Called whenever anything on the workspace changes.
     * It checks the number of data and time settting 
     * @this Blockly.Block
     */
    onchange: function() {
        if (this.getFieldValue("year") > 2050) {
            this.setFieldValue("2019", "year");
        }
        if (this.getFieldValue("month") > 12) {
            this.setFieldValue("12", "month");
        } else if (this.getFieldValue("month") < 0) {
            this.setFieldValue("1", "month");
        }
        if (this.getFieldValue("day") > 31) {
            this.setFieldValue("31", "day");
        } else if (this.getFieldValue("day") < 0) {
            this.setFieldValue("1", "day");
        }
        if (this.getFieldValue("hour") > 24) {
            this.setFieldValue("24", "hour");
        } else if (this.getFieldValue("hour") < 0) {
            this.setFieldValue("1", "hour");
        }
        if (this.getFieldValue("minute") > 60) {
            this.setFieldValue("60", "minute");
        } else if (this.getFieldValue("minute") < 0) {
            this.setFieldValue("1", "minute");
        }
        if (this.getFieldValue("second") > 60) {
            this.setFieldValue("60", "second");
        } else if (this.getFieldValue("second") < 0) {
            this.setFieldValue("1", "second");
        }
    }
};
/* RTC 
 *  get Date
 */
Blockly.Blocks['RTC_getDate'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_GETDATE)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.RTC_YEAR, "year"],
                [Blockly.Msg.RTC_MONTH, "month"],
                [Blockly.Msg.RTC_DAY, "day"],
            ]), "date_detail")
        this.setColour(340);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
};

/* RTC 
 *  get Time
 */
Blockly.Blocks['RTC_getTime'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_GETTIME)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.RTC_HOUR, "hour"],
                [Blockly.Msg.RTC_MINUTE, "minute"],
                [Blockly.Msg.RTC_SECOND, "second"],
            ]), "time_detail")
        this.setColour(340);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
};

/* RTC 
 *  get Week
 */
Blockly.Blocks['RTC_getWeek'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.RTC_GETWEEK);
        this.setColour(340);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
};

/* PulseSensor */
Blockly.Blocks['PulseSensor_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_pulseSensor.png", 40, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/sensors-heart_beat.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_SETUP)
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_SETUP_1)
            .appendField(new Blockly.FieldDropdown(Blockly.Python.Boards.selected.analogPins), "PIN");
        this.setColour(360);
        this.setTooltip("");
        this.setHelpUrl("");
    },
};
/* PulseSensor */
Blockly.Blocks['PulseSensor_getBPM'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_GETBPM);
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(360);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
}
/* PulseSensor */
Blockly.Blocks['PulseSensor_Heart_Beats'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_HEARTBEAT_WHEN);
        this.setOutput(true, Blockly.Types.BOOLEAN.output);
        this.setColour(360);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.BOOLEAN;
    }
};
/* PulseSensor */
Blockly.Blocks['PulseSensor_Heart_noBeats'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_HEARTBEAT_WHENNO);
        this.setOutput(true, Blockly.Types.BOOLEAN.output);
        this.setColour(360);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.BOOLEANs;
    }
};
/* PulseSensor */
Blockly.Blocks['PulseSensor_print'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.PULSESENSOR_HEARTBEAT_PRINT);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(360);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

/*Encoder*/
Blockly.Blocks['Encoder_Setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_encoder.png", 40, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/sensors-encoder.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENCODER_SETUP)
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENCODER_SETUP_1)
            .appendField(new Blockly.FieldDropdown([
                ["2", "2"],
                ["3", "3"]
            ]), "PIN");
        this.setColour(260);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}

/*Encoder*/
Blockly.Blocks['Encoder_getRotationSpeed'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ENCODER_GETROTATIONSPEED)
        this.setOutput(true);
        this.setColour(260);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        var numString = '1';
        return Blockly.Types.identifyNumber(numString);
    }
}

/* RFID 
 *  Setup
 */
Blockly.Blocks['RFID_Setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_encoder.png", 40, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/sensors-rfid.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.RFID_SETUP)
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}

/* RFID 
 * Read ID
 */
Blockly.Blocks['RFID_ReadID'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.RFID_READID);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
}

/* GPS 
 * Setup
 */
Blockly.Blocks['GPS_setup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/gps_header.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.GPS_SETUP);
        this.setColour(210);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}

/* GPS 
 * get latitude
 */
Blockly.Blocks['GPS_getLatitude'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.GPS_LATITUDE);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setColour(210);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
}

/* GPS 
 * get longitude
 */
Blockly.Blocks['GPS_getLongitude'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.GPS_LONGITUDE);
        this.setOutput(true, Blockly.Types.TEXT.output);
        this.setColour(210);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }
}

/* Water Temperature Sensor, DS18S20
 * setup
 */

Blockly.Blocks['water_temp_sensor_setup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/water-temp-sensor-setup.png", 70, 60, "*"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.WATERTEMP_SETUP_BLOCKNAME);
        this.appendDummyInput()
            .appendField(Blockly.Msg.WATERTEMP_SETUP_SENSORTYPE)
            .appendField(new Blockly.FieldDropdown([
                ["DS18S20", "water_temp_sensor_setup_"]
            ]), "water_temp_sensor_setup_dropdown")
            .appendField(Blockly.Msg.WATERTEMP_SETUP_SETUP);
        this.appendDummyInput()
            .appendField(Blockly.Msg.WATERTEMP_SETUP_ATTACHED_PIN)
            .appendField(new Blockly.FieldNumber(3, 0, 13, 1), "water_temp_sensor_setup_pin");
        this.setColour(0);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

/* Water Temperature Sensor, get value
 */

Blockly.Blocks['water_temp_sensor_get_value'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.WATERTEMP_GET_TEMP);
        this.setOutput(true, Blockly.Types.DECIMAL.output);
        this.setColour(0);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.DECIMAL;
    }
};

// CO2 sensor setup

Blockly.Blocks['thirdparty_co2_sensor_setup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/third-party_co2_header.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
        this.appendDummyInput()
            .appendField(Blockly.Msg.CO2SENSOR_SETUP);
        this.appendDummyInput()
            .appendField(Blockly.Msg.CO2SENSOR_SETUP_SDA)
            .appendField(new Blockly.FieldDropdown([
                ["D2", "D2"]
            ]), "thirdparty_co2_sensor_setup_sdapin")
            .appendField(Blockly.Msg.CO2SENSOR_SETUP_SCL)
            .appendField(new Blockly.FieldDropdown([
                ["D3", "D3"]
            ]), "thirdparty_co2_sensor_setup_sclpin");
        this.setColour(330);
        this.setTooltip("");
        this.setHelpUrl("https://help.cocorobo.cn/");
    }
};

Blockly.Blocks['thirdparty_co2_sensor_get_co2'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.CO2SENSOR_GET_CO2);
    this.setOutput(true, null);
    this.setColour(330);
 this.setTooltip("");
 this.setHelpUrl("https://help.cocorobo.cn/");
  }
};

Blockly.Blocks['thirdparty_co2_sensor_get_tvoc'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.CO2SENSOR_GET_TVOC);
    this.setOutput(true, null);
    this.setColour(330);
 this.setTooltip("");
 this.setHelpUrl("https://help.cocorobo.cn/");
  }
};


// nrf module 

Blockly.Blocks['thirdparty_nrf24l01_setup'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(new Blockly.FieldImage("./../blockly/media/third-party_nrf-sensor.png", 130, 50, { alt: "*", flipRtl: "FALSE" }));
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_SETUP_TITLE);
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_SETUP_CE_TEXT)
        .appendField(new Blockly.FieldDropdown([["D7","d7"]]), "thirdparty_nrf24l01_setup_dropdown_cepin")
        .appendField(Blockly.Msg.NRFMODULE_SETUP_CSN_TEXT)
        .appendField(new Blockly.FieldDropdown([["D5","d5"]]), "thirdparty_nrf24l01_setup_dropdown_csnpin");
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_SETUP_SET_ADDRESS)
        .appendField(new Blockly.FieldTextInput("00001"), "thirdparty_nrf24l01_setup_addr");
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Blockly.Blocks['thirdparty_nrf24l01_send_data'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_SEND_DATA_TITLE);
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_SEND_DATA_DATA_TEXT)
        .appendField(new Blockly.FieldTextInput("Hello World"), "thirdparty_nrf24l01_send_data_data");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Blockly.Blocks['thirdparty_nrf24l01_receive_data'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_RECEIVING_DATA_TEXT);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Blockly.Blocks['thirdparty_nrf24l01_data_received'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.NRFMODULE_GET_RECEIVED_DATA);
    this.setOutput(true, null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};