class Public::FacebookAuthsController < ApplicationController  

  def facebook_connect_callback

    # Get facebook information
    auth = request.env["omniauth.auth"]
    

    
    if @mail_profile = MailProfile.where(:provider => auth['provider'], :uid => auth['uid']).first
      #redirect_to public_my_account_path
    else
      @mail_profile = MailProfile.new
      @mail_profile.provider = auth['provider']
      @mail_profile.uid = auth['uid']
      @mail_profile.facebook_token = auth['credentials']['token']
      
      @mail_profile.fb_avatar_url = auth["extra"]["raw_info"]["picture"]["data"]["url"]
      
      
      @mail_profile.skip_email = true
      if auth['info']

        # Account info
        @mail_profile.firstname = auth['info']['first_name']
        @mail_profile.name = auth['info']['last_name']
        @mail_profile.email = auth['info']['email']
      end
      
      
      @mail_profile.save
      
      #redirect_to public_my_account_path, :notice => "Votre compte facebook a bien été lié à ce compte utilisateur."
    
      
    end
    session[:facebook_mail_profile] = @mail_profile.id
    
    if request.env["omniauth.params"]["petition_id"].to_i != 0
      if request.env["omniauth.params"]["lang_id"] and LangSite.find_by_slug(request.env["omniauth.params"]["lang_id"])
        @lang = LangSite.find_by_slug(request.env["omniauth.params"]["lang_id"])
      end
      petition_lang = Petition.find(request.env["omniauth.params"]["petition_id"].to_i).lang(@lang.slug)
    
      redirect_to facebook_public_petition_signator_path(:slug => petition_lang.slug, :lang => @lang.slug )
    
    else
      
      if @forum_user = ForumUser.find_by_mail_profile_id(@mail_profile.id)
        cookies[:forum_auth_token] = @forum_user.auth_token
        redirect_to forum_forum_topics_path 
        
      else
        redirect_to facebook_forum_forum_user_path
        
        #render :inline => "ins fb"
      end
    end
      

    
  end

end
