class Admin::MasterInventoryController < AdminController
  before_action :authenticate_user!
  before_action :dashboard_topbar
  skip_before_action :check_permission
  before_action :allow_user
  before_action :set_router_inventory_group, only: [:change_router_presence]

  def index
    if params[:search].present?
      @q = params[:search].to_s
      @ris = RouterInventory.search("#{@q}")
      # @ris = Kaminari.paginate_array(@ris).page(params[:page]).per(5)
      @inventory_up_list = RouterInventory.find_up_list(@ris.map(&:mac_id), RouterInventory.get_hb_intreval(@current_network.blank? ? 5 : @current_network.hb_freq))
      @hardware_parts_list = HardwarePart.all
      @hardware_parts = @hardware_parts_list.inject({}) {|h, v| h[v.id] = v.name; h }
      respond_to do |format|
        format.html #new.html.erb
        format.json { render json: @ris}
      end
    end  
  end

  def show_all
    @routers = RouterInventory.order("router_inventories.created_at desc")
    @routers = Kaminari.paginate_array(@routers).page(params[:page]).per(20)
    @inventory_up_list = RouterInventory.find_up_list(@routers.map(&:mac_id), RouterInventory.get_hb_intreval(@current_network.hb_freq))
    @hardware_parts_list = HardwarePart.all
    @hardware_parts = @hardware_parts_list.inject({}) {|h, v| h[v.id] = v.name; h }
  end   

  def destroy
    @inventory = RouterInventory.where(:id => params[:id]).first
    if @inventory.destroy
      flash[:notice] = "Successfully deleted the device!"
    else
      flash[:error] = "Oops! there was a problem deleting device"
    end
    redirect_to admin_master_inventory_index_path
  end

  def change_router_presence
    if @inventory.update_attribute :presence ,params[:presence]
      render :json=> {:status=>'success',:msg=>'Successfully updated Device'}.to_json and return
    else
      render :json=> {:status=>'error',:msg=>'Oops! Problem updating Device, Please try after some time'}.to_json and return
    end
  end

  def default_wpa_password
    @result = nil
    @mac_id = params[:mac_id].try(:strip)
    if @mac_id.try(:length) == 12
      @mac_id = @mac_id.try(:upcase).gsub(/(.{2})/,'\1:').slice(0,17)
    else  
      @mac_id = @mac_id.try(:upcase)
    end 
    if @mac_id.present? && ( @mac_id =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i )
      flash[:notice] = nil
      @result = `./pwpakey_generator #{@mac_id}`
    elsif @mac_id.present?
      flash[:notice] = "Entered MAC address is not valid"
      @mac_id = nil
    end
  end

  def bulk_update
    unless params[:router].blank?
      hardware =  HardwarePart.where(:id => params[:update_HW_id]).first
      RouterInventory.change_HW_part(params[:router],hardware) unless hardware.blank?
      flash[:success] = "Hardware part has been changed to selected devices!"
    end
    redirect_to :action=> :index
  end
  private

  def set_router_inventory_group
    @inventory = RouterInventory.find_by(:mac_id => params[:master_inventory_id]) || RouterInventory.find_by(:id => params[:master_inventory_id])
    puts @inventory
    unless @inventory.present?
      flash[:notice] = "Requested device is not found in your inventory"
      redirect_to admin_master_inventory_index_path
    end
  end
end
