# Path: PW_explorer/helper.py
# def preprocess_clingo_output(clingo_raw_output: list):
# 
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
#             break
# 
#     return clingo_raw_output[start:]
# 
# Path: PW_explorer/meta_data_parser.py
# def parse_pwe_meta_data(asp_rules, silent=False, print_parse_tree=False):
#     if isinstance(asp_rules, str):
#         asp_rules = asp_rules.splitlines()
#     md_comments = preprocess(asp_rules)
#     md_comments = "\n".join(md_comments)
#     parsed_meta_data = parse_meta_data_from_string(md_comments, silent, print_parse_tree)
#     return parsed_meta_data


import os
import subprocess as subprocess
from .helper import (
    preprocess_clingo_output,
)
from .meta_data_parser import parse_pwe_meta_data
    # cling_out_lines = [l.decode('utf-8') for l in cling_out_lines]

    # meta_data = {
    #     'attr_defs': attribute_defs,
    #     'temporal_decs': temporal_decs,
    # }

    return cling_out_lines, meta_data


def run_clingo(clingo_rules, num_solutions: int=0, show_warnings=False, other_args: list=None):
    """
    :param clingo_rules: string or list of strings
    :param num_solutions:
    :param show_warnings: Default: False
    :param other_args: Other arguments to pass to dlv. Provide a list of strings eg. ['-n=1', '-N=10', '-silent']
    :return:
    """

    if isinstance(clingo_rules, str):
        clingo_rules = clingo_rules.splitlines()

    dummy_fname = 'svjsihkankjbyerhoihsyvgjnclsdihcysbfhcbygweincbsydgibwyebcsygdyc.lp4'
    with open(dummy_fname, 'w') as f:
        f.write('\n'.join(clingo_rules))

    clingo_output, meta_data = get_clingo_output([dummy_fname], num_solutions=num_solutions,
                                                 show_warnings=show_warnings, other_args=other_args)
    os.remove(dummy_fname)


# Path: PW_explorer/helper.py
# def preprocess_clingo_output(clingo_raw_output: list):
# 
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
#             break
# 
#     return clingo_raw_output[start:]
# 
# Path: PW_explorer/meta_data_parser.py
# def parse_pwe_meta_data(asp_rules, silent=False, print_parse_tree=False):
#     if isinstance(asp_rules, str):
#         asp_rules = asp_rules.splitlines()
#     md_comments = preprocess(asp_rules)
#     md_comments = "\n".join(md_comments)
#     parsed_meta_data = parse_meta_data_from_string(md_comments, silent, print_parse_tree)
#     return parsed_meta_data


# Path: PW_explorer/run_clingo.py
import os
import subprocess as subprocess
from .helper import (
    preprocess_clingo_output,
)
from .meta_data_parser import parse_pwe_meta_data

    # cling_out_lines = [l.decode('utf-8') for l in cling_out_lines]

    # meta_data = {
    #     'attr_defs': attribute_defs,
    #     'temporal_decs': temporal_decs,
    # }

    return cling_out_lines, meta_data


def run_clingo(clingo_rules, num_solutions: int=0, show_warnings=False, other_args: list=None):
    """
    :param clingo_rules: string or list of strings
    :param num_solutions:
    :param show_warnings: Default: False
    :param other_args: Other arguments to pass to dlv. Provide a list of strings eg. ['-n=1', '-N=10', '-silent']
    :return:
    """

    if isinstance(clingo_rules, str):
        clingo_rules = clingo_rules.splitlines()

    dummy_fname = 'svjsihkankjbyerhoihsyvgjnclsdihcysbfhcbygweincbsydgibwyebcsygdyc.lp4'
    with open(dummy_fname, 'w') as f:
        f.write('\n'.join(clingo_rules))

    clingo_output, meta_data = get_clingo_output([dummy_fname], num_solutions=num_solutions,
                                                 show_warnings=show_warnings, other_args=other_args)
    os.remove(dummy_fname)


    return preprocess_clingo_output(clingo_output), meta_data