package com.sheep.gamegroup.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity;
import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
import com.kfzs.duanduan.event.BigEvent;
import com.kfzs.duanduan.event.EventTypes;
import com.kfzs.duanduan.services.DownloadTaskService;
import com.sheep.gamegroup.view.activity.ActWeb;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import okhttp3.Call;

/**
 * Created by ljy on 2018/6/25.
 */

public class WebviewJs {
    private Activity activity;
    private int taskId;
    private String packgeName;
    private DownloadTaskService mDownloadTaskService;

    public WebviewJs(Activity activity) {
        this.activity = activity;
        mDownloadTaskService = new DownloadTaskService(activity);
    }

    public WebviewJs(Activity activity, int taskId) {
        this.activity = activity;
        this.taskId = taskId;
        mDownloadTaskService = new DownloadTaskService(activity);
    }

    @JavascriptInterface
    public void setTitle(final String title) {

    }

    @JavascriptInterface
    public void setCanGoBack() {
    }

    @JavascriptInterface
    public void downloadFile(final String url, final String name) {
        //去下载
        CommonUtil.getInstance().showDownLoad(activity, url, url);
    }

    @JavascriptInterface
    public void startApp(String pkg, String cls) {
        PackageUtil.startApp(activity, pkg);
    }

    @JavascriptInterface
    public void gotoLogin() {
        Jump2View.getInstance().gotoLoginAgain();
    }
    @JavascriptInterface
    public void gotoWeb(String url, String title) {
        if(!TextUtils.isEmpty(url)) {
            if (TextUtils.isEmpty(title)) {
                Jump2View.getInstance().goWeb(activity, url);//启动浏览器h5
            } else {
                Jump2View.getInstance().goWeb(activity, url, title);//进入小绵羊h5界面
            }
        }
    }
    @JavascriptInterface
    public void gotoRecharge() {
        Jump2View.getInstance().goRechargeAct(activity, "内部H5");//进入充值绵羊币界面
    }

    @JavascriptInterface
    public String getInvitationCode(){
        return DataUtil.getInstance().getInvitationCode();
    }
    @JavascriptInterface
    public String popShare(String url, String type) {
        CommonUtil.getInstance().popShare(activity, url, type);
        return "";
    }
    @JavascriptInterface
    public String getHeader() {
        return SpUtils.getToken(activity);
    }

    @JavascriptInterface
    public String getWcUrl(String url) {
        if(!TextUtils.isEmpty(url)){
            Jump2View.getInstance().goWeb(activity, url);
        }
        return "";
    }

    @JavascriptInterface
    public int getTaskId(){
        return taskId;
    }

    /**
     * 返回结果int类型 0：未安装 1：已安装
     * @return
     */
    @JavascriptInterface
    public int CheckInstall(String packName){
        if(TextUtils.isEmpty(packName)){
            G.showToast("包名为空");
            return 0;
        }

        return PackageUtil.isAppInstalled(activity, packName) ? 1 : 0;
    }

    @JavascriptInterface
    public int OpenAPP(String packName){
        if(TextUtils.isEmpty(packName)){
            G.showToast("包名为空");
            return 0;
        }
        PackageUtil.startApp(SheepApp.mContext, packName);
        return 0;
    }
    @JavascriptInterface
    public int InstallAPP(String packName){

        if(TextUtils.isEmpty(packName)){
            G.showToast("下载地址为空");
            return 0;
        }
        downLoad(packName);
        return 0;
    }
    @JavascriptInterface
    public int Browser(String url){
        if(TextUtils.isEmpty(url)){
            G.showToast("连接地址为空");
            return 0;
        }
        Jump2View.getInstance().goWeb(activity, url);
        return 0;
    }

    /**
     * 下载地址
     */
    private void downLoad(String packName){
        if (mDownloadTaskService == null){
            mDownloadTaskService = new DownloadTaskService(activity);
        }
//        Aria.download(this).register();
        DownloadTarget task = Aria.download(activity).load(packName);
        DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(packName);
        if(downLoadInfo == null){
            downLoadInfo = CommonUtil.getInstance()
                    .addNewDownloadTask(packName, packName, packName,
                            Build.VERSION.SDK_INT, 0+"", 10+"", 0, 0);
        }

        if (task.getTaskState() == IEntity.STATE_RUNNING) {
            task.stop();
        } else if (task.getTaskState() == IEntity.STATE_FAIL) {
            task.cancel(true);
            task.removeRecord();
        } else {
            switch (downLoadInfo.getMStatus()){
                case 2:
                case 4:
                    G.showToast("正在下载中...");
                    break;
                case 3:
//                    G.showToast("下载完成");
                    break;
                default:
                    G.showToast("开始下载中!");
                    downLoadInfo.setMStatus(2);
                    break;
            }

            mDownloadTaskService.start(downLoadInfo);
            mDownloadTaskService.addDownloadTask(downLoadInfo);
            final DownLoadInfo finalDownLoadInfo = downLoadInfo;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if(mDownloadTaskService.addDownloadTask(finalDownLoadInfo)){
                        //发送红点信息
                        EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.TIPS_DOWN_ICON));
                    }
                }
            }).start();
        }
    }

    @Download.onTaskFail
    void taskFail(DownloadTask downloadTask){
        if(mDownloadTaskService == null)
            mDownloadTaskService = new DownloadTaskService(activity);
        DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(downloadTask.getKey());
        if(downLoadInfo != null){
            downLoadInfo.setMStatus(1);
            mDownloadTaskService.addDownloadTask(downLoadInfo);
            G.showToast("下载失败");
        }
    }

    @Download.onTaskComplete
    void taskComplete(DownloadTask downloadTask){
        if(mDownloadTaskService == null)
            mDownloadTaskService = new DownloadTaskService(activity);
        DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(downloadTask.getKey());
        if(downLoadInfo != null){
            G.showToast("下载成功");
            downLoadInfo.setMStatus(3);
            mDownloadTaskService.addDownloadTask(downLoadInfo);
            PackageUtil.installApk(activity, downloadTask.getDownloadPath());
        }

    }
    @Download.onTaskRunning
    protected void running(DownloadTask downloadTask) {
        if(mDownloadTaskService == null)
            mDownloadTaskService = new DownloadTaskService(activity);
        DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(downloadTask.getKey());
        if(downLoadInfo != null){
            downLoadInfo.setMStatus(2);
            mDownloadTaskService.addDownloadTask(downLoadInfo);
//            G.showToast("下载中....");
        }
    }

}
