/* GenesisChess, an Android chess application
 * Copyright 2022, Justin Madru (justin.jdm64@gmail.com)
 *
 * 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
 *
 * http://apache.org/licenses/LICENSE-2.0
 *
 * 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.chess.genesis.util;

import java.security.*;
import java.util.concurrent.*;
import android.os.*;
import androidx.compose.runtime.*;

public class Util
{
	private final static String SUID_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~";

	private Util() {}

	public static void runThread(Runnable runner)
	{
		Executors.newSingleThreadExecutor().execute(runner);
	}

	public static void runUI(Runnable runner)
	{
		new Handler(Looper.getMainLooper()).post(runner);
	}

	public static <T> MutableState<T> getState(T val)
	{
		return SnapshotStateKt.mutableStateOf(val, SnapshotStateKt.structuralEqualityPolicy());
	}

	public static String getSUID()
	{
		var rand = new SecureRandom();
		var buff = new StringBuilder();

		buff.append(randChar(rand, true));
		for (int i = 0; i < 6; i++) {
			buff.append(randChar(rand, false));
		}
		buff.append(randChar(rand, true));
		return buff.toString();
	}

	private static char randChar(SecureRandom rand, boolean onlyAlphNum)
	{
		char c;
		do {
			c = SUID_CHARS.charAt(Math.abs(rand.nextInt() % SUID_CHARS.length()));
		} while (onlyAlphNum && !Character.isLetterOrDigit(c));
		return c;
	}
}

@@ -1412,28 +1412,27 @@
 r();%0A%0A%09%09
-buff.append(
+var last = 
 randChar
@@ -1439,24 +1439,44 @@
 (rand, true)
+;%0A%09%09buff.append(last
 );%0A%09%09for (in
@@ -1501,28 +1501,23 @@
 +) %7B%0A%09%09%09
-buff.append(
+last = 
 randChar
@@ -1527,14 +1527,62 @@
 nd, 
-false)
+!Character.isLetterOrDigit(last));%0A%09%09%09buff.append(last
 );%0A%09

don't generate two non-alphanum characters together for SUID