package com.ysnows.update.util;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.upgrade.UpgradeStateListener;
import com.ysnows.base.base.BApp;
import com.ysnows.base.utils.Toasts;
import com.ysnows.update.BuildConfig;
import com.ysnows.update.R;
import com.ysnows.update.StormUpgradeActivity;


/**
 * Created by shihuiyun
 * on 2020/6/3
 */

public class InitUpgrade {

    public static void initBugly(Context context) {
        Beta.autoInit = true;
        Beta.autoCheckUpgrade = true;
        Beta.enableNotification = true;
        Beta.showInterruptedStrategy = true;
        Beta.initDelay = 1 * 500;
        Beta.autoDownloadOnWifi = true;
        Beta.storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        Beta.strUpgradeDialogUpgradeBtn = context.getString(R.string.update_now);

        Beta.upgradeListener = (i, upgradeInfo, b, b1) -> {

            if (upgradeInfo != null) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(context, StormUpgradeActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                }, 1000);
            }
        };

        Beta.upgradeStateListener = new UpgradeStateListener() {
            @Override
            public void onUpgradeFailed(boolean b) {

            }

            @Override
            public void onUpgradeSuccess(boolean b) {

            }

            @Override
            public void onUpgradeNoVersion(boolean b) {

                if (b) {
                    Toasts.INSTANCE.toast(context, R.string.update_tips,0);
                }

            }

            @Override
            public void onUpgrading(boolean b) {

            }

            @Override
            public void onDownloadCompleted(boolean b) {

            }
        };

        Bugly.init(context, BApp.config.buglyAppId(), BuildConfig.DEBUG);
    }
}
