import os
import glob

f1 = open('mod.ini', 'w', encoding='UTF-8')
f1.write('namespace = Prevent Transparency\n\n')
f1.write('[KeyCensor]\n')
f1.write('condition = $\\WWMIv1\\enable_mods\n')
f1.write('key = LALT s\n')
f1.write('type = cycle\n')
f1.write('$censor = 0,1\n\n')
f2 = open('Constants.ini', 'w')
f2.write('namespace = Prevent Transparency\n\n')
f2.write('[Constants]\n')
f2.write('persist global $censor = 1\n')
f3 = open('.\\Menu\\MenuItem.ini', 'w')
f3.write('namespace = Prevent Transparency\n')
c = 0
x = 0
y = 0
#x87 = 0.03
#y87 = 0.0533
x87 = 0.04
y87 = 0.0711
xmax = 7
ymax = 9
f4 = open('.\\Menu\\MenuCommand.ini', 'w')
f4.write('namespace = Prevent Transparency\n\n')
f4.write('[CommandListSelecter]\n')
#f4.write('ps-t100 = ResourceMenuBackGround\n')
#f4.write('x87 = ({} * {})\n'.format(x87,xmax))
#f4.write('y87 = ({} * {})\n'.format(y87,ymax))
#f4.write('z87 = 0.2\n')
#f4.write('w87 = 0.3\n')
#f4.write('run = CustomShaderElement\n')
f4.write('x87 = {}\n'.format(x87))
f4.write('y87 = {}\n'.format(y87))
f4.write('x255 = 0\n')
f4.write('y255 = 1\n')
f4.write('z255 = 1\n')
f4.write('w255 = 1\n')
f5 = open('.\\Menu\\TextureOverride.ini', 'w')
f5.write('namespace = Prevent Transparency\n')
f6 = open('.\\Menu\\Present.ini', 'w')
f6.write('namespace = Prevent Transparency\n\n')
f6.write('[Present]\n')
f6.write('x219 = $censor * $upgrade\n')

#files = glob.glob('.\\Shaders\\**\\*.bin', recursive=True)
#for file in files:
#    os.remove(file)

