package com.sheep.gamegroup.util.js;

import android.view.View;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.absBase.BaseUMActivity;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.share.ShareLinkConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
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
 */
public abstract class BaseActWeb extends BaseUMActivity {
    public abstract boolean isResponseBack();

    public abstract void setResponseBack(boolean responseBack);

    public abstract void clearWebView();

    public abstract void loadJs(String js);

    public abstract void showTitleBar(boolean isShow);

    public void superFinish(){
        super.finish();
    }


    public static String addUrlToken(String url) {
        return addUrl(url, "authorization", SpUtils.getToken(SheepApp.getInstance()));
    }

    public static String addUrl(String url, String key, String value) {
        return url + ((url.contains("?") || url.contains("#") && url.contains("=")) ? "&" : "?") + key + "=" + value;
    }
    public void setH5Type(String h5Type) {
        switch (h5Type){
            case "is_unicom":
                TitleBarUtils
                        .getInstance()
                        .setRightImgBotton(this, R.mipmap.share, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                CommonUtil.getInstance().callActionWithUserInfo(new Action1<UserEntity>() {
                                    @Override
                                    public void call(UserEntity userEntity) {
                                        if (userEntity == null) {
                                            G.showToast("分享功能调试中");
                                            return;
                                        }
//                                是否是联通卡分享 1:是 0:不是
//                                分享链接需要加个 is_unicom  参数
                                        String url = addUrl(userEntity.getShareLink(), "is_unicom", "1");
                                        new ShareLinkConfig().setLink(url).setTitle("下载小绵羊，免费领取联通大/小天神卡").toShare(BaseActWeb.this);
                                    }
                                });
                            }
                        });
                break;
        }
    }
    @Override
    public void onStart(SHARE_MEDIA share_media) {
        //开始分享
        loadJs(String.format(Locale.CHINA, "callback(\"onStart\", %s)", share_media.getName()));
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
        //分享成功 微信分享时，取消也会成功
        loadJs(String.format(Locale.CHINA, "callback(\"onResult\", %s)", share_media.getName()));
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        //分享出错
        String msg = throwable.getMessage();
        G.showToast(msg != null && msg.contains("没有安装应用") ? "没有安装应用" : msg);
        loadJs(String.format(Locale.CHINA, "callback(\"onError\", %s)", share_media.getName()));
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        //取消分享 微信分享时，没有取消
        loadJs(String.format(Locale.CHINA, "callback(\"onCancel\", %s)", share_media.getName()));
    }
}
