package com.hh.hhomc.deviceui;

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

import com.common.face.api.FaceUtil;
import com.common.lib_base.utils.log.AppLogUtils;
import com.common.pos.api.util.ShellUtils;

/**
 * 天波-D2
 */
public class D2DeviceUI implements IDeviceUI{


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

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

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


    // 显示下拉通知栏
    @Override
    public void showNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.systemui");
        intent.putExtra("statusbar_drop", "on");
        activity.sendBroadcast(intent);
    }

    // 禁止下拉通知栏
    @Override
    public void hideNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.systemui");
        intent.putExtra("statusbar_drop", "off");
        activity.sendBroadcast(intent);
    }

    // 显示底部导航栏
    @Override
    public void showNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.systemui");
        intent.putExtra("navigation_bar", "show");
        activity.sendBroadcast(intent);
    }

    // 隐藏底部导航栏
    @Override
    public void hideNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.systemui");
        intent.putExtra("navigation_bar", "dismiss");
        activity.sendBroadcast(intent);
    }

    @Override
    public void showFillLight(Activity activity) {
        FaceUtil.GPIOSet("rgb_led_en",1);//1开,0关
        FaceUtil.GPIOSet("ir_led_en",1);//1开,0关
    }

    @Override
    public void hideFillLight(Activity activity) {
        FaceUtil.GPIOSet("rgb_led_en",0);//1开,0关
        FaceUtil.GPIOSet("ir_led_en",0);//1开,0关
    }

    @Override
    public void reboot() {

        try {
            // posutil
            ShellUtils.execCommand("reboot", true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void silenceInstall(String path,SilenceInstallSuccessListener listener) {
        if(TextUtils.isEmpty(path)){
            return;
        }

        try {
            String[] commands = {"0", "chmod", "777", path};
            ShellUtils.execCommand(commands, true);
            String cmd = "pm install -r " + path;
            ShellUtils.CommandResult commandResult = ShellUtils.execCommand(cmd, true);
            if (commandResult != null && commandResult.result == 0 && listener != null) {
                listener.success(true);
            } else if (listener != null) {
                listener.success(false);
            }
        } catch (Exception e) {
            AppLogUtils.e(e.toString());
            if (listener != null) {
                listener.success(false);
            }
        }
    }

    @Override
    public void silenceUnInstall(String packageName) {

    }




}
