class PortforwardingGroupsController < ApplicationController
  before_action :set_portforwarding_group, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource :class => PortforwardingGroup, :except => [:showing_tag]
  skip_load_resource :only => [:new, :create]

  before_action :authenticate_user!
  #before_action :dashboard_topbar

  # GET /portforwarding_groups
  # GET /portforwarding_groups.json
  def index
    # @portforwarding_groups = PortforwardingGroup.all
    @existing_pfgroup = current_user.organisation.portforwarding_groups rescue []
    redirect_to new_portforwarding_group_path if @existing_pfgroup.blank?
  end

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

  # GET /portforwarding_groups/new
  def new
    @portforwarding_group = PortforwardingGroup.new
    @ar=[]
    @port_forwardings = @portforwarding_group.port_forwardings.build
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
  end

  # GET /portforwarding_groups/1/edit
  def edit
    @port_forwardings = @portforwarding_group.port_forwardings
    @ar=[]
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
    @ar = SnmpConfig.tagging_lists(@portforwarding_group.tagging_lists) unless @portforwarding_group.tagging_lists.blank?
  end

  # POST /portforwarding_groups
  # POST /portforwarding_groups.json
  def create
    @portforwarding_group = current_user.organisation.portforwarding_groups.new(portforwarding_group_params)
    respond_to do |format|
      if @portforwarding_group.save
        flash[:success] = "Successfully created Port forwarding group #{@portforwarding_group.group_name}"
        format.html { redirect_to @portforwarding_group}
        format.json { render action: 'show', status: :created, location: @portforwarding_group }
      else
        flash[:error] = "Oops! there was some problem in creating Port forwarding group"
        Rails.logger.warn "#{@portforwarding_group.errors.full_messages}"
        format.html { render action: 'new' }
        format.json { render json: @portforwarding_group.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /portforwarding_groups/1
  # PATCH/PUT /portforwarding_groups/1.json
  def update
    #@portforwarding_group.port_forwardings.delete_all
    respond_to do |format|
      if @portforwarding_group.update(portforwarding_group_params)
        flash[:success] = "Successfully updated Port forwarding group #{@portforwarding_group.group_name}"
        format.html { redirect_to @portforwarding_group}
        format.json { head :no_content }
      else
        flash[:error] = "Oops! there was some problem in updating Port forwarding group"
        Rails.logger.warn "#{@portforwarding_group.errors.full_messages}"
        format.html { render action: 'edit' }
        format.json { render json: @portforwarding_group.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /portforwarding_groups/1
  # DELETE /portforwarding_groups/1.json
  def destroy
    if @portforwarding_group.destroy
      flash[:success] = "Successfully deleted Port forwarding group #{@portforwarding_group.group_name}"
    else
      flash[:error] = "Oops! there was some problem in deleting Port forwarding group"
      Rails.logger.warn "#{@portforwarding_group.errors.full_messages}"
    end
    respond_to do |format|
      format.html { redirect_to portforwarding_groups_url }
      format.json { head :no_content }
    end
  end

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

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_portforwarding_group
      @portforwarding_group = PortforwardingGroup.where(:id => params[:id]).first
      if @portforwarding_group.blank?
        flash[:notice] = "Requested Portforwarding Group not found!"
        redirect_to portforwarding_groups_url
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def portforwarding_group_params
      params.require(:portforwarding_group).permit(:group_name,:tagging_lists,:id, associated_resources: [],port_forwardings_attributes: [:rule_name, :protocol, :external_port, :ip_address, :internal_port,:id, :_destroy])
    end
end
