package com.cooleshow.teacher.widgets.helper;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.utils.LogUtils;

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


/**
 * Description:
 * Copyright  : Copyright (c) 2016
 * Company    : 大雅
 * Author     : 刘瑞
 * Date       : 2018/11/12 15:23
 */
public class JsInterfaceHelper extends Object {
    public static final String ENTERLIVEROOM = "enterLiveRoom";
    public static final String GET_NAV_HEIGHT = "getNavHeight";//获取状态栏高度
    public static final String BACKICONCHANGE = "backIconChange";//顶部栏返回按钮颜色
    public static final String SETBARSTATUS = "setBarStatus";//顶部栏控制

    private Activity activity;
    JSONObject resultJson;

    public OnJsMethodCallListener onListener;


    public JsInterfaceHelper(Activity activity) {
        this.activity = activity;
    }

    /*
     *统一方法
     */
    @JavascriptInterface
    public void postMessage(String message) {
        LogUtils.i("pq", "收到Msg:" + message);
        if (activity == null) {
            return;
        }
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(message);
                    String api = jsonObject.getString("api");
                    handleAction(api, jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        });
    }

    private void handleAction(String api, JSONObject jsonObject) {
        if (TextUtils.equals(GET_NAV_HEIGHT, api)) {
            //获取状态栏高度
            if (onListener != null) {
                onListener.getNavHeight(jsonObject);
            }
            return;
        }

        if (TextUtils.equals(BACKICONCHANGE, api)) {
            //返回按钮颜色
            if (onListener != null) {
                onListener.backIconChange(jsonObject);
            }
            return;
        }

        if (TextUtils.equals(SETBARSTATUS, api)) {
            //状态栏控制
            if (onListener != null) {
                onListener.setBarStatus(jsonObject);
            }
            return;
        }
    }


    public void setOnJsCallListener(OnJsMethodCallListener onListener) {
        this.onListener = onListener;
    }


    public interface OnJsMethodCallListener {

        /**
         * 顶部栏控制
         *
         * @param message
         */
        void setBarStatus(JSONObject message);

        /**
         * 顶部栏返回按钮颜色
         * {iconStyle:  black , white}
         *
         * @param message
         */
        void backIconChange(JSONObject message);

        /**
         * 返回顶部高度
         * 顶部栏高度
         * 以及dpi
         *
         * @param message
         */
        void getNavHeight(JSONObject message);
    }


    public void release() {
        this.activity = null;
    }
}
