package com.hh.hhomc.deviceui;

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

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

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

/**
 * K8、F10
 */
public class K8DeviceUI implements IDeviceUI {

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

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

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

    @Override
    public void showNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.android.systemui.statusbar.phone.statusopen");
        activity.sendBroadcast(intent);
    }

    @Override
    public void hideNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.android.systemui.statusbar.phone.statusclose");
        activity.sendBroadcast(intent);
    }

    @Override
    public void showNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.android.internal.policy.impl.showNavigationBar");
        activity.sendBroadcast(intent);
    }

    @Override
    public void hideNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.android.internal.policy.impl.hideNavigationBar");
        activity.sendBroadcast(intent);
    }

    @Override
    public void showFillLight(Activity activity) {

    }

    @Override
    public void hideFillLight(Activity activity) {

    }

    @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) {

    }


}
