/*******************************************************************************
 * Copyright (c) 2024-2025 R3944Realms. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 *     http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.r3944realms.dg_lab.utils.stringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The type String handler util.
 */
public class StringHandlerUtil {
    /**
     * Gets char for string.
     *
     * @param str   the str
     * @param index the index
     * @return the char for string
     */
    public static char getCharForString(String str, int index) {
        return str.charAt(index);
    }

    /**
     * Build web socket url string.
     *
     * @param address the address
     * @param port    the port
     * @param ssl     the ssl
     * @return the string
     */
    public static String buildWebSocketURL(String address, int port, boolean ssl) {
        String scheme = ssl ? "wss" : "ws";
        return String.format("%s://%s:%d/", scheme, address, port).replace("%","%25");
    }

    /**
     * Add quotes string [ ].
     *
     * @param arr the arr
     * @return the string [ ]
     */
    public static String[] addQuotes(String[] arr) {
        String[] newArr = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = "\"" + arr[i] + "\"";
        }
        return newArr;
    }

    /**
     * Remove quotes string [ ].
     *
     * @param arr the arr
     * @return the string [ ]
     */
    public static String[] removeQuotes(String[] arr) {
        String[] newArr = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i].substring(1, arr[i].length() - 1);
        }
        return newArr;
    }

    /**
     * 将String[] 转化为 <code>["xxxxxxxxxxxxxxxx",......,"xxxxxxxxxxxxxxxx"]</code><br/><br/>
     * <b>例 :</b><code>[ff00ff, ac013021, 23990123]</code> -> <code>["ff00ff","ac013021","23990123"]</code>
     *
     * @param waveDataList the wave data list
     * @return the string
     */
    public static String reformWaveDataList(String[] waveDataList) {
        List<String> b = new ArrayList<>(Arrays.asList(addQuotes(waveDataList)));
        return b.toString().replace(" ","");//去除空格
    }
}
