package com.dbs.mplus.fatima.view.web;

import com.dbs.mplus.fatima.framework.util.MPlusLogUtil;
import com.dbs.mplus.fatima.processor.MessageProcessor;

import android.app.Activity;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

/**
 * 서버로부터 메시지를 수신하여(실체는 javaScript로부터 넘어오는 json문자열) 처리기를 돌린다.
 * Created by aple on 2015. 7. 19..
 */
public class MPlusWebProcessor {
    private final Activity activity;
    private final WebView webView; 
    
    /**
     * 생성자
     * @param activity
     * @param webView  javaScript 실행을 위함 
     */
    public MPlusWebProcessor(Activity activity, WebView webView) {
        this.activity = activity;
        this.webView = webView;         		
    }

    /**
     * 서버로부터 json 메시지를 받아서 처리
     * @param jsonStr
     */
    @JavascriptInterface
    public void toNative(final String jsonStr) {
        MPlusLogUtil.DEBUG(this.getClass(), jsonStr);

        // 처리기 만들고
        MessageProcessor m = new MessageProcessor(this.activity, this.webView, jsonStr);
        // 돌리기
        this.activity.runOnUiThread(m);
    }

    /**
     * [실험용] toast 메시지 보기
     * @param msg
     */
    @JavascriptInterface
    public void showToast(String msg) {
        Toast.makeText(this.activity.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
