#!/usr/bin/env python3

import argparse

from PW_explorer.run_clingo import *
from PW_explorer.pwe_helper import get_asp_output_folder, get_asp_input_folder, preprocess_clingo_output, set_current_project_name
import subprocess32 as subprocess


def __main__():

    parser = argparse.ArgumentParser()
    parser.add_argument("fnames", type=str, help="provide the clingo files", nargs='+')
    parser.add_argument("project_name", type=str,
                        help='provide a suitable session/project name to reference these results in future scripts')
    parser.add_argument("-n", "--num_solutions", type=int, default=0,
                        help="number of solutions to generate using clingo, optional, generates all by default")
    args = parser.parse_args()

    fnames = args.fnames
    project_name = args.project_name

    for fname in fnames:
        if not os.path.exists(fname):
            print("Could not find the file {}".format(str(fname)))
            exit(1)

    clingo_in_files = []

    for i, fname in enumerate(fnames):
        clingo_in_files.append(
            get_asp_input_folder(project_name) + '/{}{}.lp4'.format(project_name, ('_' + str(i)) if len(fnames) > 1 else ''))
        subprocess.check_call(['cp', fname, clingo_in_files[-1]])
    print("Copied files into {}".format(get_asp_input_folder(project_name)))

    clingo_output = get_clingo_output(clingo_in_files, args.num_solutions)
    preprocessed_clingo_output = preprocess_clingo_output(clingo_output)

    temp_data = open(get_asp_output_folder(project_name) + '/{}.txt'.format(project_name), 'w')
    temp_data.writelines('\n'.join(preprocessed_clingo_output))
    print("Preprocessed clingo output written to {}".format(get_asp_output_folder(project_name)))

    temp_data.close()
    set_current_project_name(project_name)


if __name__ == '__main__':
    __main__()