class AclGroupsController < ApplicationController

  before_action :authenticate_user!
  before_action :set_acl_group, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource :class => AclGroup, :except => [:showing_tag]
  # check_policy_for :create, :update, :destroy, with: AclGroup, id_param: :id
  skip_load_resource :only => [:new, :create]

  #before_action :dashboard_topbar

  # GET /acl_groups
  # GET /acl_groups.json
  def index
    # @acl_groups = AclGroup.all
    @existing_aclgroup = current_user.organisation.acl_groups rescue []
    if request.format.json?
      render json: {data: {acls: @existing_aclgroup.map(&:json_build)}}
    else
      redirect_to new_acl_group_path if @existing_aclgroup.blank?
    end
  end

  # GET /acl_groups/1
  # GET /acl_groups/1.json
  def show
    if request.format.json?
      render json: {data: {acl_group: @acl_group.json_build}}
    end
  end

  # GET /acl_groups/new
  def new
    @acl_group = AclGroup.new
    @ar=[]
    @access_control_lists = [@acl_group.access_control_lists.build]
    @access_control_list_layer_nines = [@acl_group.access_control_list_layer_nines.build]
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
  end

  # GET /acl_groups/1/edit
  def edit
    @access_control_lists = @acl_group.access_control_lists
    @access_control_lists = [@acl_group.access_control_lists.build] if @access_control_lists.blank?

    @access_control_list_layer_nines = @acl_group.access_control_list_layer_nines
    @access_control_list_layer_nines = [@acl_group.access_control_list_layer_nines.build] if @access_control_list_layer_nines.blank?
    
    @ar=[]
    @lists =  AclGroup.aps_inventory_tags(current_user.organisation)
    @ar = SnmpConfig.tagging_lists(@acl_group.tagging_lists) unless @acl_group.tagging_lists.blank?
  end

  # POST /acl_groups
  # POST /acl_groups.json
  def create
    # @acl_group = AclGroup.new(acl_group_params)
    @acl_group = current_user.organisation.acl_groups.new(acl_group_params)
    if @acl_group.save
      flash[:success] = "Acl Group was Successfully created."
      json_data = {data: @acl_group.json_build , status: 200}
    else
      flash[:error] = "Oops! there was some problem in creating Acl group"
      json_data = {message: "Acl Group is not created. Please try again." , status: 422}
      Rails.logger.warn "#{@acl_group.errors.full_messages}"
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to  @acl_group
    end
  end


  # PATCH/PUT /acl_groups/1
  # PATCH/PUT /acl_groups/1.json
  def update
    #@acl_group.access_control_lists.delete_all
    if @acl_group.update(acl_group_params)
      flash[:success] = "Successfully updated Acl group"
      json_data = {data: @acl_group.json_build , status: 200}        
    else
      flash[:error] = "Oops! there was some problem updating Acl group"
      json_data = {message: "Acl Group is not updated (Reason: #{@acl_group.errors.full_messages}).", status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to @acl_group
    end
  end

  # DELETE /acl_groups/1
  # DELETE /acl_groups/1.json
  def destroy
    if @acl_group.destroy
      flash[:success] = "Successfully deleted Acl group"
      json_data = {data: {}, status: 200}
    else
      flash[:error] = "Oops! there was some problem in deleting Acl group"
      json_data = {message: "Acl Group is not deleted. Please try again.", status: 422}
      Rails.logger.warn "#{@acl_group.errors.full_messages}"
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to acl_groups_url
    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 clone_acl
    acl = AclGroup.where(:id => params["id"]).first
    new_config = acl.deep_dup
    new_config.group_name = new_config.group_name + " (copy)"
    if new_config.save
      acl.access_control_lists.each do |ac_list|
        new_acl_config = ac_list.deep_dup
        new_acl_config.acl_group_id = new_config.id
        new_acl_config.save
      end
      new_acl_group = AclGroup.find_by_id(new_config.id)
      flash[:success] = 'Acl Group cloned Successfully'
      json_data = {data: new_acl_group.json_build , status: 200}
    else
      flash[:notice] = 'Oops, Problem in cloning Acl Group, Please try after some time'
      json_data = {message: "Oops, Problem in cloning Acl Group, Please try after some time", status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to acl_groups_path
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_acl_group
    @acl_group = @current_user.organisation.acl_groups.where(:id => params[:id]).first
    if @acl_group.blank?
      if request.format.json?
        render json: {message: "Requested ACL Group is not found", status: 404}, status: 404
      else
        flash[:notice] = "Requested AclGroup not found!"
        redirect_to acl_group_path
      end
    end
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def acl_group_params
    params.require(:acl_group).permit(:group_name, :tagging_lists, :organisation_id, :router_inventory_id, :location_network_id,:id,:logging,associated_resources: [], access_control_lists_attributes: [:policy,:protocol,:src_ip,:src_mac,:scr_bitmask,:src_port,:dst_port,:dst_ip,:dst_bitmask,:id, :_destroy, :src_uplink_type, :dst_uplink_type, :is_enabled, :log, :priority], access_control_list_layer_nines_attributes: [:policy, :policy_ly7, :protocol,:src_ip, :application, :host_iprange, :port, :application_value, :src_domainly7, :src_bitmaskly7, :src_uplink_type, :src_uplink_typely7, :src_iply7, :id, :_destroy])
  end

end
