class SwitchPortConfigurationsController < ApplicationController
  before_action :authenticate_user!
  before_action :dashboard_topbar, if: Proc.new { |c| !c.request.format.json? }
  before_action :find_switch_configuration
  before_action :find_switch_port_configuration, only: [:update, :destroy, :show]
  check_policy_for :create, :update, :destroy, with: SwitchConfiguration, id_param: :switch_configuration_id

  # skip_load_resource :only => [:change_network]

  def index
    @existing_switch_port_configs = @switch_configuration.switch_port_configurations rescue []
    if request.format.json?
      render json: {data: {switch_port_configurations: @existing_switch_port_configs.map(&:json_build)}}
    end
  end

  def new
  end

  def create
    @switch_port_configuration = @switch_configuration.switch_port_configurations.new(switch_port_configuration_params)
    if @switch_port_configuration.save
      flash[:success] = "Switch Port Configuration Successfully created."
      json_data = {data: @switch_port_configuration.json_build , status: 200}
    else
      json_data = {message: "Switch Port Configuration is not created. Please check again.", status: 422}
      flash[:error] = "Oops! there was some problem in creating Switch Port Configuration"
      Rails.logger.warn "#{@switch_port_configuration.errors.full_messages}"
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to edit_switch_configuration_path(@switch_configuration)
    end
  end

  def edit
  end

  def update
    if @switch_port_configuration.update(switch_port_configuration_params)
      flash[:success] = "Successfully updated Switch Port Configuration"
      json_data = {data: @switch_port_configuration.json_build , status: 200}
    else
      flash[:error] = "Oops! there was some problem updating Switch Port Configuration"
      json_data = {message: "Switch Port Configuration is not updated (Reason: #{@switch_port_configuration.errors.full_messages}).", status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to edit_switch_configuration_path @switch_configuration
    end
  end

  def destroy
    if @switch_port_configuration.destroy
      flash[:success] = "Successfully deleted Switch Port Configuration"
      json_data = {data: {}, status: 200}
    else
      flash[:error] = "Oops! there was some problem in deleting Switch Port Configuration"
      Rails.logger.warn "#{@switch_port_configuration.errors.full_messages}"
      json_data = {message: "Switch Port Configuration is not deleted. Please try again.", status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to switch_port_configurations_url
    end
  end

  def show
    @switch_port_configuration = @switch_configuration.switch_port_configurations.where(id: params[:id]).last

    if @switch_port_configuration.present?
      render json: {switch_port_detail: @switch_port_configuration.json_build}
    else
      render json: {switch_port_detail: {}, msg: "Requested port details not found"}, status: 404
    end
  end

  private

  def switch_port_configuration_params
    params.require(:switch_port_configuration).permit(:id, :port_number, :name, :port_status, :link_negotiation, :speed, :power, :poe, :vlan, :port_isolation, :switch_configuration_id, :rstp, :native_vlan, :port_type, :acl_mac, :mac_filter_policy, :acl_mac_list)
  end

  def find_switch_configuration
    @switch_configuration = SwitchConfiguration.where(id: params[:switch_configuration_id]).last
    @switch_configuration = SwitchConfiguration.where(id: params[:switch_port_configuration][:switch_configuration_id]).last if @switch_configuration.blank? && params[:switch_port_configuration].present?
    if @switch_configuration.blank?
      flash[:notice] = "Requested switch configuration is not found."
      respond_to do |format|
        format.html { redirect_to switch_configurations_url }
        format.json { {status: false, message: "Requested switch configuraion is not found. please check and try again."} }
      end
    end
  end

  def find_switch_port_configuration
    @switch_port_configuration = @switch_configuration.switch_port_configurations.where(id: params[:id]).last
    @switch_port_configuration = SwitchConfiguration.where(id: params[:switch_port_configuration][:id]).last unless @switch_port_configuration.present?
    if @switch_port_configuration.blank?
      flash[:notice] = "Requested switch port configuration is not found."
      respond_to do |format|
        format.html { redirect_to switch_configuration_url(@switch_configuration) }
        format.json { {status: false, message: "Requested switch port configuraion is not found. please check and try again."} }
      end
    end
  end  

end