package com.gh.gamecenter.core.utils;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.lightgame.config.CommonDebug;

/**
 * Created by khy on 27/03/18.
 */

public class UrlFilterUtils {

    /**
     * keyName,value,keyName,value......
     * 如果key/value其中一个为空直接忽略这对数据(key/value)
     */
    public static String getFilterQuery(@NonNull String... arrStr) {
        if (CommonDebug.IS_DEBUG && (arrStr.length <= 1 || arrStr.length % 2 != 0)) {
            throw new IllegalStateException("UrlFilterUtils->getFilterQuery");
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < arrStr.length; i++) {
            if (TextUtils.isEmpty(arrStr[i])) continue;
            if (i % 2 == 0 && TextUtils.isEmpty(arrStr[i + 1])) continue;
            if (i % 2 != 0 && TextUtils.isEmpty(arrStr[i - 1])) continue;

            String s = arrStr[i];
            result.append(s);
            if (i % 2 == 0) {
                result.append(":");
            } else if (i != arrStr.length - 1) {
                result.append(",");
            }
        }
        return result.toString();
    }
}
