import { pythonGenerator } from "blockly/python";

const Blockly = {
    Python: pythonGenerator || { Msg: Object.create(null) }
};
Blockly.Python['math_degrad'] = function (block) {
    var dropdown_rad_deg = block.getFieldValue('rad_deg');
    var value_convert = Blockly.Python.valueToCode(block, 'convert', Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    Blockly.Python.definitions_['import_math'] = 'import math\n';

    var code = 'math.' + dropdown_rad_deg + '(' + value_convert + ')';
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Python.math_angle = function (a) {
    return [a.getFieldValue("angle"), Blockly.Python.ORDER_ATOMIC]
};

Blockly.Python.math_keep_decimal = function (a) {
    var b = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "PLACE", Blockly.Python.ORDER_ATOMIC);
    return ["round(" + b + ", " + a + ")", Blockly.Python.ORDER_ATOMIC]
};

Blockly.Python.math_division_consult = function (a) {
    var b = Blockly.Python.valueToCode(a, "DIVIDEND", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
    a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
    return [b + " // " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
}

Blockly.Python.labplus_mapping = function (a) {
    Blockly.Python.definitions_.import_microbit = "from microbit import *";
    var b = Blockly.Python.valueToCode(a, "inputNum", Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.valueToCode(a, "outputNum", Blockly.Python.ORDER_ATOMIC);
    var c = Blockly.Python.valueToCode(a, "bMin", Blockly.Python.ORDER_ATOMIC)
        , d = Blockly.Python.valueToCode(a, "bMax", Blockly.Python.ORDER_ATOMIC)
        , e = Blockly.Python.valueToCode(a, "cMin", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "cMax", Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.definitions_.labplus_mapping = "def numberMap(inputNum,bMin,bMax,cMin,cMax):\n  outputNum \x3d 0\n  outputNum \x3d((cMax - cMin) / (bMax - bMin))*(inputNum - bMin)+cMin\n  return outputNum\n";
    return ["numberMap(" + b + "," + c + "," + d + "," + e + "," + a + ")", Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.math_random_randrange = function (a) {
    Blockly.Python.definitions_.import_time = "import time";
    // Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
    Blockly.Python.definitions_.import_random = "import random";
    var b = Blockly.Python.valueToCode(a, "start", Blockly.Python.ORDER_ATOMIC),
        c = Blockly.Python.valueToCode(a, "stop", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "step", Blockly.Python.ORDER_ATOMIC);
    return ["random.randrange(" + b + ", " + c + ", " + a + ")", Blockly.Python.ORDER_ATOMIC]
};

Blockly.Python.bit_inversion = function (a) {
    return ["~" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_ten_convert_to = function (a) {
    var b = Blockly.Python.valueToCode(a, "int_num", Blockly.Python.ORDER_ATOMIC);
    return [a.getFieldValue("ten_convert_to") + "(" + b + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_convert_to_ten = function (a) {
    var b = Blockly.Python.valueToCode(a, "convert_str", Blockly.Python.ORDER_ATOMIC);
    a = a.getFieldValue("convert_choice");
    return ["int(" + b + ", " + a + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_ten_convert_to = function (a) {
    var b = Blockly.Python.valueToCode(a, "int_num", Blockly.Python.ORDER_ATOMIC);
    return [a.getFieldValue("ten_convert_to") + "(" + b + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_high_low = function (a) {
    return [a.getFieldValue("high_low"), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_baudrate = function (a) {
    return [a.getFieldValue("baudrate"), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_start_new_thread = function (a) {
    Blockly.Python.definitions_.import_thread = "import _thread";
    var b = a.getFieldValue("thread_name"),
        c = Blockly.Python.statementToCode(a, "DO"),
        c = Blockly.Python.addLoopTrap(c, a.id) || Blockly.Python.PASS;
    Blockly.Python.codeFunctions_["thread_" + b] = "def thread_" + b + "():\n" + c;
    return "_thread.start_new_thread(thread_" + b + ", ())\n"
};

Blockly.Python.CocoRobo_ten_converted_to_bytes = function (a) {
    Blockly.Python.definitions_.import_struct = "import struct";
    var b = Blockly.Python.valueToCode(a, "convert_num", Blockly.Python.ORDER_ATOMIC);
    return ["struct.pack(" + a.getFieldValue("convert_choice") + ", " + b + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_int_to_chr = function (a) {
    return ["chr(" + Blockly.Python.valueToCode(a, "convert_num", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_hcsr04_obj = function (a) {
    Blockly.Python.definitions_.from_hcsr04_import_HCSR04 = "from hcsr04 import HCSR04";
    //Blockly.Python.definitions_.import_CocoRobo = "from CocoRobo import *";
    var b = a.getFieldValue("hcsr04_name"),
        c = Blockly.Python.valueToCode(a, "trigger", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "echo", Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.definitions_["CocoRobo_hcsr04_object" + b] = b + " \x3d HCSR04(trigger_pin\x3dPin.P" + c + ", echo_pin\x3dPin.P" + a + ")";
    return "\n"
};
Blockly.Python.CocoRobo_hcsr04_distance = function (a) {
    Blockly.Python.definitions_.from_hcsr04_import_HCSR04 = "from hcsr04 import HCSR04";
    //Blockly.Python.definitions_.import_CocoRobo = "from CocoRobo import *";
    var b = a.getFieldValue("hcsr04_name");
    a = a.getFieldValue("unit");
    return [b + ".distance_" + a + "()", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_hcsr04_distance_mm = function (a) {
    Blockly.Python.definitions_.from_hcsr04_import_HCSR04 = "from hcsr04 import HCSR04";
    //Blockly.Python.definitions_.import_CocoRobo = "from CocoRobo import *";
    return [a.getFieldValue("hcsr04_name") + ".distance_mm()", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_pm2_5_MIC = function (a) {
    //Blockly.Python.definitions_.import_CocoRobo = "from CocoRobo import *";
    Blockly.Python.definitions_.machine_Timer = "from machine import Timer";
    var b = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.codeFunctions_.CocoRobo_pm2_5 = "uart\x3dUART(1, baudrate\x3d9600, rx\x3dPin.P" + b + ", timeout\x3d200)\n_pm_data \x3d [None, None, None, None, None, None, None, None, None]\ndef timer13_tick(_):\n  global _pm_data\n  try:\n      if uart.any():\n          d \x3d uart.read(0x42)\n          if d[0] \x3d\x3d 0x42 and d[1] \x3d\x3d 0x4d:\n              _pm_data \x3d [d[5], d[7], d[9], d[17], d[19], d[21], d[23], d[25], d[27]]\n  except: pass\n\ntim13 \x3d Timer(13)\ntim13.init(period\x3d1000, mode\x3dTimer.PERIODIC, callback\x3dtimer13_tick)";
    return [a.getFieldValue("unit"), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_pm2_5_LitresAir = function (a) {
    //Blockly.Python.definitions_.import_CocoRobo = "from CocoRobo import *";
    Blockly.Python.definitions_.machine_Timer = "from machine import Timer";
    var b = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.codeFunctions_.CocoRobo_pm2_5 = "uart\x3dUART(1, baudrate\x3d9600, rx\x3dPin.P" + b + ", timeout\x3d200)\n_pm_data \x3d [None, None, None, None, None, None, None, None, None]\ndef timer13_tick(_):\n  global _pm_data\n  try:\n      if uart.any():\n          d \x3d uart.read(0x42)\n          if d[0] \x3d\x3d 0x42 and d[1] \x3d\x3d 0x4d:\n              _pm_data \x3d [d[5], d[7], d[9], d[17], d[19], d[21], d[23], d[25], d[27]]\n  except: pass\n\ntim13 \x3d Timer(13)\ntim13.init(period\x3d1000, mode\x3dTimer.PERIODIC, callback\x3dtimer13_tick)";
    return [a.getFieldValue("unit"), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_mac_address = function (a) {
    Blockly.Python.definitions_.import_machine = "import machine";
    Blockly.Python.definitions_.import_ubinascii = "import ubinascii";
    return ["ubinascii.hexlify(machine.unique_id()).decode().upper()", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_ujson_loads = function (a) {
    Blockly.Python.definitions_.import_ujson = "import json";
    return ["json.loads(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_code_annotation = function (a) {
    var code = Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC);
    return "# " + code.replace('"', '').replace('"', '') + "\n";
};
Blockly.Python.CocoRobo_ujson_dumps = function (a) {
    Blockly.Python.definitions_.import_ujson = "import json";
    return ["json.dumps(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_base64_to_data = function (a) {
    Blockly.Python.definitions_.v831_import_base64 = "import base64";
    return ["base64.b64decode(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_data_to_base64 = function (a) {
    Blockly.Python.definitions_.v831_import_base64 = "import base64";
    return ["base64.b64encode(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ".encode('utf-8'))", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_hex_to_bin_str = function (a) {
    return ["bytes.fromhex(" + Blockly.Python.valueToCode(a, "convert_chr", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_str_to_hex = function (a) {
    return [Blockly.Python.valueToCode(a, "convert_chr", Blockly.Python.ORDER_ATOMIC) + ".encode().hex()", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_chr_to_int = function (a) {
    return ["ord(" + Blockly.Python.valueToCode(a, "convert_chr", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.math_convert = function (a) {
    var b = a.getFieldValue("TYPE0");
    a = Blockly.Python.valueToCode(a, "INPUT0", Blockly.Python.ORDER_ATOMIC);
    return [b + "(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
};
Blockly.Python.math_number_bits_ops = function (a) {
    var b = {
        " | ": [" | ", Blockly.Python.ORDER_BITWISE_OR],
        " & ": [" & ", Blockly.Python.ORDER_BITWISE_AND],
        " ^ ": [" ^ ", Blockly.Python.ORDER_BITWISE_XOR],
        " >> ": [" >> ", Blockly.Python.ORDER_BITWISE_SHIFT],
        " << ": [" << ", Blockly.Python.ORDER_BITWISE_SHIFT]
    }[a.getFieldValue("OP")]
        , c = b[0];
    b = b[1];
    var d = Blockly.Python.valueToCode(a, "A", b) || "0";
    a = Blockly.Python.valueToCode(a, "B", b) || "0";
    return [d + c + a, b]
}

Blockly.Python.controls_repeat_forever = function (a) {
    var b = Blockly.Python.statementToCode(a, "DO");
    b = Blockly.Python.addLoopTrap(b, a.id) || Blockly.Python.PASS;
    return "while True:\n" + b
}
export default Blockly