import re
import os
import fileinput

# Define Path
game_location = os.getcwd()

# Content Files
manage_file_list = [
    os.path.join(game_location, '261.js'),
    os.path.join(game_location, '473.js'),
    os.path.join(game_location, '505.js'),
    os.path.join(game_location, '737.js'),
    os.path.join(game_location, '256.js'),
    os.path.join(game_location, '826.js'),
    os.path.join(game_location, 'main.loader.js')
]

content_file_list = [
    os.path.join(game_location, 'Content_CentaurVillage.js'),
    os.path.join(game_location, 'Content_Companions.js'),
    os.path.join(game_location, 'Content_DragonLair.js'),
    os.path.join(game_location, 'Content_DreamPalace.js'),
    os.path.join(game_location, 'Content_Dungeons.js'),
    os.path.join(game_location, 'Content_FireTemple.js'),
    os.path.join(game_location, 'Content_Foothills.js'),
    os.path.join(game_location, 'Content_Forest.js'),
    os.path.join(game_location, 'Content_Frostwood.js'),
    os.path.join(game_location, 'Content_GlacialRift.js'),
    os.path.join(game_location, 'Content_Hawkethorne.js'),
    os.path.join(game_location, 'Content_HornetHive.js'),
    os.path.join(game_location, 'Content_Khorminos.js'),
    os.path.join(game_location, 'Content_KitsuneDen.js'),
    os.path.join(game_location, 'Content_LightPalace.js'),
    os.path.join(game_location, 'Content_MarefolkVillage.js'),
    os.path.join(game_location, 'Content_Misc.js'),
    os.path.join(game_location, 'Content_OrcVillage.js'),
    os.path.join(game_location, 'Content_Other.js'),
    os.path.join(game_location, 'Content_Starhome.js'),
    os.path.join(game_location, 'Content_Undermountain.js'),
    os.path.join(game_location, 'Content_Valley.js'),
    os.path.join(game_location, 'Content_Wayfort.js'),
    os.path.join(game_location, 'Content_WindyPeaks.js'),
    os.path.join(game_location, 'Content_WinterCity.js'),
    os.path.join(game_location, 'manifest.js'),
    # Add more file paths as needed
]

# Clear Hashes
# Rename the specific file
for root, dirs, files in os.walk(game_location):
    for file in files:
        if re.match(r'main\..*\.js', file):
            new_name = os.path.join(root, 'main.loader.js')
            os.rename(os.path.join(root, file), new_name)

# Get all files in the directory and rename them
for root, dirs, files in os.walk(game_location):
    for file in files:
        match = re.match(r'(.*\.)[0-9a-f]{20}(\..*)?$', file)
        if match:
            new_name = os.path.join(root, match.group(1).rstrip('.') + match.group(2))
            os.rename(os.path.join(root, file), new_name)

# Removes hashes in index.html
def update_index_html(game_location):
    import os
    import re
    index_path = os.path.join(game_location, 'index.html')

    with open(index_path, 'r', encoding='utf-8') as file:
        content = file.read()

    content = re.sub(r'\./manifest\.[a-zA-Z0-9]+\.js', './manifest.js', content)
    content = re.sub(r'\./main\.[a-zA-Z0-9]+\.js', './main.loader.js', content)

    with open(index_path, 'w', encoding='utf-8') as file:
        file.write(content)
update_index_html(game_location)


# Remove Hashes for files in the loader
def clean_main_loader_js(game_location):
    import os
    import re
    loader_path = os.path.join(game_location, 'main.loader.js')

    with open(loader_path, 'r', encoding='utf-8') as file:
        content = file.read()

    pattern = r'\}\[e\]\s*\|\|\s*e\)\s*\+\s*"\."\s*\+\s*\{.*?\}\[e\]\s*\+\s*"\.js"'
    replacement = '}[e]||e)+".js"'

    new_content, count = re.subn(pattern, replacement, content, flags=re.DOTALL)

    if count > 0:
        with open(loader_path, 'w', encoding='utf-8') as file:
            file.write(new_content)
    else:
        print("No loader hash matches found.")
clean_main_loader_js(game_location)

# Party Limit
with fileinput.FileInput('505.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'this.members.length>=5',
            'this.members.length>=18'
        ), end='')

# Coin Display Cutoff
with fileinput.FileInput('256.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'this.props.allies.length<5',
            'this.props.allies.length<15'
        ), end='')

# Waystone Selector
with fileinput.FileInput('826.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'DoChangeParty=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2',
            'DoChangeParty=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:14'
        ), end='')

# CSS Modifications
with fileinput.FileInput('manifest.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'n.partyContainer {\\n  height: 95%;\\n  width: 100%;\\n  position: absolute;\\n}',
            'n.partyContainer {\\n  height:95%;\\n  width: 100%;\\n  position: absolute;\\n  margin-left: 2.5%;\\n  display: flex;\\n  flex-wrap: wrap;\\n  align-content: flex-start;\\n}\\n.partyContainer.enemy {\\n  margin-left: 0%;\\n} \\n.partyContainer.enemy .characterCard {\\n  height: 18.5%;\\n  margin: 0 auto;\\n}'
        ), end='')

# Repeat the encoding addition for all other fileinput operations
with fileinput.FileInput('manifest.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'n.characterCard {\\n  position: relative;\\n  width: min-content;\\n  width: -moz-min-content;\\n  height: 18.5%;\\n  margin: 0 auto;\\n  margin-bottom: 1.5%;\\n}',
            'n.characterCard {\\n  position:relative;\\n  width: min-content;\\n  width: -moz-min-content;\\n  height: 10.1%;\\n  margin: 0;\\n  margin-bottom: 0.5%;\\n}'
        ), end='')

with fileinput.FileInput('manifest.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'n.characterCard div .hp {\\n  left: 53%;\\n  color: red;\\n}',
            'n.characterCard div .hp {\\n  left:53%;\\n  color: red;\\n  font-size: 40% !important;\\n  }\\n.partyContainer.enemy .characterCard div .hp {\\n font-size: 70% !important;\\n}'
        ), end='')

with fileinput.FileInput('manifest.js', inplace=True, encoding='utf-8') as file:
    for line in file:
        print(line.replace(
            'n.characterCard div .dots.imagePulse {\\n  animation: pulseDots 1s infinite;\\n  animation-direction: alternate;\\n  animation-timing-function: linear;\\n}',
            'n.partyContainer .characterCard div .dots {\\n font-size:55% !important;\\n}\\n.partyContainer.enemy .characterCard div .dots {\\n font-size:110% !important;\\n}\\n.characterCard div .dots.imagePulse {\\n  animation-direction: alternate;\\n  animation: pulseDots 1s infinite;\\n  animation-timing-function: linear;\\n}'
        ), end='')