class ActivityLogController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource :class => PublicActivity::Activity
  skip_load_resource :only => [:index, :list_activity]

  def index
    @startDate = 6.days.ago
    @endDate = 0.days.ago
    @user = current_user.is_admin == "true" || current_user.role.name == "admin" ? current_user.organisation.users - [current_user] : []
    @network = current_user.networks_with_full_access
    #@log = PublicActivity::Activity.where(owner_id: current_user.id,created_at: @startDate..@endDate).order("created_at desc").page(1).per(10)
  end

  def list_activity
    @startDate = params[:start_date] || 6.days.ago
    @endDate = params[:end_date] || 0.day.ago
    page = params[:page] || 1
    network = params[:network_id] == '0' ? {} : {location_network_id: params[:network_id]}
    user = params[:user_id] == '0' ? (current_user.is_admin == "true"  || current_user.role.name == "admin") ? {} : {owner_id: current_user.id}: {owner_id: params[:user_id]}
    condition =  current_user.is_admin == true && !params[:organisation_id].blank? ? {organisation_id: params[:organisation_id],created_at: @startDate.to_datetime..@endDate.to_datetime} : {organisation_id: current_user.organisation_id,created_at: @startDate.to_datetime..@endDate.to_datetime}
    params[:activity] = params[:activity].split(',') if params[:activity] != "null"
    activity = (params[:activity_type].present? && !params[:activity].include?('null') && !params[:activity].include?('0')) ? {trackable_type: params[:activity].map {|activity| activity.split('_').map(&:capitalize).join('')}} : {}#params[:activity].split('_').map(&:capitalize).join('')} : {}
    activity_type = !params[:activity].include?('0') && params[:activity_type] != '0' ? {key: [params[:activity],params[:activity_type]].join('.')} : {}
    activity_type = "activities.key LIKE '%.#{params[:activity_type]}%'" unless params[:activity_type] == '0'
    unless activity.empty?
      activity[:trackable_type] += ["AlertRule"] if activity[:trackable_type].include?("Alert")
      activity[:trackable_type] += ["AccessControlList"] if activity[:trackable_type].include?("AclGroup")
      activity[:trackable_type] += ["SwitchPortConfiguration"] if activity[:trackable_type].include?("SwitchConfiguration")
      activity[:trackable_type] += ["RadioSetting"] if activity[:trackable_type].include?("RadioProfile")
    end
    @log = PublicActivity::Activity.where(condition.merge(network).merge(user).merge(activity)).where(activity_type).order("created_at desc").page(page).per(10)
    render :layout => false
  end

   def change_network
    network = current_user.location_networks.find_by_id(params[:id])
    unless network.present?
      network = current_user.location_networks.first
      flash[:notice] = "Your network has been changed to #{network.network_name}"
    end
    current_user.current_network = network.id
    @current_network = current_user.current_network
    logger.info("Current Network in show <<<<<<#{@current_network.id}") unless @current_network.blank?
    redirect_to "/home/#{params[:id]}"
  end
end
