package com.hh.hhomc.deviceui;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.common.pos.api.util.ShellUtils;
import com.hh.hhomc.MyApplication;
import com.roco.api.weigenRelay.weigenRelay;

/**
 * 紫鹏
 */
public class ZpDeviceUI implements IDeviceUI{


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

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

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


    @Override
    public void showNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.custom.open.statubar");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        activity.sendBroadcast(intent);
    }

    @Override
    public void hideNotification(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.custom.close.statubar");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        activity.sendBroadcast(intent);
    }

    @Override
    public void showNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.custom.show_navigationbar");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        activity.sendBroadcast(intent);
    }

    @Override
    public void hideNavigationBar(Activity activity) {
        Intent intent = new Intent();
        intent.setAction("com.custom.hide_navigationbar");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        activity.sendBroadcast(intent);
    }

    @Override
    public void showFillLight(Activity activity) {

    }

    @Override
    public void hideFillLight(Activity activity) {

    }



    @Override
    public void reboot() {

        try {
            // posutil
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    weigenRelay ii = new weigenRelay(MyApplication.getContext());
                    ii.Reboot();
                }
            });

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


    }

    @Override
    public void silenceInstall(String path,SilenceInstallSuccessListener listener) {
        if(TextUtils.isEmpty(path)){
            return ;
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent("com.custom.app_install");
                intent.putExtra("apkPath", path);
                intent.setPackage("com.android.settings");
                MyApplication.getContext().sendBroadcast(intent);
                if(listener!=null){
                    listener.success(true);
                }
            }
        });
    }


    //Todo 目前还无法使用
    @Override
    public void silenceUnInstall(String packageName) {
        new Handler(Looper.getMainLooper()).post(() -> {
            Intent intent = new Intent("com.custom.app_uninstall");
            intent.putExtra("apkPackageName ", packageName);
            intent.setPackage("com.android.settings");
            MyApplication.getContext().sendBroadcast(intent);
        });
    }


}
