package com.cooleshow.teacher.widgets.helper;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.usercenter.constants.UserConstants;
import com.cooleshow.usercenter.helper.UserHelper;

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";//顶部栏控制
    public static final String FINISH = "back";//结束本页面
    public static final String CHOOSEFILE = "chooseFile";//选择文件
    public static final String LOGIN = "login";//选择文件
    public static final String OPENACCOMPANYWEBVIEW = "openAccompanyWebView";
    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(SETBARSTATUS, api)) {
            //状态栏控制
            if (onListener != null) {
                onListener.setBarStatus(jsonObject);
            }
            return;
        }

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

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


        if (TextUtils.equals(FINISH, api)) {
            //结束本页面
            activity.finish();
            return;
        }

        if (TextUtils.equals(CHOOSEFILE, api)) {
            //选择文件
            if (onListener != null) {
                onListener.chooseFile(jsonObject);
            }
            return;
        }
        if (TextUtils.equals(LOGIN, api)) {
            UserHelper.saveUserToken("");
            ARouter.getInstance().build(RouterPath.UserCenter.PATH_VERIFY_LOGIN)
                    .withString(UserConstants.PHONE_NUM_KEY, UserHelper.getUserPhone())
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
                    .navigation();
            return;
        }
        if (TextUtils.equals(OPENACCOMPANYWEBVIEW, api)) {
            try {
                JSONObject content = jsonObject.getJSONObject("content");
                boolean isHideTitle = false;
                boolean statusBarTextColor = false;
                boolean isOpenLight = false;
                int orientation = -1;
                try {
                    isHideTitle = content.getBoolean("isHideTitle");
                    statusBarTextColor = content.getBoolean("statusBarTextColor");
                    isOpenLight = content.getBoolean("isOpenLight");
                    orientation = content.getInt("orientation");
                } catch (Exception e) {
                }
                ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_ACCOMPANY_HTML)
                        .withString("url", content.getString("url"))
                        .withBoolean("isHideTitle", isHideTitle)
                        .withBoolean("statusBarTextColor", statusBarTextColor)
                        .withBoolean("isOpenLight", isOpenLight)
                        .withInt("orientation", orientation)
                        .navigation();
                return;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


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


    public interface OnJsMethodCallListener {
        void onSendMessage(String message);

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

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

        /**
         * 选择文件
         *
         * @param message
         */
        void chooseFile(JSONObject message);

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


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