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

'use strict';

goog.provide('Blockly.Arduino.Pixetto');

goog.require('Blockly.Arduino');

/** . */
Blockly.Arduino['pixetto_setup'] = function (block) {
    var rxPin = block.getFieldValue('PIXETTO_RX') || 'A4';
    var txPin = block.getFieldValue('PIXETTO_TX') || 'A5';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    Blockly.Arduino.addInclude('Pixetto_inc', '#include <Pixetto.h>');
    Blockly.Arduino.addVariable("ss", 'Pixetto ' + pixettoId + '(' + rxPin + ', ' + txPin + ');', true);

    var code = pixettoId + '.begin();\n' + 'delay(1000); //等待Pixetto開機，避免Pixetto進入當機模式\n';

    return code;
};

/** . */
Blockly.Arduino['pixetto_uvc'] = function (block) {
    // Pixetto 1.6.x does not expose an enableUVC() method.  Keep legacy XML
    // loadable without emitting a call that fails at compile time.
    return '// Pixetto UVC 設定由感測器韌體管理；此舊積木不產生額外程式碼。\n';
};

/** . */
Blockly.Arduino['pixetto_set_detected'] = function (block) {
    // Pixetto 1.6.x has no setDetectMode() API.  Preserve the legacy block
    // while avoiding undefined-member compile errors.
    return '// Pixetto 偵測模式由 enableFunc() 控制；此舊積木不產生額外程式碼。\n';
};

/** . */
Blockly.Arduino['pixetto_flush'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.flush();\n';

    return code;
};

/** . */
Blockly.Arduino['pixetto_function'] = function (block) {
    var pixettoFunc = block.getFieldValue('PIXETTO_FUNC_SELECTOR') || '18';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);
    if (pixettoFunc != '18')
        pixettoFunc = 'Pixetto::' + pixettoFunc;
    var code = pixettoId + '.enableFunc(' + pixettoFunc + ');\n';

    return code;
};

