package com.danding.yang800.wxapi;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.danding.yang800.BaseActivity;
import com.danding.yang800.Util.ToastUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;

/**
 * @author xiong_it
 * @description 微信第三方登录，分享demo,更多移动开发内容请关注： http://blog.csdn.net/xiong_it
 * @charset UTF-8
 * @date 2015-9-9下午2:50:14
 */
/*
 * 微信登录，分享应用中必须有这个名字叫WXEntryActivity，并且必须在wxapi包名下，腾讯官方文档中有要求
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    public static final String APP_ID = "wx8cba515fdff29f38";// 微信开放平台申请到的app_id
    private IWXAPI api;
    private SharedPreferences sp;
    RequestQueue requestQueue;

    // String tmDevice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        requestQueue = Volley.newRequestQueue(this);

        if (api == null) {
            api = WXAPIFactory.createWXAPI(getApplicationContext(), APP_ID,
                    true);
        }
        if (!api.isWXAppInstalled()) {
            ToastUtil.show(getBaseContext(), "请先安装微信");
            return;
        }
        api.handleIntent(getIntent(), this);
    }


    /**
     * 请求回调接口
     */
    @Override
    public void onReq(BaseReq req) {
        //	Log.i("TAG", "onReq");
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (api != null) {
            api.handleIntent(getIntent(), this);
        }
        //finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp != null) {
            if (sp.getBoolean("WXlogin", false)) {
                SendAuth.Resp sendAuthResp = (SendAuth.Resp) resp;// 用于分享时不要有这个，不能强转
                final String code = sendAuthResp.code;
                sp.edit().putString("WXcode", code).commit();
            } else {
                switch (resp.errCode) {
                    case BaseResp.ErrCode.ERR_OK:
                        ToastUtil.show(this, "分享成功了");
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:
                        ToastUtil.show(this, "分享取消了");
                        break;
                    case BaseResp.ErrCode.ERR_AUTH_DENIED:
                        ToastUtil.show(this, "分享失败了");
                        break;
                    default:
                        ToastUtil.show(this, "未知错误");
                        break;
                }
            }


//            final String httpurl = "http://"+ Constants.hostUrl+"/b2b2c/wap/login!weixingLogin.do";
//            loginByHttpClientGet(httpurl, code);
        }
        finish();
    }


}
