package performa.form;

import javax.servlet.http.*;
import oneit.appservices.config.*;
import oneit.security.oauth.form.BaseOAuthLoginFP;
import oneit.servlets.forms.*;
import oneit.utils.*;


public class FacebookOAuthLoginFP extends BaseOAuthLoginFP
{
    public static final String CONFIG_FACEBOOK_PROFILE_PARAMS_KEY   = "facebook.profile.params";
    
    public static final String FACEBOOK_APPID_ATTRIB_NAME           = "facebook.appId";
    public static final String FACEBOOK_APP_OATH_URL                = "http://www.facebook.com/dialog/oauth";
    public static final String FACEBOOK_PROFILE_API_URL             = "https://graph.facebook.com/me";
    public static final String FACEBOOK_ACCSESS_TOKEN_URL           = "https://graph.facebook.com/oauth/access_token";
    public static final String FACEBOOK_APP_SECRET_ATTR_NAME        = "facebook.appSecret";
    public static final String FACEBOOK_PROFILE_REQ_PARAMS          = ConfigMgr.getKeyfileString(CONFIG_FACEBOOK_PROFILE_PARAMS_KEY, "id,first_name,last_name,email");
    
    @Override
    public String getOAuthLoginURL(SubmissionDetails submission, String token)
    {
        String appId = ConfigMgr.getKeyfileString(FACEBOOK_APPID_ATTRIB_NAME);
        
        String callbackURL = getCallbackURL(submission.getRequest());
        
        return FACEBOOK_APP_OATH_URL
                + HTTPRequestDetails.getParamStringURL(CollectionUtils.mapEntry("client_id", appId).mapEntry("redirect_uri", callbackURL)
                .mapEntry("scope", "public_profile,email").mapEntry("state", token).toMap());
    }
    
    public static String getCallbackURL(HttpServletRequest  request)
    {
        String              urlFirstBit = request.getScheme () + "://" + request.getServerName ();

        System.out.println("urlFirstBit : " + urlFirstBit);
        System.out.println("request.getContextPath() : " + request.getContextPath());
        return HTTPRequestDetails.getFullURL(urlFirstBit + request.getContextPath() + "/facebookCallback", new String[0]);
    }
}