import debug from 'debug'
import EventEmitter from 'events'
import protocol from '../src/protocolDefinitions'

let numberOfMessages = 1

export default class MRIL extends EventEmitter {
  constructor(instruction = '') {
    super()

    this.instruction = instruction
    this.bytes = 0
    this.preparedMRILMessage = 0
    this.number = numberOfMessages++

    this.mrcp

    this.state = {
      executing: false,
      executed: false,
    }

    // remove whitespace and number
    this.preparedMRILMessage = instruction.split(' ').join('')
      .replace(new RegExp(`${protocol.MRIL.COMMAND_NUMBER}\\d+`, 'gi'), '')

    if (this.preparedMRILMessage.indexOf('#') > 0) { // remove comment
      this.preparedMRILMessage = this.preparedMRILMessage.substring(0, this.preparedMRILMessage.indexOf('#'))
    }

    this.preparedMRILMessage = protocol.MRIL.COMMAND_NUMBER + this.number + this.preparedMRILMessage

    this.bytes = this.preparedMRILMessage.length
  }
/**
 * the instruction send to the robot
 * @return {[string]} preparedMRILMessage
 */
  getInstruction() {
    return this.preparedMRILMessage
  }
/**
 * the instruction used to construct the MRIL object. (number and spacing may differ)
 * @return {[type]} raw instruction
 */
  getRawInstruction() {
    return this.instruction
  }

  setExecuting() {
    this.state.executing = true
    this.emit('executing')
  }

  isExecuting() {
    return this.state.executing
  }

  onExecuting(cb) {
    this.on('executing', cb)
  }

  setExecuted(response = '') {
    this.state.executed = true
    this.response = response
    this.emit('executed', response)
  }

  isExecuted() {
    return this.state.executed
  }

  onExecuted(cb) {
    this.on('executed', cb)
  }

  getNumber() {
    return this.number
  }

  getBytes() {
    return this.bytes
  }

}

@@ -311,17 +311,18 @@
 ssage = 
-0
+''
 %0A    thi
@@ -436,24 +436,25 @@
 lse,%0A    %7D%0A%0A
+%0A
     // remov
@@ -805,16 +805,96 @@
 %0A    %7D%0A%0A
+    if (instruction.length !== 0) %7B // no message number for an empty message%0A  
     this
@@ -985,16 +985,22 @@
 LMessage
+%0A    %7D
 %0A%0A    th

allow empty message