package com.ttstd.dialer.activity.app;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ResolveInfo;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.ActivityAppListBinding;
import com.ttstd.dialer.db.app.AppInfo;
import com.ttstd.dialer.db.app.AppRepository;
import com.ttstd.dialer.utils.ApkUtils;

import java.text.Collator;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class AppListViewModel extends BaseViewModel<ActivityAppListBinding, ActivityEvent> {
    private static final String TAG = "AppListViewModel";

    private AppRepository mAppRepository;

    @Override
    public void setContext(Context context) {
        super.setContext(context);
        mAppRepository = new AppRepository(context);
    }

    public MutableLiveData<List<AppInfo>> mDesktopSortAppData = new MutableLiveData<>();

    public void getDbAppList() {
        Observable.fromCallable(new Callable<List<AppInfo>>() {
            @Override
            public List<AppInfo> call() throws Exception {
                return mAppRepository.getInsideApp();
            }
        }).compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<AppInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getDbAppList", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull List<AppInfo> appInfos) {
                        Log.e("getDbAppList", "onNext: " + appInfos);
                        mDesktopSortAppData.setValue(appInfos);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getDbAppList", "onError: " + e.getMessage());
                    }

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

    public MutableLiveData<Integer> mAppUpdateData = new MutableLiveData<>();

    public void updateAppInfo(AppInfo appInfo){
        Observable.fromCallable(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return mAppRepository.update(appInfo);
            }
        }).compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("updateAppInfo", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull Integer row) {
                        Log.e("updateAppInfo", "onNext: " + row);
                        mAppUpdateData.setValue(row);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("updateAppInfo", "onError: " + e.getMessage());
                    }

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

}
