package com.hw.nativeapp.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


import com.blankj.utilcode.util.ToastUtils;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.config.Contents;
import com.hw.nativeapp.httpnet.ErrorConsumer;
import com.hw.nativeapp.httpnet.ResponseConsumer;
import com.hw.nativeapp.httpnet.entity.UpdateVerAskBean;
import com.hw.nativeapp.httpnet.entity.UpdateVerReqBean;
import com.hw.nativeapp.httpnet.upgrade.UpgradeHandler;
import com.hw.nativeapp.ui.dialogs.Dialog_Update;
import com.hw.nativeapp.ui.dialogs.Dialog_UpdateProgress;

public class UpgradeUtils {


    public static  void upgrade(Context context){
        try {
            UpdateVerReqBean bean = new UpdateVerReqBean();
            bean.appId = Contents.APPID;
            bean.name = context.getResources().getString(R.string.app_name);
            PackageManager manager = context.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
            bean.versionName = packageInfo.versionName;
            bean.versionCode = packageInfo.versionCode;
            MApplication.getApiService().version(bean,System.currentTimeMillis()/1000)
                    .compose(RxUtil.applyObservableAsync())
                    .subscribe(new ResponseConsumer<UpdateVerAskBean>() {
                        @Override
                        public void onSuccess(UpdateVerAskBean data) {
                            if (!data.isUpload){
                                ToastUtils.showLong("当前为最新版本");
                            }else {
                                showUpdate(data,packageInfo, context);
                            }
                        }
                    },new ErrorConsumer());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void showUpdate(UpdateVerAskBean bean, PackageInfo packageInfo, Context context){
        String note = "当前版本:" + packageInfo.versionName + "." + packageInfo.versionCode + "\n" +
                "新版本:" + bean.versionName + "." + bean.versionCode + "\n" +
                "版本描述:" + bean.note;
        Dialog_Update.Builder builder = new Dialog_Update.Builder(context);
        builder.setMessage(note)
                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                .setPositionButton("确定", (dialog, which) -> {
                    dialog.dismiss();
                    startDownload(context, bean.pkgUrl);
                })
                .create()
                .show();
    }

    private static void startDownload(Context context , String downUrl){
        Dialog_UpdateProgress.Builder builder = new Dialog_UpdateProgress.Builder(context);
        Dialog_UpdateProgress builderdig  = builder.create();
        builderdig.show();
        UpgradeHandler updateHandler = new UpgradeHandler(context,downUrl);
        updateHandler.setOnListener(new UpgradeHandler.OnDownloadListener() {
            @Override
            public void complate() {
                builderdig.dismiss();
            }

            @Override
            public void progress(int soFarBytes, int totalBytes) {
                builder.setTxProgress(soFarBytes, totalBytes);
                builder.setProgressBar(soFarBytes, totalBytes);
            }
        });
        updateHandler.start();
    }
}
