class SamlUserRolesController < ApplicationController
  before_action :authenticate_user!
  before_action :dashboard_topbar
  # skip_before_action :check_permission
  # load_and_authorize_resource :class => SamlUserRole
  skip_load_resource :only => [:create]
  skip_load_and_authorize_resource :only => [:create]
  

  def index
    @saml_users = current_user.organisation.saml_user_roles
    if params[:id].present?
      @saml_user = current_user.organisation.saml_user_roles.find(params[:id])
    else
      @saml_user = current_user.organisation.saml_user_roles.new
    end
    if request.format.json?
      render json: {data: {saml_roles: @saml_users.map(&:json_build)}}
    end
  end

  def create
    if params["saml_user_role"]["authorized_networks"].present?
      network_tags = {}
      network_tags["network_tags"] = params["saml_user_role"]["authorized_networks"]
      saml_user_role_params_dup = saml_user_role_params.merge(hsh_params).merge(network_tags)
      @saml_user = current_user.organisation.saml_user_roles.build(saml_user_role_params_dup)
      val = tags.values.map {|i| i.values}.flatten    
      @saml_user.tag_list.add(val)    
    else
      @saml_user = current_user.organisation.saml_user_roles.build(saml_user_role_params)
    end
    if @saml_user.save
      flash.now[:success] = "Saml Role created Successfully"
      json_data = {data: @saml_user.json_build , status: 200}
    else
      json_data = {message: "Saml Role is not created (Reason: #{@saml_user.errors.full_messages}). Please check again.", status: 422}
      flash.now[:error] = "Oops! Problem in creating Saml Role"
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to saml_user_roles_path
    end
  end

    def update
    if params["saml_user_role"]["authorized_networks"].present?
      @saml_user = current_user.organisation.saml_user_roles.find(params[:id])
      network_tags = {}
      network_tags["network_tags"] = params["saml_user_role"]["authorized_networks"]
      saml_user_params_dup = saml_user_role_params.merge(hsh_params).merge(network_tags)
      val = tags.values.map {|i| i.values}.flatten    
      @saml_user.tag_list.add(val)
      status = @saml_user.update saml_user_params_dup
    else
      status = @saml_user.update saml_user_role_params
    end
    if status
      flash[:success] = "Saml User updated Successfully"
      json_data = {data: @saml_user.json_build , status: 200}      
    else
      flash[:error] = "Oops! Problem in saml user update"
      json_data = {message: "Saml User is not updated (Reason: #{@saml_user.errors.full_messages}).", status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to saml_user_roles_path
    end
  end

  def show
    @saml_user = current_user.organisation.saml_user_roles.find(params[:id])
    if request.format.json?
      render json: {data: @saml_user.json_build, status: 200}
    end
  end

  def destroy
    @saml_user = current_user.organisation.saml_user_roles.find(params[:id])
    if @saml_user.destroy
      flash[:success] = "Saml User deleted Successfully"
      json_data = {data: {}, status: 200}
    else
      flash[:error] = "Oops! Problem in Saml user delete"
      json_data = {message: "Saml User is not deleted. Please try again." , status: 422}
    end
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to saml_user_roles_path
    end
  end

  def tags
    hsh = {}
    h = {}
    check = params["saml_user_role"]["authorized_networks"].keys
    if check.include?("full")
      tags, locations = saml_user_role_params["authorized_networks"]["full"].partition{|i| i.split(':').include?("Tag") }
      values = tags.map{|i| i.split(':').last}
      # values << locations.map{|i| i.split(':').last} if locations.present?
      h["full"] = (values.flatten.map &:to_s) 
    end
    if check.include?("limited")
      l_tags, l_locations = saml_user_role_params["authorized_networks"]["limited"].partition{|i| i.split(':').include?("Tag") }
      values = l_tags.map{|i| i.split(':').last}
      # values << l_locations.map{|i| i.split(':').last} if l_locations.present?
      h["limited"] = (values.flatten.map &:to_s)
    end   
    hsh["authorized_networks"] = h
    hsh
  end

  def hsh_params
    value = final_values
    hsh = {}
    check = params["saml_user_role"]["authorized_networks"].keys
    final_full = []
    final_limited = []
    res = []
    res1 = []
    if check.include?("full")
      full = value["authorized_networks"]["full"]
      limited = value["authorized_networks"]["limited"]
      if @l_locations.present?
        loc = @l_locations.map{|i| i.split(':').last} if @l_locations.present?
        res = full - loc
      else
        res = value["authorized_networks"]["full"]
      end
    end 
    if check.include?("limited")
      full1 = value["authorized_networks"]["full"]
      limited1 = value["authorized_networks"]["limited"]    
      if @locations.present?
        loc1 = @locations.map{|i| i.split(':').last} if @locations.present?
        res1 = limited1 - loc1
      else
        res1 = value["authorized_networks"]["limited"]   
      end
    end    
    h = {}
    h["full"] = res.flatten.uniq
    h["limited"] = res1.flatten.uniq
    hsh["authorized_networks"] = h    
    hsh
  end

  def final_values
    hsh = {}
    h = {}
    check = params["saml_user_role"]["authorized_networks"].keys
    if check.include?("full")
      @tags, @locations = saml_user_role_params["authorized_networks"]["full"].partition{|i| i.split(':').include?("Tag") }
      values = LocationNetwork.tagged_with(@tags.map{|i| i.split(':').last}, :any => true).pluck(:id)
      @f_values = values.dup
      values << @locations.map{|i| i.split(':').last} if @locations.present?
      h["full"] = (values.flatten.map &:to_s) 
    end
    if check.include?("limited")
      @l_tags, @l_locations = saml_user_role_params["authorized_networks"]["limited"].partition{|i| i.split(':').include?("Tag") }
      values = LocationNetwork.tagged_with(@l_tags.map{|i| i.split(':').last}, :any => true).pluck(:id)
      @l_values = values.dup
      values << @l_locations.map{|i| i.split(':').last} if @l_locations.present?
      h["limited"] = (values.flatten.map &:to_s)
    end   
    hsh["authorized_networks"] = h
    hsh  
  end

  private

  def saml_user_role_params
    params.require(:saml_user_role).permit(:role_name, :role_id, :network_tags, :authorized_networks => [:full => [], :limited => []])
  end

end