package com.yihu.hos.web.framework.util.sql;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.yihu.hos.web.framework.constant.ErrorCode;
import com.yihu.hos.web.framework.exception.ApiException;
import com.yihu.hos.core.datatype.StringUtil;
import org.apache.commons.lang3.StringUtils;    //TODO: Use the hos-core
import org.apache.commons.lang3.math.NumberUtils;

import java.io.IOException;
import java.util.*;

/**
 * @created Airhead 2015/12/29.
 */
public class RequestParamTransformer {

    public static final String SEPARATOR_FIELD = ",";
    public static final String SEPARATOR_KEYVALUE = ":";

    public static Map<String, Object> parseQuery(String query) throws ApiException {
        HashMap<String, Object> queryMap = new HashMap<String, Object>();
        if (StringUtils.isEmpty(query)) {
            return queryMap;
        }

        for (String cond : StringUtils.split(query, SEPARATOR_FIELD)) {
            String[] kv = StringUtils.split(cond, SEPARATOR_KEYVALUE);
            if (kv.length != 2) {
                throw new ApiException(ErrorCode.InvalidParameterFormat);
            }

            String key = kv[0];
            String value = kv[1];
            queryMap.put(key, value);
        }

        return queryMap;
    }

    public static List<String> parseField(String fields) {
        List<String> fieldList = new ArrayList<String>();
        if (StringUtils.isEmpty(fields)) {
            return fieldList;
        }

        String[] filters = StringUtils.split(fields, SEPARATOR_FIELD);
        return Arrays.asList(filters);
    }

    public static List<String> parseSortBy(String sortBy) {
        List<String> sortList = new ArrayList<String>();
        if (StringUtils.isEmpty(sortBy)) {
            return sortList;
        }

        String[] fields = StringUtils.split(sortBy, SEPARATOR_FIELD);
        return Arrays.asList(fields);
    }

    public static List<String> parseOrder(String order) {
        List<String> orderList = new ArrayList<String>();
        if (StringUtils.isEmpty(order)) {
            return orderList;
        }

        String[] fields = StringUtils.split(order, SEPARATOR_FIELD);
        return Arrays.asList(fields);
    }

    public static Long parseLimit(String limit) {
        if (StringUtils.isEmpty(limit)) {
            return -1L;
        }

        return NumberUtils.toLong(limit);
    }

    public static Long parseOffset(String offset) {
        if (StringUtils.isEmpty(offset)) {
            return 0L;
        }

        return NumberUtils.toLong(offset);
    }

    public static Map parseJsonToMap(String condition) {
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (!StringUtil.isEmpty(condition)) {
            try {
                JsonNode jsonNode = objectMapper.readTree(condition);
                Iterator<String> fileNames = jsonNode.fieldNames();
                while (fileNames.hasNext()) {
                    String fieldName = fileNames.next();
                    JsonNode valueNode = jsonNode.get(fieldName);
                    if (valueNode.getNodeType().equals(JsonNodeType.NUMBER)) {
                        map.put(fieldName, jsonNode.get(fieldName).asInt());
                    } else {
                        map.put(fieldName, jsonNode.get(fieldName).asText());
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
