import json
import os

def calculate_repeat_counts(strings, step=2):
    """
    Calculates the repeat counts for each string based on a given step size.

    Args:
        strings (list): The list of strings.
        step (int): The number of letters to add at a time.

    Returns:
        list: The calculated repeat counts for each string.
    """
    return [max(1, (len(s) + step - 1) // step) for s in strings]

def get_key_lengths(keys, strings, step=2):
    """
    Returns a dictionary in the format {key: length-1} for each key.
    This function calculates the same way as the repeat counts, but subtracts 1 from each length.

    Args:
        keys (list): The list of keys (without prefix).
        strings (list): The list of strings.
        step (int): The number of letters to add at a time.

    Returns:
        dict: A dictionary with keys and adjusted lengths.
    """
    key_lengths = {}
    for key, s in zip(keys, strings):
        length = len(s)
        adjusted_length = max(1, (length + step - 1) // step) - 1  # Adjusted for step size and minus 1
        key_lengths[key] = adjusted_length
    
    return key_lengths

def main():
    # Get the directory where the script is located
    script_dir = os.path.dirname(os.path.abspath(__file__))

    # Navigate to the parent folder
    parent_dir = os.path.dirname(script_dir)

    # Define the path to the language file
    lang_file = os.path.join(parent_dir, "en_us.json")

    # Load the language file
    with open(lang_file, "r") as file:
        data = json.load(file)
    
    # Define input keys
    input_keys = [
        "scientist.hub.0",
        "scientist.hub.1",
        "scientist.hub.2",
        "scientist.hub.3",
        "scientist.hub.4",
        "scientist.trial.0",
        "scientist.trial.1",
        "scientist.trial.2",
        "scientist.victory.0",
        "scientist.victory.1",
        "scientist.victory.2",
        "scientist.victory.3",
        "scientist.victory.4"
    ]
    
    # Fetch the strings from the JSON file
    input_strings = [data.get("bungeon.dialogue." + key, "") for key in input_keys]
    
    # Calculate repeat counts based on the string lengths
    repeat_counts = calculate_repeat_counts(input_strings)
    
    # Print the repeat counts
    print("\n\nRepeat counts:", repeat_counts)

    # Print a dictionary of keys and their corresponding string lengths (adjusted for step and minus 1)
    key_lengths = get_key_lengths(input_keys, input_strings)
    print("\n\nKey Lengths:", key_lengths)

if __name__ == "__main__":
    main()