#!/usr/bin/env python3
import json
import string
import random
import glob, os
import sys

used_names = []


# rand string for class names
def gen_rand_str():
    rand_str = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=8))
    while rand_str in used_names:
        rand_str = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=8))

    used_names.append(rand_str)
    return rand_str


def get_rand_code():
    indentasi = random.randint(0, 5);
    indentasiStr = ""
    for num in range(0,indentasi):
        indentasiStr +="\t"

    #随机缩进
    num = random.randint(0, 4);
    code = ";\n//-------"+str(num)+"-------\n"
    #随机代码
    if num == 0:
        code += indentasiStr+"String " + gen_rand_str() + "=\"" + gen_rand_str() + "\";\n"
    elif num == 1:
        code += indentasiStr+"int " + gen_rand_str() + "=" + str(random.randint(0, 100000)) + ";\n"
    elif num == 2:
        code += indentasiStr+"float " + gen_rand_str() + "=" + str(random.randint(0, 100000)) + ";\n"
    elif num == 3:
        code += indentasiStr+"double " + gen_rand_str() + "=" + str(random.randint(0, 100000)) + ";\n"
    elif num == 4:
        code += indentasiStr+"long " + gen_rand_str() + "=" + str(random.randint(0, 100000)) + ";\n"
    # elif num == 2:
    #     code += ";\nif(\"" + gen_rand_str() + "\".equals(\"" + gen_rand_str() + "\")){}\n"
    # elif num == 3:
    #     index = gen_rand_str()
    #     code += ";\nfor(int " + index + "="+str(random.randint(0,10))+";"+index+"<"+str(random.randint(11,20))+";"+index+"++){}\n"

    return code


def process_file_content(path, file_name):
    print("processing content: %s..." % path)
    with open(path, "r", encoding="utf-8") as f:
        # readlines以列表的形式将文件读出
        lines = f.readlines()

    with open(path, "w", encoding="utf-8") as f_w:
        for line in lines:
            if ";\n" in line and "return" not in line and "break" not in line and "continue" not in line and "import" not in line and "package" not in line:
                if random.randint(0, 20) % 2 == 0:
                    line = line.replace(";\n", get_rand_code())
            f_w.write(line)
        print("done\n")
    return



# perform content replace for the files
for root, dirs, files in os.walk("../../../app/src/main"):
    directory = os.path.join(os.getcwd(), root)
    for file in files:
        if file.endswith(".java"):
            # get the path of the java file
            path = os.path.join(directory, file)

            process_file_content(path, file)

for root, dirs, files in os.walk("../../../lib_base/src/main"):
    directory = os.path.join(os.getcwd(), root)
    for file in files:
        if file.endswith(".java"):
            # get the path of the java file
            path = os.path.join(directory, file)

            process_file_content(path, file)

print("processing file content done")
