/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.com.cmm;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**  
 * @Class Name : FacebookAPI.java
 * @Description : 페이스북 API Class
 * @Modification Information  
 * @
 * @  수정일      수정자              수정내용
 * @ ---------   ---------   -------------------------------
 * @ 2015.04.06          최초생성
 * 
 * @author (주)드림웨어
 * @version 1.0
 * @see
 * 
 *  
 */

public class FacebookAPI{

	private static FacebookAPI facebookAPI = null;

	private static String CLIENT_ID = "846197678724759";

	private static String CLIENT_SECRET="75180f5cd6b38cd21ba7f756566e30e5";

	//private static String REDIRECT_URI="http://YOUR_REDIRECT_URI";

	private String getAccessToken()
	{
		URL url;
		InputStream is = null;
		try {
			url = new URL("https://graph.facebook.com/oauth/access_token"
					+"?client_id="+CLIENT_ID
					+"&client_secret=" +CLIENT_SECRET
					+"&grant_type=client_credentials");
			HttpURLConnection request = (HttpURLConnection) url.openConnection();
			is = request.getInputStream();
		} catch (MalformedURLException e) {

			// TODO Auto-generated catch block

			System.out.println("페이스북에러");

		} catch (IOException e) {

			// TODO Auto-generated catch block

			System.out.println("페이스북에러");

		}

		int result;

		StringBuffer buffer = new StringBuffer();
		Scanner scan = new Scanner(is, "UTF-8");
		while(scan.hasNextLine())
		{
			buffer.append(scan.nextLine());

		}

		//String access_token = buffer.substring(13); 
		JSONObject  jsonObj  = (JSONObject) JSONValue.parse(buffer.toString());
		String strAccess_token  = jsonObj.get("access_token").toString() ;
			
		//System.out.println("access_token : "+access_token);
		//return access_token;
		return strAccess_token ;
		

	}

	public String getPosts()
	{

		URL url;
		InputStream is = null;
		String access_token = getAccessToken();
		try {

			/*url = new URL("https://graph.facebook.com/v2.3/457530414275899/posts"
					+"?access_token="+access_token
					+"&locale=ko_KR"
					+"&limit=10");*/
			
			access_token = "EAAMBnMAM1pcBAPZBlJUe2lP0CkXAbNoAyZBbZAjhZCZBVKI04p6ZA3Bu6z538bvh81IHM2HvZAP77DksglvH3SwJ8Sb0Rw3KTggGI21KYVaiNzshnRY8UOKUX8HpfnhEy1Gyu6ZB1YuDxvQq22KTiZAVpbXycH6x29tnJ6JIGQ6a7gKZAZBbBuvzwHd" ;
			String fields =  "type,picture,from,caption,message,description,story,name,link,permalink_url" ;
			url = new URL("https://graph.facebook.com/v2.6/457530414275899/feed"
					+"?access_token="+access_token
					+"&fields="+fields + "&limit=10" );
					
			HttpURLConnection request = (HttpURLConnection) url.openConnection();
			is = request.getInputStream();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("페이스북에러");
		}

		StringBuffer buffer = new StringBuffer();
		Scanner scan = new Scanner(is, "UTF-8");

		while(scan.hasNextLine())
		{
			buffer.append(scan.nextLine());
		}
		return buffer.toString();
	}
	
	public String getPhoto()
	{

		URL url;
		InputStream is = null;
		try {

			url = new URL("https://graph.facebook.com/v2.3/457530414275899/picture?redirect=false");
			HttpURLConnection request = (HttpURLConnection) url.openConnection();
			is = request.getInputStream();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("페이스북에러");
		}

		StringBuffer buffer = new StringBuffer();
		Scanner scan = new Scanner(is, "UTF-8");
		while(scan.hasNextLine())
		{
			buffer.append(scan.nextLine());
		}
		return buffer.toString();
	}


	

	public static FacebookAPI getInstance()

	{

		if(facebookAPI==null)

		{

			facebookAPI = new FacebookAPI();

		}

		return facebookAPI;

	}

	

	private FacebookAPI() {}

}
