package com.uiui.zyappstore.network;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.uiui.zyappstore.BuildConfig;
import com.uiui.zyappstore.bean.AppInfo;
import com.uiui.zyappstore.bean.BaseResponse;
import com.uiui.zyappstore.utils.FileUtils;
import com.uiui.zyappstore.utils.JGYUtils;

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


public class HTTPInterface {
    private static final int GET_ALL_APPINFO = 0;
    private static final int GET_USERINFO = 1;
    private static final int GET_SORTAPP = 2;


    public static void checkUpdate(final Context context, String packageName, String versionCode) {
        NetInterfaceManager.getInstance()
                .getNewestAppUpdateControl()
                .getAppUpdate(packageName, versionCode, JGYUtils.getInstance().checkAppPlatform())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<AppInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<AppInfo> appInfoBaseResponse) {
                        int code = appInfoBaseResponse.code;
                        if (code == 200) {
                            AppInfo appInfo = appInfoBaseResponse.data;
                            if (null != appInfo) {
                                JsonObject jsonObject = JsonParser.parseString(new Gson().toJson(appInfo)).getAsJsonObject();
                                FileUtils.ariaDownload(context, appInfo.getApp_url(), jsonObject);
                            }
                            Log.e("checkUpdate", "update now");
                        } else {
                            Log.e("checkUpdate", "not find update");
                        }
                    }

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

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public static void checkUpdate(Context context) {
        String packageName = BuildConfig.APPLICATION_ID;
        String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
        checkUpdate(context, packageName, versionCode);
    }

    public static void checkUpdate(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = null;
        try {
            info = pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (null != info) {
            long appVersionCode;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = info.getLongVersionCode();
            } else {
                appVersionCode = info.versionCode;
            }
            checkUpdate(context, packageName, String.valueOf(appVersionCode));
        } else {
            checkUpdate(context, packageName, "0");
        }
    }
}

