package com.aidex.common.gps.common;

import com.aidex.common.constant.Constants;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.framework.cache.ConfigUtils;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class GeneratorGspApi {

    @Autowired
    private RedisCache redisCache;
    private final String apiHost = ConfigUtils.getConfigByKey("sys.gps.api.url").getConfigValue();

    // 获取当前坐标
    private final String server = "/GetDataService.aspx";
    private final String mapServer = "/RouterPass.ashx";

    public String GenerationMapUrl(String mapType,String custName,String objectId,String userId){
        String mds = redisCache.getStringValue(Constants.GPS_TOKEN_PREFIX);
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("mds",mds);
        parmas.put("mapType",mapType);
        parmas.put("custname",custName);
        parmas.put("objectid",objectId);
        parmas.put("lang","cn");
        parmas.put("requestSource","web");
        parmas.put("custid",userId);
        parmas.put("school_id",userId);
        parmas.put("loginUrl","");
        parmas.put("r",System.currentTimeMillis());

        StringBuilder builder = new StringBuilder("https://www.18gps.net/user/playback.html");
        builder.append("?");
        builder.append(GeneratorParams(parmas));
        return builder.toString();
    }

    public Map<String, Object> GenerationParams(String method,String api){
        String mds = redisCache.getStringValue(Constants.GPS_TOKEN_PREFIX);
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("method",method);
        parmas.put("w",api);
        parmas.put("mds",mds);
        return parmas;
    }

    public String GeneratorApi(){
        StringBuilder builder = new StringBuilder(apiHost);
        builder.append(server);
        return builder.toString();
    }

    public Map<String, Object> GeneratorQueryParams(String api){
        return GenerationParams(RquestMethod.query,api);
    }

    public Map<String, Object> GeneratorSignParams(String api){
        return GenerationParams(RquestMethod.sign,api);
    }

    public interface RquestMethod {
        String query = "QueryApi";
        String sign = "SignApi";
        String follow = "Follow";
        String playback = "Playback";
    }

    public interface GpsMapType {
        String GOOGLECN = "GOOGLECN";
        String BAIDU = "BAIDU";
        String GAODE = "GAODE";
        String QQ = "QQ";
        String DEFAULT = "";

    }


    public static String GeneratorParams(Map<String, Object> params){
        // TODO 如果要编码的话自己加下编码逻辑
        return Joiner.on("&")
                // 用指定符号代替空值,key 或者value 为null都会被替换
                .useForNull("")
                .withKeyValueSeparator("=")
                .join(params);
    }

}
