class VisitorCreateWorker
	include Sidekiq::Worker

	def segregate_the_horstdata(data)
    client_data={}; visitors_data=[]; client=""
    if data.is_a? Hash
      case 
        when data.keys.include?('probing')
         visitors_data =  data["probing"]
         data.delete("probing")
         client_data =  data
         client =  'meraki_1.0'
        when data.keys.include?('data')
          visitors_data =  data["data"]["observations"]
          data["data"].delete("observations")
          client_data =  data
          client = 'meraki_2.0'
        # when data.keys.include?("apMac")
        when data.keys.include?("PRESENCE")
          data = data["PRESENCE"]
          visitors_data =  data["CLIENTS"]
          data.delete("CLIENTS")
          client_data =  data
          client = 'wavespot'
      end
    elsif data.is_a? Array
      case 
        when data[0].keys.include?('PROTYPE')
          visitors_data =  data
          client = 'wavespot'
      end
    end
    return {:client_data=>client_data,:visitors_data=>visitors_data, :client=>client}
  end

    def perform(horst)
    	horst = JSON.parse(horst) if horst.is_a? String 
=begin    	horst = JSON.parse(horst) if horst.is_a? String
		mac_id = []
		horst_data = segregate_the_horstdata(horst)
		client = horst_data[:client]
		macid = get_macid(horst_data).upcase rescue return
		client = APPLICATION['defaults'][client]
		rl = RouterLocation.where(:mac_id => macid).last
		if rl.blank?
		 	rl = RouterLocation.create({:mac_id=> macid, :client_data=>horst_data[:client_data], :client=>horst_data[:client]})
        else
			rl.update_attributes({:mac_id=> macid, :client_data=>horst_data[:client_data], :client=>horst_data[:client] })
		end
		
		(horst_data[:visitors_data] || []).each do |y|
			next if y.blank?
			next if(mac_id.blank? ? false : mac_id.include?(macid) || y[client["client_mac"]].blank?)
			next if y[client['last_seen']].blank?
			ar_child = []
			(horst_data[:visitors_data] || []).each do |x|
				x[client['last_seen']] = Time.parse(x[client['last_seen']]) if (x[client['last_seen']].is_a? String) if "meraki_1.0".eql?(horst_data[:client])
				tm = String.eql?(x[client['last_seen']].class) ? (x[client['last_seen']].to_i == 0 ? x[client['last_seen']] : eval(x[client['last_seen']].to_i.to_s)) : x[client['last_seen']]
				ar_child.push({"timestamp" =>  tm,
					"created_at" => Time.zone.now,
					"rl_mac_id" => macid,
					"vd_mac_id" => x[client["client_mac"]],
					"status" =>  x[client['status']],
					"location" =>  x[client['location']],
					"ssid" =>  x[client['ssid']],
					"manufacturer" =>  x[client['manufacturer']],
					"os" =>  x[client['os']],
					"ipv6" =>  x[client['ipv6']],
					"ipv4" =>  x[client['ipv4']],
					"rss" =>  x[client['rss']], 
					"protype" =>  x[client['protype']], 
					"signal" =>  x[client['signal']], 
					"noise" =>  x[client['noise']], 
					"sgnl_noise_ratio" =>  x[client['sgnl_noise_ratio']], 
					"rate" =>  x[client['rate']], 
					"tsf" =>  x[client['tsf']], 
					"mode" =>  x[client['mode']], 
					"channel" =>  x[client['channel']], 
					"seentime" =>  tm, 
					"router_locations_id"=>rl.id }) if(x[client["client_mac"]] == y[client["client_mac"]])
			end
			mac_id.push(y[client["client_mac"]])
			vd = rl.visitor_details.where(:device_id => y[client["client_mac"]])
			vd = rl.visitor_details.create({"device_id" => y[client["client_mac"]]}).to_a if vd.blank?
			vd.last.visitor_timings.create(ar_child)
			VisitorTimeHopeWorker.perform_async(vd[0].id,macid)
		end
=end
	end

	def get_macid(horst_data)
		case horst_data[:client]
		when "meraki_2.0"; return horst_data[:client_data]["data"]["apMac"];
		when "meraki_1.0"; return horst_data[:visitors_data][0]["ap_mac"];
		when "wavespot"; return horst_data[:client_data]["apMac"];
		end
	end
end