package com.sheep.gamegroup.util;

import android.app.Activity;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.util.share.ShareLinkConfig;
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 java.io.File;
import java.lang.ref.WeakReference;
import java.util.Locale;

import okhttp3.Call;

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

public class WebViewJs {

    private WeakReference<Activity> actRef;

    private int taskId;

    public WebViewJs(Activity activity) {
        actRef = new WeakReference<>(activity);
    }

    public WebViewJs(Activity activity, int taskId) {
        this.taskId = taskId;
        actRef = new WeakReference<>(activity);
    }

    @JavascriptInterface
    public void setTitle(final String title) {

    }

    @JavascriptInterface
    public void setCanGoBack() {
    }

    @JavascriptInterface
    public void downloadFile(final String url, final String name) {
        //去下载
        if (actRef.get() != null) {
            CommonUtil.getInstance().showDownLoad(actRef.get(), url, name);
        }
    }

    @JavascriptInterface
    public void startApp(String pkg, String cls) {
        if (actRef.get() != null) {
            PackageUtil.startApp(actRef.get(), pkg);
        }
    }

    @JavascriptInterface
    public void gotoLogin() {
        Jump2View.getInstance().gotoLoginAgain();
    }

    @JavascriptInterface
    public void gotoWeb(String url, String title) {
        if (actRef.get() != null) {
            if (!TextUtils.isEmpty(url)) {
                if (TextUtils.isEmpty(title)) {
                    Jump2View.getInstance().goWeb(actRef.get(), url);//启动浏览器h5
                } else {
                    Jump2View.getInstance().goWeb(actRef.get(), url, title);//进入小绵羊h5界面
                }
            }
        }
    }

    @JavascriptInterface
    public void gotoRecharge() {
        if (actRef.get() != null) {
            Jump2View.getInstance().goRechargeAct(actRef.get(), "内部H5");//进入充值绵羊币界面
        }
    }

    @JavascriptInterface
    public String getInvitationCode() {
        return DataUtil.getInstance().getInvitationCode();
    }

    @JavascriptInterface
    public String popShare(String url, String type) {
        if (actRef.get() != null) {
            new ShareLinkConfig().setLink(url).setShareType(type).toShare(actRef.get());
        }
        return "";
    }

    @JavascriptInterface
    public String getHeader() {
        return SpUtils.getToken(SheepApp.getInstance());
    }

    @JavascriptInterface
    public String getWcUrl(String url) {
        if (!TextUtils.isEmpty(url) && actRef.get() != null) {
            Jump2View.getInstance().goWeb(actRef.get(), url);
        }
        return "";
    }

    @JavascriptInterface
    public String getIMEI() {
        return DeviceUtil.getDeviceId(SheepApp.getInstance());
    }

    @JavascriptInterface
    public String getPhoneModel() {
        return SysAppUtil.getSystemModel();
    }

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

    /**
     * 返回结果int类型 0：未安装 1：已安装
     *
     * @return
     */
    @JavascriptInterface
    public int CheckInstall(String packName) {
        if (TextUtils.isEmpty(packName)) {
            G.showToast("包名为空");
            return 0;
        }
        if (actRef.get() != null) {
            return PackageUtil.isAppInstalled(actRef.get(), packName) ? 1 : 0;
        } else {
            return 0;
        }
    }

    @JavascriptInterface
    public int OpenAPP(String packName) {
        if (TextUtils.isEmpty(packName)) {
            G.showToast("包名为空");
            return 0;
        }
        PackageUtil.startApp(SheepApp.getInstance(), 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;
        }
        if(actRef.get()!=null) {
            Jump2View.getInstance().goWeb(actRef.get(), url);
        }
        return 0;
    }

    /**
     * 下载地址
     */
    private void downLoad(String url) {
        OkHttpUtils.get().url(url).build().execute(new FileCallBack(ClassFileHelper.DIR, FileUtil.getFileName(url)) {
            @Override
            public void onError(Call call, Exception e, int id) {
                G.showToast(String.format(Locale.CHINA, "下载失败(%s)", e.getMessage()));
            }

            @Override
            public void onResponse(File response, int id) {
                if (response.getAbsolutePath().endsWith(".apk") && actRef.get()!=null) {
                    ApkUtils.installApk(actRef.get(), response.getAbsolutePath());
                }
            }
        });
    }

}
