import { pinIoCategory } from '../blocks/categories/pinIo.js';
import { sensorsCategory } from '../blocks/categories/sensors.js';
import { timeCategory } from '../blocks/categories/time.js';
import { serialPrintCategory } from '../blocks/categories/serialPrint.js';
import { randomCategory } from '../blocks/categories/random.js';
import { hbridgeMotorCategory } from '../blocks/categories/hbridgeMotor.js';

const analogCategory = {
  kind: 'category',
  name: 'Analog',
  colour: '30',
  contents: [
    { kind: 'block', type: 'arduino_analog_read' },
    {
      kind: 'block',
      type: 'arduino_analog_write',
      inputs: {
        VALUE: { shadow: { type: 'math_number', fields: { NUM: 128 } } },
      },
    },
    {
      kind: 'block',
      type: 'arduino_map',
      inputs: {
        VALUE: { shadow: { type: 'math_number', fields: { NUM: 512 } } },
        FROM_LOW: { shadow: { type: 'math_number', fields: { NUM: 0 } } },
        FROM_HIGH: { shadow: { type: 'math_number', fields: { NUM: 1023 } } },
        TO_LOW: { shadow: { type: 'math_number', fields: { NUM: 0 } } },
        TO_HIGH: { shadow: { type: 'math_number', fields: { NUM: 255 } } },
      },
    },
  ],
};

const servoCategory = {
  kind: 'category',
  name: 'Servo',
  colour: '260',
  contents: [
    { kind: 'block', type: 'arduino_servo_attach' },
    {
      kind: 'block',
      type: 'arduino_servo_write',
      inputs: {
        ANGLE: { shadow: { type: 'math_number', fields: { NUM: 90 } } },
      },
    },
  ],
};

const arduinoSoundCategory = {
  kind: 'category',
  name: 'Sound',
  colour: '300',
  contents: [
    {
      kind: 'block',
      type: 'arduino_tone',
      inputs: {
        FREQ: { shadow: { type: 'math_number', fields: { NUM: 440 } } },
        DURATION: { shadow: { type: 'math_number', fields: { NUM: 500 } } },
      },
    },
    { kind: 'block', type: 'arduino_no_tone' },
  ],
};

const arduinoPinIoCategory = {
  kind: 'category',
  name: 'Pin I/O',
  colour: '230',
  contents: [
    ...pinIoCategory.contents,
    { kind: 'block', type: 'arduino_builtin_led' },
  ],
};

export const arduinoUno = {
  id: 'arduino_uno',
  label: 'Arduino Uno/Nano',
  language: 'arduino',
  firmware: null,
  categories: [
    arduinoPinIoCategory,
    analogCategory,
    servoCategory,
    arduinoSoundCategory,
    sensorsCategory,
    timeCategory,
    hbridgeMotorCategory,
    serialPrintCategory,
    randomCategory,
  ],
};
