class IntegrationTypesController < ApplicationController
  include ChangeNetwork
  before_action :authenticate_user!
  skip_load_resource :only => [:create, :change_network]

  def index
    if params[:id].present?
      @integration_type = IntegrationType.where(id: params[:id]).first
      unless @integration_type.present?
        flash[:notice] = 'Integration type not found! Please check.'
        @integration_type = IntegrationType.new
      else
        @zones = []#get_zones @integration_type
      end
    else
      @integration_type = IntegrationType.new
    end

    @all_integration_type = current_user.organisation.integration_types rescue []
  end

  def show
  end

  def new
    @alert = current_user.organisation.integration_types.new
  end

  def create
    @integration_type = IntegrationType.where(id: params[:id]).first
    network = current_user.current_network
    details = {"username" => params[:username], "password" => params[:password], "sz_host" => params[:sz_host], "api_version" => "v11_0"}
    int_type = current_user.organisation.integration_types.new(vendor_name: params[:integration_type][:vendor_name], details: details, level: 'organisation')
    if int_type.save
      response = update_service_ticket(int_type)
      if response["serviceTicket"].present?
        flash[:success] = 'Successfully created Integration Type'
        redirect_to integration_types_path(:id => int_type.id)
      else
        flash[:notice] = "#{response["message"]}"
        redirect_to integration_types_path
      end
    else
      flash[:notice] = 'Oops!, Problem in creating Integration Type, Please try after some time'
    end
  end

  def update_service_ticket(int_type)
    Ruckus::BaseApi.service_ticket(int_type)
  end

  def get_zones int_type
    response = Ruckus::SyncService.new(int_type).get_zones_list
    zones = {"zones" => response.map {|zn| [zn["name"], zn["id"]]}}
    zones
  end

  def update
    @integration_type = IntegrationType.where(id: params[:id]).first
    details = {"username" => params[:username], "password" => params[:password], "sz_host" => params[:sz_host], "zone" => params[:zone]}
    if @integration_type.update(vendor_name: params[:integration_type][:vendor_name], details: details)
      flash[:success] = "Successfully updated integration type "
    else
      flash[:error] = "Oops! there was some in problem updating integration configuration"
    end
    redirect_to :action=>:index
  end

  def destroy
    @integration_type = IntegrationType.where(id: params[:id]).first
    if @integration_type.destroy
      flash[:success] = "Successfully deleted integration type #{@integration_type.vendor_name}"
    else
      flash[:error] = "Oops! there was some problem in deleting integration type"
      Rails.logger.warn "#{@integration_type.errors.full_messages}"
    end
    redirect_to :action=>:index
  end

  def get_domains
    integration_type = IntegrationType.where(id: params[:int_type]).first
    response = integration_type.details["domains"].map{|k, v| {"id" => k, "name" => v["name"]}}
    render :json => response
  end

  def change_network
    change_user_network
  end

end
