package com.hc.webapp.web;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.webkit.URLUtil;

import com.hc.webapp.BuildConfig;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.util.FileDownloadUtils;

import java.io.File;

public class DownloadInstall {
    private String TAG = "DownloadInstall";
    private Context context;
    public  DownloadInstall(Context context){
        this.context = context;
    }
    public void start(String url,String md5){
        downloadApk(url,md5);
    }

    private void downloadApk(final String url,final String md5){
        String filePath = FileDownloadUtils.getDefaultSaveRootPath();
        FileDownloader.getImpl().create(url)
                .setPath(filePath,true)
                .setCallbackProgressTimes(300)
                .setMinIntervalUpdateSpeed(400)
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        File file = new File( FileDownloadUtils.getDefaultSaveFilePath(url));
                        String apkMD5 = MD5.getFileMD5(file).toUpperCase();
                        String updateD5 = md5.toUpperCase();
                        if(!apkMD5.equals(updateD5)){
                            file.delete();
                            downloadApk(url,md5);
                        }else{
                            apkInstall(file);
                        }
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {

                    }
                })
                .start();
    }

    private void apkInstall(File apkFile){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        }else{
            intent.setDataAndType(Uri.fromFile(apkFile),
                    "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
}
