#!/usr/bin/python
#
# Copyright 2015 Michael Sparks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import pprint
import sys
import os

from pyxie.core import parse_file
from pyxie.core import analyse_file
from pyxie.core import compile_file
from pyxie.core import codegen_phase
from pyxie.core import compilation_tests
from pyxie.core import compile_testfile
from pyxie.core import parsing_tests

from pyxie.core import testprogs_dir

# This is to support compilation profiles. This may or may not turn out to
# be a good approach.  (#3/#3.x)
profile = "default"

class BadArguments(Exception):
    pass

def handle_tests(argv):
    if argv[2] == "run-tests":
        parsing_tests()
        compilation_tests(profile)
        return

    elif argv[2] == "parse-tests":
        parsing_tests()
        return

    elif argv[2] == "compile-tests":
        compilation_tests(profile)
        return

    elif argv[2] == "parse" and len(argv) == 4:
        filename = argv[3]
        AST = parse_testfile(testprogs_dir, filename)
        pprint.pprint(AST)
        pprint.pprint(AST.__json__())
        return

    elif argv[2] == "compile" and len(argv) == 4:
        filename = argv[3]
        compile_testfile(testprogs_dir, filename, profile)
        return
    else:
        raise BadArguments("Bad Arguments")


def parse_option(argv):
    filename = argv[2]
    AST = parse_file(filename)
    pprint.pprint(AST)
    pprint.pprint(AST.__json__())
    if 0:
        import json
        print "Also writing a copy to ", filename+".json"
        f = open(filename+".json", "w")
        f.write(json.dumps(AST.__json__()))
        f.close()


def analyse_option(argv):
    filename = argv[2]
    analyse_file(filename)


def codegen_option(argv):
    filename = argv[2]
    result_filename = argv[3] if len(argv) == 4 else None
    codegen_phase(filename, result_filename, profile)


def compile_option(argv):
    filename = argv[2]
    result_filename = argv[3] if len(argv) == 4 else None
    compile_file(filename, profile, result_filename)


def main(argv):
    global profile
    if len(argv) < 2:
        raise BadArguments()

    # If a profile is pulled defined, use that
    if argv[1] == "--profile" and len(argv)> 2:
        profile = argv[2]
        argv[1:3] = []

    if argv[1] == "--test":
        handle_tests(argv)

    elif argv[1] == "parse" and len(argv) == 3:
        parse_option(argv)

    elif argv[1] == "analyse" and len(argv) == 3:
        analyse_option(argv)

    elif argv[1] == "codegen" and len(argv) == 3 or len(argv) == 4:
        codegen_option(argv)

    elif argv[1] == "compile" and len(argv) == 3 or len(argv) == 4:
        compile_option(argv)

    else:
        raise BadArguments()


def show_help():
    print """\npyxie -- A little python compiler\nUsage:\n
    pyxie -- show runtime arguments
    pyxie --test run-tests -- Run all tests
    pyxie --test parse-tests -- Just run parse tests
    pyxie --test compile-tests -- Just run compile tests
    pyxie --test parse filename -- Parses a given test given a certain filename
    pyxie parse filename -- parses the given filename, outputs result to console
    pyxie analyse filename -- parses and analyse the given filename, outputs result to console
    pyxie codegen filename -- parses, analyse and generate code for the given filename, outputs result to console. Does not attempt compiling
    pyxie [--profile arduino] compile path/to/filename.suffix -- compiles the given file to path/to/filename
    pyxie [--profile arduino] compile path/to/filename.suffix  path/to/other/filename -- compiles the given file to the destination filename
"""


if __name__ == "__main__":
    try:
        main(argv = sys.argv[:])
    except BadArguments:
        show_help()

