#!/usr/bin/env python3

# This script replaces in place.

import os
from glob import glob
import fileinput
import re
import sys

import variables

path = os.path.join("sugarcube","src")
filesNames = [y for x in os.walk(path) for y in glob(os.path.join(x[0], '*.tw'))]
#filesNames = ["sugarcube/src/sidebar/Sidebar.tw"]


for filesName in filesNames:
    print(filesName)
    for line in fileinput.input(filesName, inplace=True, encoding="utf8"):
        output = line
        for variable_replacement in variables.variable_replacements:
            if len(variable_replacement) > 2:
                if variable_replacement[2] == 1:
                    output = re.sub(variable_replacement[0],variable_replacement[1],output)
                else:
                    output = output.replace(variable_replacement[0],variable_replacement[1])
            else:
                output = output.replace(variable_replacement[0],variable_replacement[1])
                output = output.replace('getvar("'+variable_replacement[1]+'")',variable_replacement[1])


        for quest_variable in variables.quest_variables:
            output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".set('"+quest_variable[2]+r"',\g<value>)>>",output)
            output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*\+=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".inc('"+quest_variable[2]+r"',\g<value>)>>",output)
            output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*-=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".dec('"+quest_variable[2]+r"',\g<value>)>>",output)
            output = re.sub(r"(?:getvar\(\")?"+quest_variable[0]+r"(?:\"\))?","$q."+quest_variable[1]+"?.get('"+quest_variable[2]+"')",output)

        for inventory_variable in variables.inventory_variables:
            output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run $inventory.set('{inventory_variable[1]}',\1)>>",output)
            output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
            output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
            output = re.sub(rf"<<{inventory_variable[0]}>>",rf"""$inventory.get("{inventory_variable[1]}")""",output)
            output = re.sub(rf"""getvar\("\${inventory_variable[0]}"\)""",rf"""$inventory.get("{inventory_variable[1]}")""",output)
            output = re.sub(rf"""\${inventory_variable[0]}([^\w])""",rf"""$inventory.get("{inventory_variable[1]}")\1""",output)

        for get_set_variable in variables.get_set_variables:
            output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run {get_set_variable[2]}>>",output)
            if(len(get_set_variable) == 5):
                output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run {get_set_variable[3]}>>",output)
                output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\-=\s*(.*?)\s*>>",rf"<<run {get_set_variable[4]}>>",output)
            #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
            #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
            output = re.sub(rf"<<{get_set_variable[0]}>>",get_set_variable[1],output)
            output = re.sub(rf"""getvar\("{get_set_variable[0]}"\)""",get_set_variable[1],output)
            output = re.sub(rf"""{get_set_variable[0]}(?P<stuffToTheRight>[^\w])""",rf"""{get_set_variable[1]}\g<stuffToTheRight>""",output)

        for pgsvr in  variables.post_get_set_variables_replacements:
            output = output.replace(pgsvr[0],pgsvr[1])

        print(output, end='')


#print(result)
