class FirewallConfigController < ApplicationController
  include ChangeNetwork
  before_action :authenticate_user!
  before_action :dashboard_topbar
  before_action :set_firewall_config, only: [:edit, :update, :destroy]
  load_and_authorize_resource :class => FirewallConfig
  skip_load_resource :only => [:new, :create,:change_network]
  skip_authorize_resource :only => [:change_network]
  check_policy_for :update, with: FirewallConfig, id_param: :id

  #before_action :dashboard_topbar

  # GET /acl_groups
  # GET /acl_groups.json
  def index
    @existing_firewallconfigs = @current_network.firewall_configs rescue []
  end

  # GET /acl_groups/new
  def new
    @firewall_config = FirewallConfig.new
    @client_communications = @firewall_config.client_communications.build
    @ssid={}
    @ssid["SSId"] = @current_network.network_ssids.pluck(:ssid_name,:id)
    @ssid["VLAN"] = @current_network.get_configured_vlans.map {|s| [s.v_lan_id, s.uniq_identifier]}#.present? ? [["VLAN",'-1'],['Any','0']] : [['Any','0']]
    @ssid["Other"] = [['Any','0']]
  end

  # GET /firewall_config/1/edit
  def edit
    @ssid={}
    @ssid["SSId"] = @current_network.network_ssids.pluck(:ssid_name,:id)

    @ssid["VLAN"] = @current_network.get_configured_vlans.map {|s| [s.v_lan_id, s.uniq_identifier]}#.present? ? [["VLAN",'-1'],['Any','0']] : [['Any','0']]
    @ssid["Other"] = [['Any','0']]
  end
  # POST /acl_groups
  # POST /acl_groups.json
  def create
    @firewall_config = @current_network.firewall_configs.build(firewall_config_params)
    respond_to do |format|
      if @firewall_config.save
        flash[:success] = "Firewall Configuration was Successfully created."
        format.html { redirect_to firewall_config_index_url }
        format.json { render action: 'show', status: :created, location: @firewall_config }
      else
        flash[:error] = "Oops! there was some problem in creating Firewall Configuration"
        Rails.logger.warn "#{@firewall_config.errors.full_messages}"
        format.html { render action: 'new' }
        format.json { render json: @firewall_config.errors, status: :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /acl_groups/1
  # PATCH/PUT /acl_groups/1.json
  def update
    respond_to do |format|
      if @firewall_config.update(firewall_config_params)
        flash[:success] = "Successfully updated Firewall Configuration"
        format.html { redirect_to firewall_config_index_url }
        format.json { head :no_content }
      else
        flash[:error] = "Oops! there was some problem updating Firewall Configuration"
        format.html { render action: 'edit' }
        format.json { render json: @firewall_config.errors, status: :unprocessable_entity }
      end
    end
  end

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

  def change_network
    change_user_network
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_firewall_config
      @firewall_config = FirewallConfig.where(:id => params[:id]).first
       if @firewall_config.blank?
        flash[:notice] = "Requested Firewall Configuration not found!"
        redirect_to firewall_config_index_path
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def firewall_config_params
      params.require(:firewall_config).permit(:name,associated_resources: [],:client_communications_attributes => [:source_interface,:destination_interface,:policy,:unidir,:id, :priority,:_destroy])
    end
end
