<template>
  <div class="sim-worker">
    <q-linear-progress size="25px" stripe rounded :value="progress" color="primary" class="q-mt-sm">
      <div class="absolute-full flex flex-center">
        <q-badge color="white" text-color="primary" :label="progressLabel"></q-badge>
      </div>
    </q-linear-progress>
  </div>
</template>

<script>
module.exports = {
  name: 'SimWorker',
  components: {
  },
  props: {
    simSerialNumber: String,
    subscriptionId: String,
    stompClient: Object
  },
  data: function() {
    return {
      allocationInfo: null,
      messagesSent: 0,
      currentMessage : null
    }
  },
  computed: {
    progress() {
      if (this.allocationInfo == null) {
        return 0;
      }
      var totalMessageCount = this.allocationInfo.totalMessageCount;
      return totalMessageCount > 0 ? this.messagesSent / totalMessageCount : 0;
    },
    progressLabel() {
      if (this.allocationInfo == null) {
        return "Fără sarcini";
      }
      var totalMessageCount = this.allocationInfo.totalMessageCount;
      return totalMessageCount > 0 ? `${this.messagesSent} / ${totalMessageCount}${this.currentMessage != null ? ': ' + this.currentMessage.to : ''}` : "Fără sarcini";
    }
  },
  methods: {
    confirmAllocation() {
      this.stompClient.sendJson(
          "/app/sms/workers/" + this.simSerialNumber + "/confirm-allocation",
          this.allocationInfo.allocationUuid
      );
    },
  },
  created() {
    console.log("Subscribed - " + this.simSerialNumber);
    this.stompClient.subscribe(
        "/user/queue/sms/" + this.simSerialNumber,
        (message) => {
          var data = message.data;
          switch(data["@class"]) {
            case "com.ease.gsms.server.controllers.api.v1.AllocationInfo":
              this.messagesSent = 0;
              this.allocationInfo = data;
              this.confirmAllocation();
              break;
            case "com.ease.gsms.server.model.Message":
              this.currentMessage = data;
              var allocationUuid = this.allocationInfo.allocationUuid;

              SmsSender.send(
                  this.subscriptionId,
                  data.to,
                  data.content,
                  (status) => {
                    this.messagesSent++;
                    this.stompClient.sendJson(
                        "/app/sms/workers/" + this.simSerialNumber + "/message-sent/" + data.id,
                        {
                          allocation: allocationUuid,
                          messageId: data.id,
                          dispatchId: data.dispatchRequestId,
                          sentDate: new Date(),
                          sendingStatusId: status
                        }
                    );
                  },
                  data.deliveryStatus !== -1, // we set delivery status id to -1 when delivery reports are not requested in this dispatch
                  (status) => {
                    this.stompClient.sendJson(
                        "/app/sms/workers/" + this.simSerialNumber + "/message-delivered/" + data.id,
                        {
                          allocation: allocationUuid,
                          messageId: data.id,
                          dispatchId: data.dispatchRequestId,
                          deliveryDate: new Date(),
                          deliveryStatusId: status
                        }
                    );

                  }
              );
          }
        }
    );
    this.stompClient.sendJson(
        "/app/sms/workers/" + this.simSerialNumber + "/register",
        {}
    );
  },
  beforeDestroy() {
    this.stompClient.sendJson(
        "/app/sms/workers/" + this.simSerialNumber + "/unregister",
        {}
    );
    this.stompClient.unsubscribe(
        "/user/queue/sms/" + this.simSerialNumber
    );
    console.log("Unsubscribed - " + this.simSerialNumber);
  }
}

var NoopSender = {
  send: function(subscriptionId, phone, message, onSent, requestDeliveryReport, onDelivered) {
    setTimeout(() => {
      onSent(1);
      if (requestDeliveryReport) {
        setTimeout(() => {
          onDelivered(1);
        }, 5000); // fake delivery updates
      }
    }, 1000);
  }
}

var SimSender = {
  send: function(subscriptionId, phone, message, onSent, requestDeliveryReport, onDelivered) {
    sms.send(
        phone,
        subscriptionId,
        message,
        (info) => onSent(info.statusId),
        (info) => onSent(info.statusId),
        requestDeliveryReport,
        (info) => onDelivered(info.statusId)
    );
  }
};

var SmsSender = SimSender;

</script>

<style scoped>
  div.sim-worker {
    margin-bottom: 5px;
    width: 100%;
  }
</style>
