class StaticRoutesController < ApplicationController
  include ChangeNetwork
  before_action :authenticate_user!
  before_action :dashboard_topbar
  before_action :set_static_route, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource :class => StaticRoute, :except => [:showing_tag,:change_network]
  skip_authorize_resource :only => [:new, :create]


  #before_action :dashboard_topbar

  # GET /acl_groups
  # GET /acl_groups.json
  def index
    # @acl_groups = AclGroup.all
    @existing_static_routes = @current_network.static_routes rescue []
    redirect_to new_static_route_path if @existing_static_routes.blank?
  end

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

  # GET /acl_groups/new
  def new
    @static_route = StaticRoute.new
    @ar=[]
    @static_routing_lists = @static_route.static_routing_lists.build
    @lists =  StaticRoute.aps_inventory_tags(@current_network)
    # @routers = @current_network.router_inventories
    # @mac = []
    # @routers.each do |ri|
    #   r = $redis.hgetall "AP:#{ri.mac_id}"
    #   version = r['version'].present? ? r["version"].split('.').join() : '0'
    #   @mac  << ["#{ri.name.present? ? (ri.name + " (#{ri.mac_id.last(8)})") : ri.mac_id}","AP:#{ri.id}",version,ri.mac_id]
    # end  
    # @mac

  end

  # GET /acl_groups/1/edit
  def edit
    @static_routing_lists = @static_route.static_routing_lists
    @ar=[]
    @lists =  StaticRoute.aps_inventory_tags(@current_network)
    @ar = StaticRoute.tagging_lists(@static_route.tagging_lists) unless @static_route.tagging_lists.blank?
  end

  # POST /acl_groups
  # POST /acl_groups.json
  def create
    # @acl_group = AclGroup.new(acl_group_params)
    @static_route = @current_network.static_routes.new(static_route_params)
    respond_to do |format|
      if @static_route.save
        flash[:success] = "Static Route was Successfully created."
        format.html { redirect_to @static_route }
        format.json { render action: 'show', status: :created, location: @static_route }
      else
        flash[:error] = "Oops! there was some problem in creating Static Route"
        Rails.logger.warn "#{@static_route.errors.full_messages}"
        format.html { render action: 'new' }
        format.json { render json: @static_route.errors, status: :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /acl_groups/1
  # PATCH/PUT /acl_groups/1.json
  def update
    #@acl_group.access_control_lists.delete_all
    respond_to do |format|
      if @static_route.update(static_route_params)
        flash[:success] = "Successfully updated Static Route"

        format.html { redirect_to @static_route }
        format.json { head :no_content }
      else
        flash[:error] = "Oops! there was some problem updating Static Route"
        format.html { render action: 'edit' }
        format.json { render json: @static_route.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /acl_groups/1
  # DELETE /acl_groups/1.json
  def destroy
    if @static_route.destroy
      flash[:success] = "Successfully deleted Static Route"
    else
      flash[:error] = "Oops! there was some problem deleting Static Route"
      Rails.logger.warn "#{@static_route.errors.full_messages}"
    end
    respond_to do |format|
      format.html { redirect_to static_routes_url }
      format.json { head :no_content }
    end
  end

  def save_node_data
    Monitoring.monitoring_data(params[:data])
    render :json=> {:status=>true}
  end

  def showing_tag
    @ris = RouterInventory.get_all_based_on_tag(params[:id])
  end

  def change_network
    change_user_network
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_static_route
      @static_route = StaticRoute.where(:id => params[:id]).first
       if @static_route.blank?
        flash[:notice] = "Requested Static Route not found!"
        redirect_to static_route_path
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def static_route_params
      params.require(:static_route).permit(:routing_name, :tagging_lists, :location_network_id,:id,associated_resources: [], static_routing_lists_attributes: [:network,:bitmask,:gateway,:id, :_destroy])
    end
end