listChar = ['Aemeath' ,'AemeathMecha','Augusta' ,'AugustaHeadAccessary','Baizhi'  ,'Buling'  ,'Camellya','Cantarella','Carlotta','CarlottaHair','CarlottaCrystalHair','Cartethyia','Changli' ,'Chisa'   ,'Chixia'  ,'Ciaccona','Danjin'  ,'Encore'  ,'Fluerdelys','Galbrena','Iuno'    ,'Jianxin' ,'Jinhsi'  ,'Lumi'    ,'Lupa'    ,'Lynae'   ,'Mornye'  ,'Phoebe'  ,'Phrolova','Roccia'  ,'Rover'   ,'RoverSkin','Sanhua'  ,'SanhuaSkin','Shorekeeper','Sigrika' ,'Taoqi'   ,'Verina'  ,'Yangyang','Yinlin'  ,'Youhu'   ,'Zani'                      ,'Zhezhi'  ,'NPC_Dhalifa','NPC_Margherita','NPC_NANA','NPC_Rosemary','NPC_Zahira','NPC','Gourd','Outline']
listHash = ['2738a0f0','ddc1947c'    ,'c7aae00c','c3d13594'            ,'3cb91e7f','3acffb93','7748c1d8','5432f939'  ,'4ba716ae','15885f04'    ,'873457db'           ,'a48fddd1'  ,'d14bed8b','afa1587c','1606ae41','e57c9380','6aefd9b3','592add5c','2416ec07'  ,'3765f0a6','f8b55eeb','9b60ec42','243b7e59','b441b9f6','5b33088a','0c33d628','691e4f5f','026f7616','2819cef7','8d97c1bd','4d3d06a6','e8b0d332' ,'33e4890f','b101dcf3'  ,'0266ab77'   ,'3ddf792b','e2685889','82aa82e1','bb5dfeaf','0b8b7323','03eea2d0','616f86d7:c99c5635:dbc0a3d1','b4525ff8','c659e0fc'   ,'2e0fe95b'      ,'741af53f','d395a404'    ,'a4ca8ca6'  ,''   ,''     ,'-']
for itemChar, itemHash in zip(listChar, listHash):
    i = 0
    files = glob.glob('.\\Shaders\\{}\\**\\*.txt'.format(itemChar), recursive=True)
    for file in files:
        if 'disabled' not in file.lower():
            i += 1
            f1.write('[ShaderOverride{}{}]\n'.format(itemChar, i))
            f1.write('hash = {}\n'.format(os.path.basename(file).split('-')[0]))
            f1.write('allow_duplicate_hash = overrule\n')
            if os.path.basename(file).split('-')[1] == 'OL':
                f1.write('if $\\WWMIv1\\enable_mods && $' + itemChar + '_Active && $Outline_Toggle && $' + itemChar + '_Toggle > 0\n')
            elif itemChar.startswith('NPC_'):
                f1.write('if $\\WWMIv1\\enable_mods && $' + itemChar + '_Active && $NPC_Toggle > 0\n')
            else:
                f1.write('if $\\WWMIv1\\enable_mods && $' + itemChar + '_Active && $' + itemChar + '_Toggle > 0\n')
            f1.write('\trun = CustomShader{}{}\n'.format(itemChar, i))
            f1.write('endif\n\n')
            f1.write('[CustomShader{}{}]\n'.format(itemChar, i))
            if os.path.basename(file).split('-')[1] != 'OL':
                f1.write('cull = none\n')
            #f1.write('checktextureoverride = vb0\n')
            if os.path.basename(file).split('-')[1] == 'OL':
                f1.write('if $Outline_Toggle == 1\n')
                f1.write('\trun = CommandList\\WWMIv1\\FireCallbacks\n')
                f1.write('endif\n')
            elif itemChar.startswith('NPC_'):
                f1.write('if $NPC_Toggle == 2\n')
                f1.write('\trun = CommandList\\WWMIv1\\FireCallbacks\n')
                f1.write('endif\n')
            else:
                f1.write('if $' + itemChar + '_Toggle == 2\n')
                f1.write('\trun = CommandList\\WWMIv1\\FireCallbacks\n')
                f1.write('endif\n')
            f1.write('ps = {}\n'.format(file))
            if os.path.basename(file).split('-')[1] == 'AD':
                f1.write('handling = skip\n')
                f1.write('draw = from_caller\n\n')
            elif itemChar.startswith('NPC_'):
                f1.write('if $NPC_Toggle == 1\n')
                f1.write('\thandling = skip\n')
                f1.write('\tdraw = from_caller\n')
                f1.write('endif\n\n')
            else:
                f1.write('if $' + itemChar + '_Toggle == 1\n')
                f1.write('\thandling = skip\n')
                f1.write('\tdraw = from_caller\n')
                f1.write('endif\n\n')
    if (i > 0 or itemChar == 'Outline'):
        if ':' in itemHash:
            f2.write('global $' + itemChar + '_Active = 0\n')
            f2.write('persist global $' + itemChar + '_Toggle = 2\n')
            OtherHashCount = 0
            for OtherHash in itemHash.split(':'):
                OtherHashCount += 1
                f5.write('\n[TextureOverride' + itemChar + '{}]\n'.format(OtherHashCount))
                f5.write('hash = {}\n'.format(OtherHash))
                f5.write('match_priority = 0\n')
                f5.write('$' + itemChar + '_Active = 1\n')
            f6.write('post $' + itemChar + '_Active = 0\n')
        elif itemHash == '':
            f2.write('global $' + itemChar + '_Active = 1\n')
            f2.write('persist global $' + itemChar + '_Toggle = 1\n')
        elif itemChar == 'Outline':
            f2.write('persist global $' + itemChar + '_Toggle = 0\n')
        else:
            f2.write('global $' + itemChar + '_Active = 0\n')
            f2.write('persist global $' + itemChar + '_Toggle = 2\n')
            f5.write('\n[TextureOverride' + itemChar + ']\n')
            f5.write('hash = {}\n'.format(itemHash))
            f5.write('match_priority = 0\n')
            f5.write('$' + itemChar + '_Active = 1\n')
            f6.write('post $' + itemChar + '_Active = 0\n')
        if not itemChar.startswith('NPC_'):
            f3.write('\n[CommandListButton' + itemChar + ']\n')
            f3.write('if $toggle\n')
            if itemChar == 'Outline':
                f3.write('\t$' + itemChar + '_Toggle = 1 - $' + itemChar + '_Toggle\n')
            else:
                f3.write('\t$' + itemChar + '_Toggle = ($' + itemChar + '_Toggle + 1) % 3\n')
            f3.write('\t$click = 0\n')
            f3.write('endif\n')
            f3.write('\n[ResourceButton' + itemChar + ']\n')
            f3.write('filename = .\\MenuResource\\' + itemChar + '.png\n')
            f4.write('z87 = 0.16 + ({} * {})\n'.format(x87,x))
            f4.write('w87 = 0.2 + ({} * {})\n'.format(y87,y))
            f4.write('if $' + itemChar + '_Toggle > 0\n')
            f4.write('\tps-t100 = ResourceButtonON\n')
            if itemChar == 'Outline':
                f4.write('\tx255 = (180 - ($' + itemChar + '_Toggle * (180 / (2 - 1)))) / 360\n')
            else:
                f4.write('\tx255 = (180 - ($' + itemChar + '_Toggle * (180 / (3 - 1)))) / 360\n')
            f4.write('\trun = CustomShaderColor\n')
            f4.write('else\n')
            f4.write('\tps-t100 = ResourceButtonOFF\n')
            f4.write('\trun = CustomShaderElement\n')
            f4.write('endif\n')
            f4.write('run = CommandListJudge\n')
            f4.write('ps-t100 = ResourceButton' + itemChar + '\n')
            f4.write('run = CommandListButton' + itemChar + '\n')
            f4.write('run = CustomShaderElement\n')
            c += 1
            x, y = c % xmax, c // xmax

f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
