

def loadRess(path):
    f = open(path).read()

    b = f.find('{')
    l = f.rfind('}')
    j = f[b:l+1]

    import re
    rs = re.findall(r"'(.+)': \{\n *'en': '(.+)',\n *'fr': '(.+)'",j)
    print(path,"found", len(rs), "linguistic values")
    return rs

def findMismatch(rs):
    rr = ''
    for r in rs:
        if r[0] == r[1]:
            print('.',end='')
            continue
        if r[0] == r[2]:
            print('.',end='')
            continue
        print('+',end='')
        rr = rr + r[0] + '\n'
        yield r[0]

    ff = open('lang-assets_reference_mismatch.txt','w')
    ff.write(rr)

def recursiveSearch(directory, patterns=["*.html","*.sql"]):
    from pathlib import Path
    result = []
    for pattern in patterns:
        result += list(Path(directory).rglob(pattern))
    result.sort()
    print("found",len(result),"paths in",directory,patterns)
    for path in result:
        yield str(path)

def yieldLines(directory, patterns=["*.html","*.sql"]):
    result = recursiveSearch(directory, patterns)
    for path in result:
        fil = open(path).read()
        n = 0
        for line in fil.split('\n'):
            n = n + 1
            yield line,n,str(path)


def findoccurrence(ress,lines):
    def exemption(line):
        if 'label' in line:
            return False
        exs = [
            'name=',
            'memberName',
            '"type":',
            '"Identifier":',
            '"value":',
            '"type":',
            '"name":',
            'choiceValue=',
            '[ModelData]',
            '[FormOptions]',
            'ngModel',
            'onValueChange',
        ]
        for ex in exs:
            if ex in line:
                return True
        return False 
    def getTranslation(r,path):
        if 'FH' in path:
            return r[1]
        return r[2]
    def findKeyMatch(line,n,path):
        for r in ress:
            trans = getTranslation(r,path)
            if r[0].strip() == trans.strip():
                continue
            if trans.strip() in line:
                continue
            # if r[0].strip().lower() in line.lower():
                # yield path, n, r, line
            # if 'label="'+r[0].strip().lower()+'"' in line.lower():
            #     yield path, n, r, line
            elif "'"+r[0].strip().lower()+"'" in line.lower():
                yield path, n, r, line
            elif '"'+r[0].strip().lower()+'"' in line.lower():
                yield path, n, r, line
            elif '>'+r[0].strip().lower()+'<' in line.lower():
                yield path, n, r, line
            # elif ' '+r[0].strip().lower()+' ' in line.lower():
                # yield path, n, r, line
    for line,n,path in lines:
        if 'Global' in path:
            continue
        if exemption(line):
            continue
        for match in findKeyMatch(line,n,path):
            yield match


if __name__ == '__main__':

    # lines = yieldLines('lumed-clients-assets/clients/FH')
    lines = yieldLines('lumed-clients-assets')

    ress = loadRess('formModule/projects/forms-module/src/lib/Translations/lang-clients-assets.ts')
    # ress = loadRess('formModule/projects/forms-module/src/lib/Translations/lang.ts')
    # ress = loadRess('ZincModule\projects\zinc-module\src\lib\FormEditor\Translations\lang.ts')
    # ress = loadRess('ZincModule\projects\zinc-module\src\lib\Translations\lang.ts')

    found = findoccurrence(ress, lines)

    ff = open('occurences.txt','w')
    lastPath = ''
    for path,n,r, line in found:
        if lastPath != path:
            print(path)
            ff.write('\n'+path + '\n')
            lastPath = path
        ff.write('    line '+str(n)+' '+line.strip()+'\n')
        ff.write('          => ['+ r[0]+ '] ['+ r[1]+ '] ['+ r[2]+ ']\n')


    # flr = open('allLinguisticRessources.txt','w')
    # print('linguistic ressources')
    # for path in recursiveSearch('.',["lang*.ts"]):
    #     if not 'node_module' in path:
    #         flr.write(path+'\n')


