package kr.method.papaya;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kakao.KakaoLink;
import com.kakao.KakaoParameterException;
import com.kakao.KakaoTalkLinkMessageBuilder;

public class KakaoLinkPlugin extends CordovaPlugin {
	private final String LINK = "link";
	KakaoLink kakaoLink;

	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		//서비스 시작
		if (action.equals(LINK)) {
			final JSONObject options = args.getJSONObject(0);
			this.link(options);
		}
		
		return false;
	}
	
	/**
	 * 카카오 링크 열기
	 */
	private void link(JSONObject options){
		LOG.d("KakaoLinkPlugin", "link");
		String message;
		String image;
		String url;
		String label;
		int width;
		int height;
		try {
			kakaoLink = KakaoLink.getKakaoLink(cordova.getActivity().getApplicationContext());
			final KakaoTalkLinkMessageBuilder kakaoTalkLinkMessageBuilder = kakaoLink.createKakaoTalkLinkMessageBuilder();

			try{
				message = options.getString("message");
				kakaoTalkLinkMessageBuilder.addText(message);
			}catch(JSONException e){
				
			}
			
			try{
				image = options.getString("image");
				width = options.getInt("width");
				height = options.getInt("height");
				kakaoTalkLinkMessageBuilder.addImage(image, width, height);
			}catch(JSONException e){
				
			}
			
			try{
				url = options.getString("url");
			}catch(JSONException e){
				url = "";
			}
			
			try{
				label = options.getString("label");
			}catch(JSONException e){
				label = "";
			}
			
			if(!url.equals("") && !label.equals("")){
				kakaoTalkLinkMessageBuilder.addWebButton(label,url);
			}else if(url.equals("") && !label.equals("")){
				kakaoTalkLinkMessageBuilder.addWebButton(label);
			}
			
			
		} catch (KakaoParameterException e) {
			e.printStackTrace();
		}
	}
}

@@ -1148,23 +1148,21 @@
 String(%22
-message
+label
 %22);%0A%09%09%09%09
@@ -1516,17 +1516,26 @@
 String(%22
-u
+webButtonU
 rl%22);%0A%09%09
@@ -1618,29 +1618,37 @@
 .getString(%22
-label
+webButtonText
 %22);%0A%09%09%09%7Dcatc
@@ -1676,24 +1676,30 @@
 %09%09%09label = %22
+%EC%95%B1%EC%9C%BC%EB%A1%9C %EC%9D%B4%EB%8F%99
 %22;%0A%09%09%09%7D%0A%09%09%09%0A

Update KakaoLinkPlugin.java