package com.sheep.gamegroup.util.js;

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

import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.util.ApiUtil;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.DeviceUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.QQUtil;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.util.share.ShareLinkConfig;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Locale;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/23.
 * realicing@sina.com
 * window.kfzsjs.方法名(参数)
 */
public class KFZSJs {
    private BaseActWeb activity;

    public KFZSJs(BaseActWeb activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void setTitle(final String title) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TitleBarUtils
                        .getInstance()
                        .setTitle(activity, title);
            }
        });
    }

    /**
     * 是否响应返回键
     */
    @JavascriptInterface
    public boolean isResponseBack() {
        return activity.isResponseBack();
    }

    /**
     * 设置是否响应返回键
     */
    @JavascriptInterface
    public void setResponseBack(boolean responseBack) {
        activity.setResponseBack(responseBack);
    }

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

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

    @JavascriptInterface
    public void gotoLogin() {
        activity.clearWebView();
        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) {
        shareLink(url, null, null, null, type);
        return "";
    }
    //h5调用分享 window.kfzsjs.shareLink(link, iconUrl, title, des);
    @JavascriptInterface
    public void shareLink(String link, String iconUrl, String title, String des) {
        shareLink(link, iconUrl, title, des, ShareLinkConfig.ALL);
    }
    private void shareLink(String link, String iconUrl, String title, String des,@ShareLinkConfig.SHARE_TYPE String shareType) {
        new ShareLinkConfig().setLink(link).setIconUrl(iconUrl).setTitle(title).setDes(des).setShareType(shareType).toShare(activity);
    }

    @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 String getIMEI() {
        return DeviceUtil.getDeviceId(SheepApp.getInstance());
    }
    @JavascriptInterface
    public String getPhoneModel() {
        return SysAppUtil.getSystemModel();
    }
    @JavascriptInterface
    public void showTitleBar(final boolean isShow) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.showTitleBar(isShow);
            }
        });
    }

    @JavascriptInterface
    public void goBack() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.finish();
            }
        });
    }

    @JavascriptInterface
    public void finishAct() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.superFinish();
            }
        });
    }
    @JavascriptInterface
    public void skip2QQ() {
        ApiUtil.getNewAboutUsComplainQq(new Action1<String>() {
            @Override
            public void call(String qq) {
                QQUtil.skip3(activity, qq);
            }
        });
    }

    @JavascriptInterface
    public void gotoTask(final int taskId) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Jump2View.getInstance().goTaskDetailView(activity, taskId);
            }
        });
    }
    @JavascriptInterface
    public void setH5Type(final String h5Type) {
        if(h5Type == null)
            return;
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.setH5Type(h5Type);
            }
        });
    }

    @JavascriptInterface
    public void alipay(String payInfo, boolean b){
        CommonUtil.getInstance().alipay(payInfo, b, new AbsObserver<String>() {
            @Override
            public void onNext(String result) {
                activity.loadJs(String.format(Locale.CHINA, "onAlipayResult('%s')", result));
            }

            @Override
            public void onError(Throwable e) {
                activity.loadJs(String.format(Locale.CHINA, "onAlipayResult('%s')", e.getMessage()));
            }
        });
    }
}
