<template>

<q-page padding class="" >
<q-list class="">
<q-item class="">
  <q-item-section side>
    <q-btn color="secondary" round @click="send" icon="send">
      <q-tooltip>
        Click to send this command and payload to the sever
      </q-tooltip>
    </q-btn>
  </q-item-section>
  <q-item-section >
    <q-item-label>Command</q-item-label>
    <!-- <q-input label="Enter a . delimited command to send to server" v-model="cmd" />
   -->
    <q-select
        v-model="cmd"
        :options="commands"
        label="Choose An Available Command to Send"
        emit-value
      ></q-select>
  </q-item-section>
</q-item>

<q-item><div class="col">Property</div>:<div class="col">Value</div></q-item>
<q-item><q-input  class="col" readonly v-model="key[0]" />:<q-input dense class="col" v-model="value[0]" /></q-item>
<q-item><q-input class="col" readonly v-model="key[1]" />:<q-input class="col" v-model="value[1]" /></q-item>
<q-item><q-input class="col" label="a custom property in payload" v-model="key[2]" />:<q-input class="col" v-model="value[2]" /></q-item>
</q-list>
<!-- <q-btn :class="switches[0]" @click="toggle(1)">Switch 1</q-btn>
<q-btn :class="switches[1]"  @click="toggle(2)">Switch 2</q-btn>
<q-btn :class="switches[2]"  @click="toggle(3)">Switch 3</q-btn>
<q-btn :class="switches[3]"  @click="toggle(4)">Switch 4</q-btn> -->
<q-list class="response">
<q-input  v-model="packet" readonly  label="Sending Packet:" />
<q-input  v-model="resMsg" readonly label="Socket/Server Response Message:" />
<q-input  v-model="resPayload" readonly label="Socket/Server Response Payload:" />
<q-input  v-model="pushed" readonly type="textarea" label="Pushed from Socket/Server:" />
</q-list>
</q-page>
</template>
<script>
import btc from 'better-try-catch'
// import socket from '../socket.js'

export default {
  data () {
    return {
      cmd: 'on',
      commands: [
        {
          label: 'echo',
          value: 'echo',
          description: 'echo',
          icon: 'no'
        },
        {
          label: 'on',
          value: 'on',
          description: 'turn on something',
          icon: 'mdi:lightbulb'
        },
        {
          label: 'off',
          value: 'off',
          description: 'turn off something',
          icon: 'lightbulb-off'
        },
        {
          label: 'bogus',
          value: 'bogus',
          description: 'turn off something',
          icon: 'no'
        }
      ],
      packet: '',
      resMsg: '',
      resPayload: '',
      key: ['id', 'brightness'],
      value: ['not set', 0],
      pushed: '',
      switches: ['off', 'off', 'off', 'off']
    }
  },
  methods: {
    async send () {
      this.clear()
      let packet = {
        cmd: this.cmd,
        [this.key[0]]: this.value[0],
        [this.key[1]]: this.value[1],
        [this.key[2]]: this.value[2]
      }
      this.packet = JSON.stringify(packet)
      let [err, res] = await btc(this.$socket.send)(packet)
      if (err) {
        console.log('error ', err)
        this.$q.notify({
          color: 'negative',
          message: 'Error in repsonse of packet send'
        })
      } else {
        delete res._header
        this.resMsg = res.msg
        this.resPayload = JSON.stringify(res.payload)
      }
    },
    clear () {
      this.pushed = ''
      this.resMsg = ''
      this.resPayload = ''
      this.packet = ''
    }
  },
  async mounted () {
    this.$q.notify({
      color: 'info',
      message: `Client connecting to', ${this.$socket.url}`
    })
    let [err] = await btc(this.$socket.connect)()
    if (err) {
      this.$q.notify({
        color: 'negative',
        message: 'Websocket Server Not Available'
      })
    } else {
      this.$q.notify({ color: 'positive', message: 'Ready' })
      this.$socket.listen()
      this.clear()
    }
    this.$socket.on('pushed', packet => {
      delete packet._header
      this.pushed = JSON.stringify(packet)
    })
  }
}
</script>

<style lang="stylus">

.on
    background $positive

.off
    background grey

</style>
