package com.simple.plugin;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.google.gson.Gson;
import com.simple.plugin.signDemo.EventImgPath;
import com.simple.plugin.signDemo.MainTwoActivity;
import com.simple.plugin.utilTwo.JsonUtil;
import com.supconit.develop.JSBridge;
import com.supconit.develop.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class SimpleAndroidPluginNativiePart implements JSBridge {

    @Override
    public void onApplicationCreate(Application application) {

    }
    private Response mResponse;
    Activity activity;
    private String message;

    // todo 为了释放callBack内存 必须回调其中success或者error方法
    @Override
    public void execute(String action, JSONArray args, Response callbackContext) {
        mResponse=callbackContext;
        try {
            message = args.getString(0);
            switch (action){
                case "ckme":
                    //获取app信息
                    getAppInfo(callbackContext);
                    break;
                case "dialog":
                    //进入电子签名页面
                    if (!EventBus.getDefault().isRegistered(this)) {
                        EventBus.getDefault().register(this);
                    }
                    String screenNum= JsonUtil.getJsonString(message,"screenNum");
                    String imgPath=JsonUtil.getJsonString(message,"imgPath");
                    MainTwoActivity.start(activity,screenNum,imgPath);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityCreate(final Activity act) {
        this.activity = act;

    }

    @Override
    public void onActivityPase(Activity activity) {

    }

    @Override
    public void onActivityDestory(Activity activity) {

    }
    /**
     * 获取应用信息
     */
    private void getAppInfo(Response callbackContext) throws JSONException {
        int pid = android.os.Process.myPid();
        //任务管理类
        ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        //遍历所有应用
        assert manager != null;
        List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid) {//得到当前应用
                String packageName = info.processName;//返回包名
                PackageManager packageManager = activity.getPackageManager();
                try {
                    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
                    String appName = applicationInfo.loadLabel(packageManager).toString();

                    PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
                    String versionName = packageInfo.versionName;

                    JSONObject object = new JSONObject();
                    object.put("id", packageName);
                    object.put("name", appName);
                    object.put("version", versionName);
                    int flags = activity.getApplicationInfo().flags;
                    if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                        object.put("isRelease", false);
                    } else {
                        object.put("isRelease", true);
                    }
                    callbackContext.success(object);

                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                    callbackContext.error(e.getMessage().toString());
                }
            }
        }
    }
    @Subscribe
    public void onEvent(EventImgPath imgPath){
        if (mResponse==null){
            return;
        }
        String fileImg=imgPath.getImagePath();
        imgPath.setImagePath(fileImg);
        Gson gson=new Gson();
        try {
            mResponse.success(new JSONObject(gson.toJson(imgPath)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}