package com.sheep.gamegroup.util;

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.util.Locale;

import okhttp3.Call;

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

public class WebViewJs {
    private int taskId;

    public WebViewJs() {

    }

    public WebViewJs(int taskId) {
        this.taskId = taskId;
    }

    @JavascriptInterface
    public void setTitle(final String title) {

    }

    @JavascriptInterface
    public void setCanGoBack() {
    }

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

    @JavascriptInterface
    public void startApp(String pkg, String cls) {
        PackageUtil.startApp(ActivityManager.getInstance().currentActivity(), 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(ActivityManager.getInstance().currentActivity(), url);//启动浏览器h5
            } else {
                Jump2View.getInstance().goWeb(ActivityManager.getInstance().currentActivity(), url, title);//进入小绵羊h5界面
            }
        }
    }
    @JavascriptInterface
    public void gotoRecharge() {
        Jump2View.getInstance().goRechargeAct(ActivityManager.getInstance().currentActivity(), "内部H5");//进入充值绵羊币界面
    }

    @JavascriptInterface
    public String getInvitationCode(){
        return DataUtil.getInstance().getInvitationCode();
    }
    @JavascriptInterface
    public String popShare(String url, String type) {
        new ShareLinkConfig().setLink(url).setShareType(type).toShare(ActivityManager.getInstance().currentActivity());
        return "";
    }
    @JavascriptInterface
    public String getHeader() {
        return SpUtils.getToken(SheepApp.getInstance());
    }

    @JavascriptInterface
    public String getWcUrl(String url) {
        if(!TextUtils.isEmpty(url)){
            Jump2View.getInstance().goWeb(ActivityManager.getInstance().currentActivity(), 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;
        }

        return PackageUtil.isAppInstalled(ActivityManager.getInstance().currentActivity(), packName) ? 1 : 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;
        }
        Jump2View.getInstance().goWeb(ActivityManager.getInstance().currentActivity(), 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")){
                    ApkUtils.installApk(ActivityManager.getInstance().currentActivity(), response.getAbsolutePath());
                }
            }
        });
    }

}
