package com.imofan.android.develop.sns;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import android.content.Context;

/***
 * 开放平台接口appkey和相关接口url配置类
 * 
 * @author poble
 * 
 */
public class MFSnsConfig {

    // 应用的appkey和回调url，用户使用前需要初始化
    public static String CONSUMER_KEY_SINA = "";
    public static String CONSUMER_REDIRECT_URL_SINA = "";
    public static String CONSUMER_KEY_TECENT = "";
    public static String CONSUMER_REDIRECT_URL_TECENT = "";
    public static String CONSUMER_KEY_QZONE = "";
    public static String CONSUMER_REDIRECT_URL_QZONE = "";
    public static String CONSUMER_KEY_RENREN;
    public static String CONSUMER_SECRET_RENREN;
    public static String CONSUMER_REDIRECT_URL_RENREN = "http://graph.renren.com/oauth/login_success.html";
    
    public static boolean NIGHTMODE = false;

    // 新浪微博
    public static final String AUTHORIZE_URL_SINA = "https://api.weibo.com/oauth2/authorize";
    static final String ADD_URL_SINA = "https://api.weibo.com/2/statuses/update.json";
    static final String ADD_PIC_URL_SINA = "http://upload.api.weibo.com/2/statuses/upload.json";
    static final String USER_INFO_URL_SINA = "https://api.weibo.com/2/users/show.json";

    // 腾讯微博
    public static final String AUTHORIZE_URL_TECENT2 = "https://graph.z.qq.com/moc2/authorize";
    static final String ADD_URL_TECENT2 = "https://graph.qq.com/t/add_t";
    static final String ADD_PIC_URL_TECENT2 = "https://graph.qq.com/t/add_pic_t";
    static final String USER_INFO_URL_TECENT2 = "https://graph.qq.com/user/get_info";

    // qq空间
    public static final String AUTHORIZE_URL_QZONE = "https://graph.qq.com/oauth2.0/authorize";
    static final String ADD_URL_QZONE = "https://graph.qq.com/t/add_t";
    static final String ADD_PIC_URL_QZONE = "https://graph.qq.com/t/add_pic_t";
    static final String ADD_SHARE_QZONE = "https://graph.qq.com/share/add_share";
    static final String USER_INFO_URL_QZONE = "https://graph.qq.com/user/get_simple_userinfo";
    static final String GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";

    // 人人网
    public static final String AUTHORIZE_URL_RENREN = "https://graph.renren.com/oauth/authorize";
    
    public static String CONSUMER_WEIXIN_APPID;

    /***
     * 初始化平台账号信息
     * 
     * @param consumer_key
     * @param redirect_url
     * @param platform
     */
    public static void init(String consumer_key, String redirect_url,
            int platform) {
        switch (platform) {
        case MFSnsService.PLATFORM_SINA_WEIBO:
            CONSUMER_KEY_SINA = consumer_key;
            CONSUMER_REDIRECT_URL_SINA = redirect_url;
            break;
        case MFSnsService.PLATFORM_QQ_WEIBO:
            CONSUMER_KEY_TECENT = consumer_key;
            CONSUMER_REDIRECT_URL_TECENT = redirect_url;
            break;
        case MFSnsService.PLATFORM_QQ_QZONE:
            CONSUMER_KEY_QZONE = consumer_key;
            CONSUMER_REDIRECT_URL_QZONE = redirect_url;
            break;
        default:
            break;

        }
    }

    /**
     * 通过反射，获取资源id
     * 
     * @param c
     *            应用上下文环境
     * @param 资源类型
     * @param 资源属性名称
     * @return
     */
    public static int getIdByReflection(Context c, String className,
            String fieldName) {
        int id = 0;
        Class<?> idClass;
        Field idField;
        try {
            idClass = Class.forName((new StringBuilder(String.valueOf(c
                    .getPackageName()))).append(".R$").append(className)
                    .toString());
            idField = idClass.getField(fieldName);
            id = idField.getInt(fieldName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return id;
    }

    /****
     * 人人网，请求参数签名
     * 
     * @param paramMap
     * @param secret
     * @return
     */
    public static String getSignature(Map<String, String> paramMap) {
        List<String> paramList = new ArrayList<String>(paramMap.size());
        // 1、参数格式化
        for (Map.Entry<String, String> param : paramMap.entrySet()) {
            paramList.add(param.getKey() + "=" + param.getValue());
        }
        // 2、排序并拼接成一个字符串
        Collections.sort(paramList);
        StringBuffer buffer = new StringBuffer();
        for (String param : paramList) {
            buffer.append(param);
        }
        // 3、追加script key
        buffer.append(CONSUMER_SECRET_RENREN);
        // 4、将拼好的字符串转成MD5值
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            StringBuffer result = new StringBuffer();
            try {
                for (byte b : md.digest(buffer.toString().getBytes("UTF-8"))) {
                    result.append(Integer.toHexString((b & 0xf0) >>> 4));
                    result.append(Integer.toHexString(b & 0x0f));
                }
            } catch (UnsupportedEncodingException e) {
                for (byte b : md.digest(buffer.toString().getBytes())) {
                    result.append(Integer.toHexString((b & 0xf0) >>> 4));
                    result.append(Integer.toHexString(b & 0x0f));
                }
            }
            return result.toString();
        } catch (java.security.NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return null;
    }

}
