class ApiJamblastersController < ApiController

  before_filter :api_signed_in_user, except: [:login, :store_token, :auth_users, :can_pair, :is_allowed]

  respond_to :json

  # called from jamblaster
  def can_pair
    jbid = params[:jbid]
    user_id = params[:user_id]

    connection_a = nil

    connection_a = Connection.find_by_client_id(jbid) if jbid
    user = User.find_by_id(user_id) if user_id

    if connection_a.nil?
      render :json => {:message => "No connection found with client_id #{jbid}" , reason: "jbid"}, :status => 404
      return
    end

    if user.nil?
      render :json => {:message => "No user found with user id #{user_id}", reason: "user_id"}, :status => 404
      return
    end

    user_a = connection_a.user

    if user_a.nil?
      render :json => {:message => "No user found for jbid #{jbid}", reason: "user_a"}, :status => 404
      return
    end

    if user_a.id == user.id
      render :json => {}, :status => 200
    else
      render :json => {:message => "Users do not match for both client IDs", reason: "can_not_pair"}, :status => 403
    end
  end

  def is_allowed
    #Pass the jbid & cbid. Reply is no error on true, else error
    jbid = params[:jbid]
    user_id = params[:user_id]

    jamblaster = Jamblaster.find_by_client_id!(jbid)

    user = User.find_by_id(user_id)
    if user.nil?
      render :json => {:message => "No usepar associated with the user #{user_id}", reason: "user_id"}, :status => 404
      return
    end

#    if jamblaster && jamblaster.users.length > 0 && !user.jamblasters.include?(jamblaster)
#      render :json => {reason: "jamblaster_access", message: "current user does not have access to jamblaster #{jamblaster.id}"}, status: 403
#      return
#    end

    render :json => {}, status: 200
  end

  def auth_users
    # use by jamblaster to get all key (cid,key)* that are currently paired to it.

    jbid = params[:jbid]
    serial_no = params[:serial_no]

    @jamblaster = Jamblaster.where(client_id: jbid, serial_no: serial_no).first

    if @jamblaster.nil?
      render :json => {:message => "No jamblaster found with serial_no #{serial_no} and jbid #{jbid}", reason: "serial_no"}, :status => 404
      return
    end
  end

  def get_tokens
    @jamblasters = current_user.jamblasters
  end

  def start_pairing
    jamblaster = Jamblaster.find_by_client_id(params[:jbid])

    # allow multiple users to own a single jb
    #if jamblaster && jamblaster.users.length > 0 && !current_user.jamblasters.include?(jamblaster)
    #  render :json => {reason: "jamblaster_access", message: "current user does not have access to jamblaster #{jamblaster.id}"}, status: 403
    #  return
    #end

    @pairing = JamblasterPairingRequest.new
    @pairing.user = current_user
    @pairing.jamblaster_client_id = params[:jbid]
    @pairing.jamblaster = jamblaster
    @pairing.vtoken = params[:vtoken]
    puts "@PAIRING #{@pairing.inspect}"
    if !@pairing.save
      respond_with_model(@pairing)
    else

    end
  end

  def login
    user_id = params[:user_id]
    jbid = params[:jbid]
    vtoken = params[:vtoken]
    serial_no = params[:serial_no]
    pairing_request = JamblasterPairingRequest.where(jamblaster_client_id: jbid).where(user_id: user_id).where(vtoken: vtoken).first
    jamblaster = Jamblaster.find_by_serial_no(serial_no)

    if jamblaster.nil?
      render :json => {:message => "No jamblaster found with serial_no #{serial_no}" , reason: "serial_no"}, :status => 404
      return
    end

    if pairing_request.nil?
      render :json => {:message => "No pairing request found with jbid=#{jbid} && user_id=#{user_id} && vtoken=#{vtoken}", reason: "no_pairing_request"}, :status => 404
      return
    end

    render :json => {}, :status => 200
  end

  def store_token
    vtoken = params[:vtoken]
    scid = params[:scid]
    jbid = params[:jbid]
    key = params[:key]

    pairing_request = JamblasterPairingRequest.where(jamblaster_client_id: jbid).where(sibling_client_id: scid).where(sibling_key: key).first
    if pairing_request.nil?
      render :json => {:message => "No pairing request found with jbid=#{jbid} && sibling_client_id=#{scid} && sibling_key=#{key}", reason: "no_pairing_request"}, :status => 404
      return
    end

    if vtoken.blank?
      render :json => {:errors => {vtoken: ['is empty']}}, :status => 422
      return
    end

    @jamblaster = pairing_request.jamblaster
    @jamblaster.vtoken = vtoken
    if !@jamblaster.save
      respond_with_model(@jamblaster)
    else

    end

  end

  def pair
    key = params[:key]
    vtoken = params[:vtoken]
    user_id = params[:user_id]
    jbid = params[:jbid]

    jamblaster = Jamblaster.find_by_client_id!(jbid)

    if jamblaster.nil?
      render :json => {reason: "no_jbid", message: "No jamblaster found with jbid:#{jbid}"}, status: 404
      return
    end

    pairing_request = JamblasterPairingRequest.find_by_vtoken(vtoken)

    if pairing_request.nil?
      render :json => {:message => "No pairing request found with vtoken=#{vtoken}", reason: "no_pairing_request"}, :status => 404
      return
    end

    # allow multiples users to control a single jamblaster
    #if jamblaster.users.length > 0 && !pairing_request.user.jamblasters.include?(jamblaster)
    #  render :json => {reason: "jamblaster_access", message: "current user does not have access to jamblaster #{jamblaster.id} with vtoken #{vtoken}"}, status: 403
    #  return
    #end

    if !jamblaster.users.include?(pairing_request.user)
      jamblaster.users << pairing_request.user
      jamblaster.save!
    end

    pairing_request.activate(key)

    if pairing_request.errors.any?
      respond_with_model(pairing_request)
    else
      Jamblaster.send_pair_attempt(jbid, user_id, key)
      @jamblaster = jamblaster
    end
  end
end
