require "net/http"
require "uri"
require 'json'

class FacebookController< ApplicationController

	@@facebook_server = "http://fb.tp.rulingcom.com"

	def register_fb
		user = User.find(params[:user])
		if !user.facebook
			user.build_facebook(fb_identifier: params[:identifier],connected: true)
			user.save!
		end
    	render :json=>{"success"=>true,"user"=>user.facebook.fb_identifier}.to_json
    	# respond_to do |format|
    	# 	format.js
    	# end
  	end

  	def get_friends
  		fb = current_user.facebook rescue nil
  		if fb
	  		@url = "#{@@facebook_server}/get_friends?identifier=" + current_user.facebook.fb_identifier
		    uri = URI.parse(@url)
		    http = Net::HTTP.new(uri.host, uri.port)
		    request = Net::HTTP::Get.new(uri.request_uri)
		    response = http.request(request)
		    friends = response.body
		    friends = JSON.parse(friends)
		    output = Array.new
		    friends.each do |friend|
		    	output << {"name"=>friend['name'],"identifier"=>friend['identifier']}
		    end
	  		render :json=>{"success"=>true,"count"=>friends.count,"friends"=>output}.to_json
	  	else
	  		render :json=>{"success"=>false,"message"=>"User is not connected to facebook."}.to_json
	  	end
  	end

  	def get_feed
  		fb = current_user.facebook rescue nil
  		if fb
	  		@url = "#{@@facebook_server}/get_feed?identifier=" + current_user.facebook.fb_identifier
		    uri = URI.parse(@url)
		    http = Net::HTTP.new(uri.host, uri.port)
		    request = Net::HTTP::Get.new(uri.request_uri)
		    response = http.request(request)
		    feed = response.body
		    feed = JSON.parse(feed)
		    output = Array.new
		    feed.each do |f|
		    	output << {"story"=>f['raw_attributes']['story']}
		    end
	  		render :json=>{"success"=>true,"count"=>feed.count,"feed"=>output}.to_json
	  	else
	  		render :json=>{"success"=>false,"message"=>"User is not connected to facebook."}.to_json
	  	end
  	end

  	def disconnect
  		user_fb = current_user.facebook
  		if user_fb
  			user_fb.destroy
  		end
  		render :json=>{"success"=>true}.to_json
  	end

  	def profile_import
  		temp_user = User.find(params[:user_id])
  		fb = temp_user.facebook rescue nil
  		if fb
	  		@url = "#{@@facebook_server}/profile_import?identifier=" + fb.fb_identifier
		    uri = URI.parse(@url)
		    http = Net::HTTP.new(uri.host, uri.port)
		    request = Net::HTTP::Get.new(uri.request_uri)
		    response = http.request(request)
		    friends = response.body
		    data = JSON.parse(friends)
		    if data["success"]
		    	user = JSON.parse(data["user"])
		    	u = User.find(temp_user.id)
		    	firstname = {}
		    	lastname = {}
		    	@site_valid_locales.each do |locale|
		    		firstname[locale] = user["first_name"]
		    		lastname[locale] = user["last_name"]
		    	end
		    	u.first_name_translations = firstname
		    	u.last_name_translations = lastname
		    	u.office_tel = user["phone"]
		    	u.sex = user["gender"] || "unknown"
		    	u.remote_avatar_url = user["picture"] if user["picture"]
		    	if u.save
	  				render :json=>{"success"=>true,"user"=>user}.to_json
	  			else
	  				render :json=>{"success"=>false,"message"=>"There was an error imporing data."}.to_json
	  			end

	  		else
	  			render :json=>{"success"=>false,"message"=>data["message"]}.to_json
	  		end

	  	else
	  		render :json=>{"success"=>false,"message"=>"User is not connected to facebook."}.to_json
	  	end
  	end

  	def server_redirect
  		@url = "#{@@facebook_server}/login?callback=#{request.protocol}#{request.host_with_port}/facebook/register_fb?user="+params[:user_id] rescue nil
  		if !@url.nil?
  			redirect_to @url
  		else
  			render :layout=> "devise"
  		end
  	end
end