package com.rdiot.yx485;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.zcolin.frame.http.ZHttp;
import com.zcolin.frame.http.response.ZFileResponse;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class UpdateMgr {
    public static final int GET_UNKNOWN_APP_SOURCES = 150;
    /**
     * 下载App
     */
    public static void downLoadApp(final Activity activity, String url) {
        String fileName = "zonghe.apk";
        if (fileName != null) {
            ZHttp.downLoadFile(url, new ZFileResponse( activity.getFilesDir()+ "/zonghe.apk", activity, "正在下载……") {
                @Override
                public void onError(int code, Call call, Exception e) {
                    Toast.makeText(activity, "下载失败！", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onSuccess(Response response, File resObj) {
//                    AppUtil.installBySys(App.APP_CONTEXT, resObj);
                    installDownloadApp(activity, resObj);
                }

                @Override
                public void onProgress(float progress, long total) {
                    super.onProgress(progress, total);
                    setBarMsg("正在下载……");
                  //  setBarMsg("正在下载……" + (int)(progress * 100) + "/" + 100);
                }
            });
        } else {
            Toast.makeText(activity, "下载URL不合法", Toast.LENGTH_SHORT).show();
        }
    }
    private static void installDownloadApp(Context mContext, File apkfile){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", apkfile);
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            mContext.startActivity(install);
        } else {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
                    "application/vnd.android.package-archive");
            mContext.startActivity(i);
        }
    }

    private static String getFileNameByUrl(String url) {
        String fileName = null;
        if (!TextUtils.isEmpty(url)) {
            try {
                int start = url.lastIndexOf(".com/") + 5;
                int end =  url.lastIndexOf("?");
                fileName = url.substring(start,end);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }
    public static void checkPermission(Activity mContext, String url) {
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
//            boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();
//            if (hasInstallPermission) {
//                //下载安装应用
//                downLoadApp(mContext, url);
//            } else {
//                //跳转至"安装未知应用"权限界面，引导用户开启权限
//                Uri selfPackageUri = Uri.parse("package:" + mContext.getPackageName());
//                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, selfPackageUri);
//                mContext.startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
//            }
//        } else {
            //下载安装应用
            downLoadApp(mContext, url);
//        }
    }
}
