package com.uiui.zyappstore.receiver;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.uiui.zyappstore.utils.ApkUtils;
import com.uiui.zyappstore.utils.JGYUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;

public class NewAppReceiver extends BroadcastReceiver {
    private static final String TAG = NewAppReceiver.class.getSimpleName();
    private static NewAppListener newAppListener;
    @SuppressLint("StaticFieldLeak")
    private static Context mContext;

    static {
        sendAppInfo();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        String state;
        if (TextUtils.isEmpty(action)) {
            Log.e(TAG, "onReceive: " + "action is empty ");
            return;
        }
        String packageName = intent.getDataString().replace("package:", "");
        switch (action) {
            case Intent.ACTION_PACKAGE_ADDED:
                state = "安装了:";
                break;
            case Intent.ACTION_PACKAGE_REPLACED:
                state = "重装了:";
                break;
            case Intent.ACTION_PACKAGE_REMOVED:
                state = "卸载了:";
                break;
            default:
                state = "未知:";
                break;
        }
        Log.e(TAG, "sendAppInfo: " + state + packageName);
        ApkUtils.RemoveTask(mContext, packageName);
        if ("com.uiui.sn".equals(packageName)) {
            JGYUtils.getInstance().wakeUpDeviceInfo();
        } else if ("com.uiuios.updatetools".equals(packageName)) {
            JGYUtils.getInstance().wakeUpUpdateTools();
        }
        newAppListener.setNewAppListener(packageName);
    }


    public interface NewAppListener {
        void setNewAppListener(String packageName);
    }

    private static void sendAppInfo() {
        Observable.create((ObservableEmitter<String> emitter) -> {
            newAppListener = (String packageName) -> {
                emitter.onNext(packageName);
            };
        })
                .throttleLast(5, TimeUnit.SECONDS)
                //这句很关键，这是RxJava中的操作符，在规定时间内观察者不在接收被观察者发送的事件
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        Log.e(TAG, "sendAppInfo: onNext: " + s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: ");
                    }
                });
    }
}
