class SnmpConfigsController < ApplicationController
  include ChangeNetwork
  before_action :authenticate_user!
  before_action :dashboard_topbar

  before_action :set_snmp_config, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource :class => SnmpConfig
  skip_load_resource :only => [:new, :create,:showing_tag]
  skip_load_resource :only => [:change_network]

  # GET /snmp_configs
  # GET /snmp_configs.json
  def index
    @ar =[]
    @snmp_config = params[:id].blank? ? SnmpConfig.new : SnmpConfig.find(params[:id])
    @snmp_configs = current_user.organisation.snmp_configs rescue []
    @ar = SnmpConfig.tagging_lists(@snmp_config.tagging_lists)  unless params[:id].blank?
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
  end

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

  def showing_tag
    case params[:type]
    when "network";@network=LocationNetwork.where(id: params[:id].to_i)
    when "tag"; @ris = RouterInventory.get_all_based_on_tag(params[:id])
    else; @ris = RouterInventory.where(id: params[:id].to_i)
    end
  end

  # GET /snmp_configs/new
  def new
    @snmp_config = SnmpConfig.new
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
  end

  # GET /snmp_configs/1/edit
  def edit

  end

  # POST /snmp_configs
  # POST /snmp_configs.json
  def create
    snmp_configs = current_user.organisation.snmp_configs.create(snmp_config_params)
    unless snmp_configs.blank?
      flash[:success] = "Successfully created SNMP configuration"
    else
      flash[:error] = "Oops! there was some problem in creating SNMP configuration"
      Rails.logger.warn "#{snmp_configs.errors.full_messages}"
    end
    redirect_to :action=>:index
  end

  # PATCH/PUT /snmp_configs/1
  # PATCH/PUT /snmp_configs/1.json
  def update
    if @snmp_config.update(snmp_config_params)
      flash[:success] = "Successfully updated SNMP configuration"
    else
      flash[:error] = "Oops! there was some problem in updating SNMP configuration"
      Rails.logger.warn "#{@snmp_config.errors.full_messages}"
    end
    redirect_to :action=>:index
  end

  # DELETE /snmp_configs/1
  # DELETE /snmp_configs/1.json
  def destroy
    if @snmp_config.destroy
      flash[:success] = "Successfully deleted SNMP configuration"
    else
      flash[:error] = "Oops! there was some problem in deleting SNMP configuration"
      Rails.logger.warn "#{@snmp_config.errors.full_messages}"
    end
    redirect_to :action=>:index
  end

  def change_network
    change_user_network
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_snmp_config
      @snmp_config = SnmpConfig.where(:id => params[:id]).first
      if @snmp_config.blank?
        flash[:notice] = "Requested SNMP configuration not found!"
        redirect_to :action=>:index
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def snmp_config_params
      params.require(:snmp_config).permit(:organisation_id, :server_ip, :server_port, :server_community, :ro_community, :rw_community,:tagging_lists, associated_resources: [])
    end
end
