package com.zxhl.cms.common.base;

import android.content.Intent;
import android.view.View;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zxhl.cms.R;
import com.zxhl.cms.common.Constant;
import com.zxhl.cms.widget.LoadingDialog;

import java.util.Map;

/**
 * Created by gaoleichao on 2018/6/20.
 */

public abstract class WxImpl extends BaseActivity implements UMAuthListener {

    protected UMShareAPI mShareAPI;
    protected LoadingDialog mLoading;

    @Override
    public int layoutID() {
        return 0;
    }

    @Override
    public void init() {
       // PlatformConfig.setWeixin(Constant.Key.WX_LOGIN_APP_ID, Constant.Key.WX_LOGIN_KEY);
        mShareAPI = UMShareAPI.get(getApplicationContext());
        mLoading = LoadingDialog.getLoadingDialog(this, getString(R.string.wait_ing), false, false);

    }

    @Override
    public void onClick(View v) {

    }

    protected void onWxLogin() {
        mLoading.setLoading(getString(R.string.login_ing));
        mLoading.show();
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareAPI.setShareConfig(config);
        mShareAPI.getPlatformInfo(this, SHARE_MEDIA.WEIXIN, this);

    }

    protected void onAuth() {
        mLoading.setLoading(getString(R.string.auth_ing));
        mLoading.show();
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareAPI.getPlatformInfo(this, SHARE_MEDIA.WEIXIN, this);
    }


    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> data) {
        onWxSuccess(data.get("access_token"), data.get("openid"));
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
        mLoading.setResult(false, "没有安装微信", 1000);
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        mLoading.setResult(false, "授权取消", 1000);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        }catch (Exception ex){

        }
    }

    public abstract void onWxSuccess(String token, String openid);
}
