package com.imofan.android.develop.sns;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/***
 * 腾讯，认证builder
 * @author poble
 *
 */
public class TencentOauth implements OauthBuilder {
    
    private String requestUrl = "";         //请求url
    private String redirectUrl = "";        //认证回调url
    private String clientId = "";           //app key
    private Context context;                //上下文环境
    
    /***
     * 构造方法
     * @param context
     */
    public TencentOauth(Context context){
        this.context = context;
        this.clientId = MFSnsConfig.CONSUMER_KEY_TECENT;
        this.redirectUrl = MFSnsConfig.CONSUMER_REDIRECT_URL_TECENT;
        this.requestUrl = MFSnsConfig.AUTHORIZE_URL_TECENT2 + "?client_id=" + clientId + "&response_type=token&redirect_uri=" + redirectUrl + "&display=mobile&forcelogin=true&scope=all";
    }
    
    
    @Override
    public String doResult(String param1, String param2, String param3) {
        String result = "failed";
        // 1、获取腾讯微博用户信息
        String weiBoResult = "";
        String QQResult = "";
        JSONObject obj = null;
        String openId = MFSnsUtil.getUserOpenId(param1);
        String usermessage = "";
       
        try {
            usermessage = MFSnsUtil.getUserInfo(MFSnsService.PLATFORM_QQ_WEIBO,
                    param1, openId);
            obj = new JSONObject(usermessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if(obj!=null){
            JSONObject obj1 = obj.optJSONObject("data");
            if(obj1!=null){
            	JSONObject userObj = new JSONObject();
                try {
    				userObj.put("access_token", param1);
    				userObj.put("openid", openId);
    				userObj.put("nickname", obj1.optString("nick"));
    				userObj.put("brief", obj1.optString("introduction"));
    				userObj.put("icon_50", obj1.optString("head") + "/50");
    				userObj.put("icon_180", obj1.optString("head") + "/180");
    				userObj.put("expires_in", param2);
    				userObj.put("login_time", System.currentTimeMillis());
    				weiBoResult = userObj.toString();
    			} catch (JSONException e) {
    				weiBoResult = "";
    				e.printStackTrace();
    			}	
            }else{
            	weiBoResult = "";
            }
        }
        
        // 1、获取qq空间用户信息
        //String openId1 = MFSnsUtil.getUserOpenId(param1);
        usermessage = MFSnsUtil.getUserInfo(MFSnsService.PLATFORM_QQ_QZONE,
                param1, openId);
        try {
            obj = new JSONObject(usermessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        if(obj!=null){
            QQResult += "{'access_token':'" + param1 + "','openid':'"
            + openId + "','nickname':'" + obj.optString("nickname")
            + "','brief':'" + obj.optString("description")
            + "','icon_50':'" + obj.optString("figureurl")
            + "','icon_180':'" + obj.optString("figureurl_2")
            + "','expires_in':'" + param2 + "','login_time':'"
            + System.currentTimeMillis() + "'}";
            
            JSONObject userObj = new JSONObject();
            try {
				userObj.put("access_token", param1);
				userObj.put("openid", openId);
				userObj.put("nickname", obj.optString("nickname"));
				userObj.put("brief",  obj.optString("description"));
				userObj.put("icon_50", obj.optString("figureurl"));
				userObj.put("icon_180", obj.optString("figureurl_2"));
				userObj.put("expires_in", param2);
				userObj.put("login_time", System.currentTimeMillis());
				QQResult = userObj.toString();
			} catch (JSONException e) {
				QQResult = "";
				e.printStackTrace();
			}
        }
        if(!weiBoResult.equals("")){
            result = "success";
            //用户登录信息保存
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    "platforms", Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();// 获取编辑器
            editor.putString("platform" + MFSnsService.PLATFORM_QQ_WEIBO,weiBoResult);
            editor.commit();// 提交修改
        }
        if(!QQResult.equals("")){
            result = "success";
            //用户登录信息保存
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    "platforms", Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();// 获取编辑器
            editor.putString("platform" + MFSnsService.PLATFORM_QQ_QZONE,QQResult);
            editor.commit();// 提交修改 
        }
        
        
        return result;
    }

    @Override
    public String getRequestUrl() {
        return requestUrl;
    }

    @Override
    public String getRedirectUrl() {
        return redirectUrl;
    }

}
