package cn.com.qmth.stmms.biz.api.auth.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.com.qmth.stmms.biz.api.auth.model.AuthInfo;
import cn.com.qmth.stmms.biz.api.auth.model.BasicAuthInfo;

@Service("authInfoService")
public class AuthInfoService {

    private static final String ENTRY_SPLITE = ";";

    private static final String KV_SPLITE = "=";

    public AuthInfo getAuthInfo(String infoString) {
        Map<String, String> infoMap = buildInfoMap(infoString);
        BasicAuthInfo info = new BasicAuthInfo();
        info.setLoginName(infoMap.get("loginname"));
        info.setPassword(infoMap.get("password"));
        return info;
    }

    private Map<String, String> buildInfoMap(String infoString) {
        Map<String, String> infoMap = new HashMap<String, String>();
        String[] values = StringUtils.split(infoString, ENTRY_SPLITE);
        if (values != null && values.length > 0) {
            for (String value : values) {
                String[] pair = StringUtils.split(value, KV_SPLITE);
                if (pair != null && pair.length == 2) {
                    infoMap.put(pair[0].trim(), pair[1].trim());
                }
            }
        }
        return infoMap;
    }
}
