class ClientDevicesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_org_id
  # before_action :set_upgrade, only: [:show, :edit, :update, :destroy]
  # load_and_authorize_resource :class => Upgrade
  # skip_load_resource :only => [:new, :create]

  # GET /upgrades
  # GET /upgrades.json
  def index
    @client_devices = ClientDevice.get_all @org_id
  end

  def client_details
    # binding.pry
    values = ClientDevice.get_all @org_id
    # render json: { val: values} 
    hsh = {}
    values.each {|i| hsh[i[:mac]] = {:name => (i[:nick_name] || i[:original_name].to_s), :original_name=> i[:original_name], :mac => i[:mac]}}
    render :json=> hsh
  end

  # GET /upgrades/1
  # GET /upgrades/1.json
  def show
  end

  # GET /upgrades/new
  def new
    @client_devices = ClientDevice.new
  end

  # GET /upgrades/1/edit
  def edit
  end

  def save_client
    hsh = {}
    hsh["org_id"] = @org_id
    val_params = upgrade_params.merge(hsh)
    cd = ClientDevice.where(:mac => val_params["mac"]).to_a
    if cd.present?
      val = ClientDevice.update_data cd.first, val_params
      flash[:success] = "ClientDevice #{val} was updated Successfully"
      redirect_to controller: 'clients', action: 'client_show',  mac_id: params["mac"], nas_id: params["nas_id"]
    else
      val = ClientDevice.save_data val_params
      flash[:success] = "ClientDevice #{val} was saved Successfully"
      redirect_to controller: 'clients', action: 'client_show',  mac_id: params["mac"], nas_id: params["nas_id"]
    end
  end

  def client_actions
    if params["mac"].present? && params["nas_id"].present?
      l_n_id = RouterInventory.where(:mac_id => params["nas_id"]).last.location_network_id
      client_device = ClientDevice.where(:mac => params["mac"], :ln_id => l_n_id).last
    elsif params["mac"].present? && params["network_id"].present?
      client_device = ClientDevice.where(:mac => params["mac"], :ln_id => params["network_id"]).last
    else params["id"].present?
      client_device = ClientDevice.where(:id => params["id"]).last
    end
    if client_device.blank?
      hsh = {"org_id" => @org_id, "ln_id"=>l_n_id, "is_blocked"=>false}
      val_params = params.merge(hsh)
      client_device = ClientDevice.save_data val_params
    end
    if params["client_action"] == "block"
      val = client_device.update(is_blocked: true)
      msg = "ClientDevice was Blocked Successfully"
    else
      val = client_device.update(is_blocked: false)
      msg = "ClientDevice was Unblocked Successfully"
    end
    json_data = {data: client_device.json_build, status: 200, msg: msg} 
    render json: json_data, status: json_data[:status]
  end

  # POST /upgrades
  # POST /upgrades.json
  def create
    hsh = {}
    hsh["org_id"] = @org_id
    val_params = upgrade_params.merge(hsh)
    val = ClientDevice.save_data val_params
    flash[:success] = "ClientDevice #{val} was saved Successfully"
    redirect_to controller: 'clients', action: 'client_show',  mac_id: params["mac"], nas_id: params["nas_id"]
  end


  private

    def set_org_id
      @org_id = current_user.organisation.id
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def upgrade_params
      params.permit(:name, :original_name, :description, :ip, :mac, :monitoring, :critical, :nas_id, :nick_name, :is_blocked)
    end
end
