#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (c) Contributors to the Open 3D Engine Project.
For complete copyright and license terms please see the LICENSE at the root of this distribution.

SPDX-License-Identifier: Apache-2.0 OR MIT
"""
import sys
import os
import importlib
from argparse import ArgumentParser

if __name__ == "__main__":
    os.system('') # activate VT100 mode for windows console
    parser = ArgumentParser()
    parser.add_argument(
        '--compiler', dest='compiler',
        type=str,
        help='the path to the compiler exe',
        default="../bin/win_x64/Release/azslc.exe"
    )
    args = parser.parse_args()
    exe = args.compiler

    if not os.path.exists(exe):
        print("error: no exe found at", exe, " please pass a path on the command line")
        exit(1)

    sys.path.append("../tests")
    testFuncs = importlib.import_module("testfuncs")

    keywords = testFuncs.dumpKeywords(exe)
    if not keywords[1]:
        sys.exit(1)

    with open("AzslcPredefinedTypes.h", 'w') as outfile:
        copyright = '''/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once

// THIS FILE IS AUTO-GENERATED BY exportKeywords.py

#include <array>
#include <tuple>

namespace AZ::ShaderCompiler::Predefined
{
'''
        outfile.write(copyright)
        for categ, words in keywords[0].items():
            outfile.write("static constexpr std::array<const char*, " + str(len(words)) + "> ");
            outfile.write(categ)
            outfile.write(" = {\n")
            outfile.write(',\n'.join(['"' + w + '"' for w in words]))
            outfile.write("};\n\n")

        outfile.write("template<size_t N> struct Bag { const char* m_name = nullptr; const std::array<const char*, N> m_bag; };\n\n");
        tupdecl = "static constexpr auto All = std::make_tuple("
        outfile.write(tupdecl);
        whitespaces = " " * len(tupdecl)
        outfile.write((",\n" + whitespaces).join(['Bag<' + str(len(val)) + '>{"' + key + '", ' + key + '}' for key, val in keywords[0].items()]))
        outfile.write(");\n\n");

        outfile.write("};\n")  # end namespace