'use strict';

var Backbone = require('backbone'),
    app = require('../App.js').getApp();

module.exports = Backbone.View.extend({

  events: {

  },

  initialize: function(){
    var self = this,
        socketAddress = app.serverConfigs.getActive().getApiSocketUrl();

    //socket should be opened when view is created, and stay open
    try {
      this.socketConnection = new WebSocket(socketAddress);
      this.socketConnection.onmessage = function (e) {
        self.socketMessage(e);
      };
      this.socketConnection.onerror = function (e) {
        self.socketError(e);
      };
    } catch (exception){
      console.log(socketAddress, window.polyglot.t('errorMessages.socketError') + "<br/><br/>" + exception);
      app.simpleMessageModal.open({
        title: 'The API WebSocket cannot be reached.',
        message: '<i>Interface will continue loading, but some functionality will not be available.</i>'
      });
    }
  },

  waitForSocket: function(message) {
    var self = this;
    if (this.socketConnection.readyState === 1){
      this.socketConnection.send(message);
    } else {
      //if socket is not ready yet, try again
      this.socketTimer = setTimeout(function(){
        self.waitForSocket(message);
      }, 1000);
    }
  },

  socketMessage: function(e){
    window.obEventBus.trigger("socketMessageReceived", e);
  },

  socketError: function() {
    app.simpleMessageModal.open({
      title: 'The API WebSocket cannot be reached.',
      message: '<i>Interface will continue loading, but some functionality will not be available.</i>'
    });    
  },

  getItems: function(wsID, only_following){
    only_following = Boolean(only_following);
    //id should be generated by the view that asks for the request
    var message = {"request": {
      "api": "v1",
      "id": wsID,
      "command": "get_homepage_listings",
      "only_following": only_following
    }};

    this.waitForSocket(JSON.stringify(message));
  },

  getVendors: function(wsID){
    //id should be generated by the view that asks for the request
    var message = {"request": {
      "api": "v1",
      "id": wsID,
      "command": "get_vendors"
    }};
    this.waitForSocket(JSON.stringify(message));
  },

  getModerators: function(wsID){
    //id should be generated by the view that asks for the request
    var message = {
      "request": {
        "api": "v1",
        "id": wsID,
        "command": "get_moderators"
      }
    };
    this.waitForSocket(JSON.stringify(message));
  },

  getNotifications: function(wsID) {
    //id should be generated by the view that asks for the request
    var message = {
      "request": {
        "api": "v1",
        "id": wsID,
        "command": "get_notifications"
      }
    };
    this.waitForSocket(JSON.stringify(message));
  },

  search: function(wsID, keyword) {
    //id should be generated by the view that asks for the request
    var message = {
      "request": {
        "api": "v1",
        "id": wsID,
        "command": "search",
        "keyword": keyword
      }
    };
    this.waitForSocket(JSON.stringify(message));
  },

  sendMessage: function(chatMessage) {
    //id should be generated by the view that asks for the request
    this.waitForSocket(JSON.stringify(chatMessage));
  },

  render: function(){
    return this;
  },

  close: function(){
    this.remove();
  }
});
