package com.mayi.fastdevelop.web;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.webkit.JavascriptInterface;

import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.util.SpUtil;
import com.mayi.fastdevelop.util.SystemUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;

public class JsCallAppApi {

    private WeakReference<DWebView> weakReference;
    private Handler handler = new Handler(Looper.getMainLooper());

    public JsCallAppApi(DWebView dWebView) {
        weakReference = new WeakReference<>(dWebView);
    }

    @JavascriptInterface
    public String goBrowser(Object data) {//跳转浏览器
        DWebView webView = weakReference.get();
        if (webView != null) {
            SystemUtil.gotoWeb(webView.getContext(), data.toString());
        }
        return "";
    }

    @JavascriptInterface
    public String getUserInfo(Object data) {//给web传递用户信息
        return SpUtil.get(Key.USER_INFO, "");
    }

    @JavascriptInterface
    public String openDebug(Object data) {//开启Debug模式
        DWebView.setWebContentsDebuggingEnabled(true);
        return "开启Debug模式";
    }

    @JavascriptInterface
    public String backPage(Object data) {//返回上一页
        handler.post(new Runnable() {
            @Override
            public void run() {
                DWebView webView = weakReference.get();
                if (webView != null) {
                    if (webView.canGoBack()) {
                        webView.goBack();
                    } else {
                        if (webView.getContext() instanceof Activity) {
                            ((Activity) webView.getContext()).finish();
                        }
                    }
                }
            }
        });
        return "返回上一页";
    }

    @JavascriptInterface
    public String goCallPhone(Object data) {//去打电话
        DWebView webView = weakReference.get();
        if (webView != null) {
            try {
                JSONObject jsonObject = new JSONObject(data.toString());
                SystemUtil.makePhoneCall(webView.getContext(), jsonObject.optString("tel"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    @JavascriptInterface
    public String clearCache(Object data) {//清除缓存
        handler.post(new Runnable() {
            @Override
            public void run() {
                DWebView webView = weakReference.get();
                if (webView != null) {
                    webView.clearCache(true);
                    webView.clearHistory();
                    webView.clearFormData();
                }
            }
        });
        return "清除缓存";
    }

    @JavascriptInterface
    public String sendSMS(Object data) {//发送短信
        DWebView webView = weakReference.get();
        if (webView != null) {
            try {
                JSONObject jsonObject = new JSONObject(data.toString());
                SystemUtil.sendSMS(webView.getContext(), jsonObject.optString("phone")
                        , jsonObject.optString("msg"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 跳转原生页面
     * 待扩展
     */
    @JavascriptInterface
    public String goPage(Object data) {
        String pageName = null, parameter = null;
        try {
            JSONObject jsonObject = new JSONObject(data.toString());
            pageName = jsonObject.optString("page");
            parameter = jsonObject.optString("parameter");
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        if (TextUtils.equals(pageName, "MapLocation")) {//地图定位页面
//            Intent intent = new Intent(this, MapLocationActivity.class);
//            startActivityForResult(intent, RequestCode.MAP_LOCATION_ACTIVITY_RESULTCODE);
//        } else {
//        if (webListener != null) {
//            webListener.goPageListener(pageName, parameter);
//        }
//        }
        return "跳转原生页面" + data.toString();
    }


}
