package com.hh.hhomc.deviceui;

import static com.orbbec.ssdk.out.OrbbecSSDKConst.Key.KEY_EXT_LONG_TIME_OUT;
import static com.orbbec.ssdk.out.OrbbecSSDKConst.Key.KEY_RESULT_DATA;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import com.common.lib_base.utils.log.AppLogUtils;
import com.hh.hhomc.MyApplication;
import com.orbbec.ssdk.aidl.OrbbecSSDKResponse;
import com.orbbec.ssdk.out.OrbbecSSDKApi;
import com.orbbec.ssdk.out.OrbbecSSDKCallback;
import com.orbbec.ssdk.out.OrbbecSSDKInitCallback;

/**
 * 马里奥
 * y9
 */
public class MlaDeviceUI implements IDeviceUI{


    //用volatile关键字确保 instance 在多线程下的可见性
    private static volatile MlaDeviceUI instance = null;

    //将构造方法私有化，禁止外部通过构造方法创建实例
    private MlaDeviceUI() {
        init();
    }

    //提供一个公共的访问方法，用于获取该类的唯一实例
    public static MlaDeviceUI getInstance() {
        //如果instance为空，就进行实例化
        if (instance == null) {
            //保证多线程下只有一个线程进行实例化
            synchronized (MlaDeviceUI.class) {
                //第二次判断，避免多线程下创建多个实例
                if (instance == null) {
                    instance = new MlaDeviceUI();
                }
            }
        }
        return instance;
    }


    private void init() {
        Bundle extInfo = new Bundle();
        extInfo.putLong(KEY_EXT_LONG_TIME_OUT,2 * 60 * 1000);//初始化超时时间。默认2分钟。
        OrbbecSSDKApi.getInstance().init(MyApplication.getContext(), extInfo, new OrbbecSSDKInitCallback() {
            @Override
            public void onSuccess() {
                AppLogUtils.i("y9初始化--onSuccess");
            }
            @Override
            public void onFail(int errorCode, String errorMsg) {
                AppLogUtils.i("y9初始化--onFail:"+errorCode+" "+errorMsg);
            }
        });
    }

    @Override
    public void showNotification(Activity activity) {
        OrbbecSSDKApi.getInstance().startFunc("C.SYS","setDropdownBarEnable",new Object[]{true},null, new OrbbecSSDKCallback() {
            @Override
            public void onResponse(OrbbecSSDKResponse orbbecSSDKResponse) {
                Object data = orbbecSSDKResponse.getResult().get(KEY_RESULT_DATA);
               //AppLogUtils.i("showNotification--设置是否成功:"+(Boolean)data);
            }
        });
    }

    @Override
    public void hideNotification(Activity activity) {
        OrbbecSSDKApi.getInstance().startFunc("C.SYS","setDropdownBarEnable",new Object[]{false},null, new OrbbecSSDKCallback() {
            @Override
            public void onResponse(OrbbecSSDKResponse orbbecSSDKResponse) {
                Object data = orbbecSSDKResponse.getResult().get(KEY_RESULT_DATA);
               // AppLogUtils.i("hideNotification--设置是否成功:"+(Boolean)data);
            }
        });
    }

    @Override
    public void showNavigationBar(Activity activity) {
        OrbbecSSDKApi.getInstance().startFunc("C.SYS","setNavigationBarVisible",new Object[]{true},null, new OrbbecSSDKCallback() {
            @Override
            public void onResponse(OrbbecSSDKResponse orbbecSSDKResponse) {
                Object data = orbbecSSDKResponse.getResult().get(KEY_RESULT_DATA);
                //AppLogUtils.i("showNavigationBar--设置是否成功:"+(Boolean)data);
            }
        });
    }

    @Override
    public void hideNavigationBar(Activity activity) {
        OrbbecSSDKApi.getInstance().startFunc("C.SYS","setNavigationBarVisible",new Object[]{false},null, new OrbbecSSDKCallback() {
            @Override
            public void onResponse(OrbbecSSDKResponse orbbecSSDKResponse) {
                Object data = orbbecSSDKResponse.getResult().get(KEY_RESULT_DATA);
                //AppLogUtils.i("hideNavigationBar--设置是否成功:"+(Boolean)data);
            }
        });
    }

    @Override
    public void showFillLight(Activity activity) {

    }

    @Override
    public void hideFillLight(Activity activity) {

    }

    @Override
    public void reboot() {
        OrbbecSSDKApi.getInstance().startFunc("C.SYS","reboot",null,null, null);
    }

    @Override
    public void silenceInstall(String path,SilenceInstallSuccessListener listener) {
        if(TextUtils.isEmpty(path)){
            return;
        }
        Object[] args = new Object[]{path};
        OrbbecSSDKApi.getInstance().startFunc("C.APP", "install", args, null, new OrbbecSSDKCallback() {
            @Override
            public void onResponse(OrbbecSSDKResponse orbbecSSDKResponse) {
                if (listener != null) {
                    try {
                        Object data = orbbecSSDKResponse.getResult().get(KEY_RESULT_DATA);
                        if ((Boolean) data) {
                            listener.success(true);
                        } else {
                            listener.success(false);
                        }
                    } catch (Exception e) {
                        listener.success(false);
                    }

                }

            }
        });

    }

    @Override
    public void silenceUnInstall(String packageName) {
        Object[] args = new Object[]{packageName};
        OrbbecSSDKApi.getInstance().startFunc("C.APP","uninstall",args,null, null);
    }

}