/** . */
Blockly.Arduino['pixetto_detected'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.isDetected()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_now_function'] = function (block) {
    var pixettoFunc = block.getFieldValue('PIXETTO_FUNC_SELECTOR') || '18';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);
    if (pixettoFunc != '18')
        pixettoFunc = 'Pixetto::' + pixettoFunc;
    var code = pixettoId + '.getFuncID() == ' + pixettoFunc;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_getfuncid'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_gettypeid'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getTypeID()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_getposx'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getPosX()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_getposy'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getPosY()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_getwidth'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getWidth()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_getheigth'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getHeight()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_numobjects'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.numObjects()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_color_detection'] = function (block) {
    var pixettoColor = block.getFieldValue('PIXETTO_COLOR_SELECTOR') || 'COLOR_RED';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_COLOR_DETECTION && ' +
        pixettoId + '.getTypeID() == Pixetto::' + pixettoColor;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_shape_detection'] = function (block) {
    var pixettoShape = block.getFieldValue('PIXETTO_SHAPE_SELECTOR') || 'SHAPE_TRIANGLE';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_SHAPE_DETECTION && ' +
        pixettoId + '.getTypeID() == Pixetto::' + pixettoShape;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_sphere_detection'] = function (block) {
    var pixettoSphere = block.getFieldValue('PIXETTO_SPHERE_SELECTOR') || 'COLOR_RED';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_SPHERE_DETECTION && ' +
        pixettoId + '.getTypeID() == Pixetto::' + pixettoSphere;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_template_detection'] = function (block) {
    var pixettoTemplate = block.getFieldValue('PIXETTO_TEMPLATE_SELECTOR') || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_TEMPLATE_MATCHING && ' +
        pixettoId + '.getTypeID() == ' + pixettoTemplate;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_keypoints_detection'] = function (block) {
    var pixettoKeypoints = block.getFieldValue('PIXETTO_KEYPOINTS_SELECTOR') || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_KEYPOINTS && ' +
        pixettoId + '.getTypeID() == ' + pixettoKeypoints;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_neural_network_detection'] = function (block) {
    var pixettoNeuralNetwork = Blockly.Arduino.valueToCode(block, 'PIXETTO_NEURAL_NETWORK', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_NEURAL_NETWORK && ' +
        pixettoId + '.getTypeID() == ' + pixettoNeuralNetwork;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_sign_detection'] = function (block) {
    var pixettoSign = block.getFieldValue('PIXETTO_SIGN_SELECTOR') || 'SIGN_STOP';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_TRAFFIC_SIGN_DETECTION && ' +
        pixettoId + '.getTypeID() == Pixetto::' + pixettoSign;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_num_detection'] = function (block) {
    var pixettoNum = block.getFieldValue('PIXETTO_NUM_SELECTOR') || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_HANDWRITTEN_DIGITS_DETECTION && ' +
        pixettoId + '.getTypeID() == ' + pixettoNum;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_latter_detection'] = function (block) {
    var pixettoLatter = block.getFieldValue('PIXETTO_LATTER_SELECTOR') || 'LETTER_A';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_HANDWRITTEN_LETTERS_DETECTION && ' +
        pixettoId + '.getTypeID() == ' + pixettoLatter;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_cloud_detection'] = function (block) {
    var pixettoCloud = Blockly.Arduino.valueToCode(block, 'PIXETTO_CLOUD', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_CLOUD_DETECTION && ' +
        pixettoId + '.getTypeID() == ' + pixettoCloud;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_classifier_detection'] = function (block) {
    var pixettoClassifier = Blockly.Arduino.valueToCode(block, 'PIXETTO_CLASSIFIER', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_SIMPLE_CLASSIFIER && ' +
        pixettoId + '.getTypeID() == ' + pixettoClassifier;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_equation_expression'] = function (block) {
    var pixettoEquationExp = block.getFieldValue('PIXETTO_EQUATION_EXP') || 'pixetto_equation';
    var pixettoEquationExpId = Blockly.Arduino.variableDB_.getName(
        pixettoEquationExp,
        Blockly.Variables.NAME_TYPE);
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    Blockly.Arduino.addVariable(pixettoEquationExpId, 'char ' + pixettoEquationExpId + '[16];', true);
    var code = pixettoId + '.getEquationExpr(' + pixettoEquationExpId + ', sizeof(' + pixettoEquationExpId + '));\n';

    return code;
};

/** . */
Blockly.Arduino['pixetto_voice_detection'] = function (block) {
    var pixettoVoice = block.getFieldValue('PIXETTO_VOICE_SELECTOR') || 'VOICE_Hello';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getFuncID() == Pixetto::FUNC_VOICE_COMMAND && ' +
        pixettoId + '.getTypeID() == ' + pixettoVoice;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_apriltag_detection'] = function (block) {
    var pixettoApriltag = block.getFieldValue('PIXETTO_APRILTAG_SELECTOR') || 'APRILTAG_ID';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = "";
    if (pixettoApriltag == "APRILTAG_ID") {
        code = pixettoId + '.getTypeID()';
    }
    else {
        code = pixettoId + '.getApriltagField(Pixetto::' + pixettoApriltag + ')';
    }

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_lanes_detection'] = function (block) {
    var pixettoLanes = block.getFieldValue('PIXETTO_LANES_SELECTOR') || 'LANES_LX1';
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getLanesField(Pixetto::' + pixettoLanes + ')';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_equation_answer'] = function (block) {
    var pixettoId = Blockly.Arduino.variableDB_.getName(
        "ss",
        Blockly.Variables.NAME_TYPE/*blocklyArray_NAME_TYPE*/);

    var code = pixettoId + '.getEquationAnswer()';

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_color'] = function (block) {
    var pixettoColor = block.getFieldValue('PIXETTO_COLOR_SELECTOR') || 'COLOR_RED';
    var code = 'Pixetto::' + pixettoColor;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_shape'] = function (block) {
    var pixettoShape = block.getFieldValue('PIXETTO_SHAPE_SELECTOR') || 'SHAPE_TRIANGLE';
    var code = 'Pixetto::' + pixettoShape;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_sign'] = function (block) {
    var pixettoSign = block.getFieldValue('PIXETTO_SIGN_SELECTOR') || 'SIGN_STOP';
    var code = 'Pixetto::' + pixettoSign;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_num'] = function (block) {
    var pixettoNum = block.getFieldValue('PIXETTO_NUM_SELECTOR') || '0';
    var code = pixettoNum;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/** . */
Blockly.Arduino['pixetto_latter'] = function (block) {
    var pixettoLatter = block.getFieldValue('PIXETTO_LATTER_SELECTOR') || 'LETTER_A';
    var code = 'Pixetto::' + pixettoLatter;

    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
