import asyncio
import os
import tempfile
from contextlib import ExitStack
from typing import Text, Optional, List, Union, Dict

from rasa.importers.importer import TrainingDataImporter
from rasa import model
from rasa.model import FingerprintComparisonResult
from rasa.core.domain import Domain
from rasa.utils.common import TempDirectoryPath

from rasa.cli.utils import (
    print_success,
    print_warning,
    print_error,
    bcolors,
    print_color,
)
from rasa.constants import DEFAULT_MODELS_PATH, DEFAULT_CORE_SUBDIRECTORY_NAME


def train(
    domain: Text,
    config: Text,
    training_files: Union[Text, List[Text]],
    output: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
    additional_arguments: Optional[Dict] = None,
    loop: Optional[asyncio.AbstractEventLoop] = None,
) -> Optional[Text]:
    if loop is None:
        try:
            loop = asyncio.get_event_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)

    return loop.run_until_complete(
        train_async(
            domain=domain,
            config=config,
            training_files=training_files,
            output_path=output,
            force_training=force_training,
            fixed_model_name=fixed_model_name,
            persist_nlu_training_data=persist_nlu_training_data,
            additional_arguments=additional_arguments,
        )
    )


async def train_async(
    domain: Union[Domain, Text],
    config: Dict[Text, Text],
    training_files: Optional[Union[Text, List[Text]]],
    output_path: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
    additional_arguments: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Rasa model (Core and NLU).

    Args:
        domain: Path to the domain file.
        config: Dict of paths to the config for Core and NLU. Keys are language codes
        training_files: Paths to the training data for Core and NLU.
        output_path: Output path.
        force_training: If `True` retrain model even if data has not changed.
        fixed_model_name: Name of model to be stored.
        persist_nlu_training_data: `True` if the NLU training data should be persisted
                                   with the model.
        additional_arguments: Additional training parameters.

    Returns:
        Path of the trained model archive.
    """

    # file_importer = TrainingDataImporter.load_from_config(
    #    config, domain, training_files
    # )

    with ExitStack() as stack:
        train_path = stack.enter_context(TempDirectoryPath(tempfile.mkdtemp()))

        # bf mod
        from rasa_addons.importers import BotfrontFileImporter
        file_importer = BotfrontFileImporter(config, domain, training_files)
        # domain = await file_importer.get_domain()
        # if domain.is_empty():
        #     return await handle_domain_if_not_exists(
        #         file_importer, output_path, fixed_model_name
        #     )
        # /bf mod

        return await _train_async_internal(
            file_importer,
            train_path,
            output_path,
            force_training,
            fixed_model_name,
            persist_nlu_training_data,
            additional_arguments,
        )


async def handle_domain_if_not_exists(
    file_importer: TrainingDataImporter, output_path, fixed_model_name
):
    nlu_model_only = await _train_nlu_with_validated_data(
        file_importer, output=output_path, fixed_model_name=fixed_model_name
    )
    print_warning(
        "Core training was skipped because no valid domain file was found. Only an nlu-model was created."
        "Please specify a valid domain using '--domain' argument or check if the provided domain file exists."
    )
    return nlu_model_only


async def _train_async_internal(
    file_importer: TrainingDataImporter,
    train_path: Text,
    output_path: Text,
    force_training: bool,
    fixed_model_name: Optional[Text],
    persist_nlu_training_data: bool,
    additional_arguments: Optional[Dict],
) -> Optional[Text]:
    """Trains a Rasa model (Core and NLU). Use only from `train_async`.

    Args:
        file_importer: `TrainingDataImporter` which supplies the training data.
        train_path: Directory in which to train the model.
        output_path: Output path.
        force_training: If `True` retrain model even if data has not changed.
        persist_nlu_training_data: `True` if the NLU training data should be persisted
                                   with the model.
        fixed_model_name: Name of model to be stored.
        additional_arguments: Additional training parameters.

    Returns:
        Path of the trained model archive.
    """

    stories, nlu_data = await asyncio.gather(
        file_importer.get_stories(), file_importer.get_nlu_data()
    )

    # if stories.is_empty() and nlu_data.is_empty():
    #     print_error(
    #         "No training data given. Please provide stories and NLU data in "
    #         "order to train a Rasa model using the '--data' argument."
    #     )
    #     return

    # if nlu_data.is_empty():
    #     print_warning("No NLU data present. Just a Rasa Core model will be trained.")
    #     return await _train_core_with_validated_data(
    #         file_importer,
    #         output=output_path,
    #         fixed_model_name=fixed_model_name,
    #         additional_arguments=additional_arguments,
    #     )

    new_fingerprint = await model.model_fingerprint(file_importer)
    old_model = model.get_latest_model(output_path)
    fingerprint_comparison = FingerprintComparisonResult(force_training=force_training)
    if not force_training:
        fingerprint_comparison = model.should_retrain(
            new_fingerprint, old_model, train_path
        )

    # bf mod >
    if fingerprint_comparison.nlu == True: # replace True with list of all langs
        fingerprint_comparison.nlu = list(new_fingerprint.get("nlu-config", {}).keys())
    domain = await file_importer.get_domain()
    core_untrainable = domain.is_empty() or stories.is_empty()
    nlu_untrainable = [l for l, d in nlu_data.items() if d.is_empty()]
    fingerprint_comparison.core = fingerprint_comparison.core and not core_untrainable
    fingerprint_comparison.nlu = [l for l in fingerprint_comparison.nlu if l not in nlu_untrainable]

    if core_untrainable:
        print_color("Skipping Core training since domain or stories are empty.", color=bcolors.OKBLUE)
    for lang in nlu_untrainable:
        print_color("No NLU data found for language <{}>, skipping training...".format(lang), color=bcolors.OKBLUE)
    # </ bf mod

    if fingerprint_comparison.is_training_required():
        await _do_training(
            file_importer,
            output_path=output_path,
            train_path=train_path,
            fingerprint_comparison_result=fingerprint_comparison,
            fixed_model_name=fixed_model_name,
            persist_nlu_training_data=persist_nlu_training_data,
            additional_arguments=additional_arguments,
        )

        return model.package_model(
            fingerprint=new_fingerprint,
            output_directory=output_path,
            train_path=train_path,
            fixed_model_name=fixed_model_name,
        )

    print_success(
        "Nothing changed. You can use the old model stored at '{}'."
        "".format(os.path.abspath(old_model))
    )
    return old_model


async def _do_training(
    file_importer: TrainingDataImporter,
    output_path: Text,
    train_path: Text,
    fingerprint_comparison_result: Optional[FingerprintComparisonResult] = None,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
    additional_arguments: Optional[Dict] = None,
):
    if not fingerprint_comparison_result:
        fingerprint_comparison_result = FingerprintComparisonResult()

    if fingerprint_comparison_result.should_retrain_core():
        await _train_core_with_validated_data(
            file_importer,
            output=output_path,
            train_path=train_path,
            fixed_model_name=fixed_model_name,
            additional_arguments=additional_arguments,
        )
    elif fingerprint_comparison_result.should_retrain_nlg():
        print_color(
            "Core stories/configuration did not change. "
            "Only the templates section has been changed. A new model with "
            "the updated templates will be created.",
            color=bcolors.OKBLUE,
        )
        await model.update_model_with_new_domain(file_importer, train_path)
    else:
        print_color(
            "Core stories/configuration did not change. No need to retrain Core model.",
            color=bcolors.OKBLUE,
        )

    if fingerprint_comparison_result.should_retrain_nlu():
        await _train_nlu_with_validated_data(
            file_importer,
            output=output_path,
            train_path=train_path,
            fixed_model_name=fixed_model_name,
            retrain_nlu=fingerprint_comparison_result.nlu,
            persist_nlu_training_data=persist_nlu_training_data,
        )
    else:
        print_color(
            "NLU data/configuration did not change. No need to retrain NLU model.",
            color=bcolors.OKBLUE,
        )


def train_core(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    additional_arguments: Optional[Dict] = None,
) -> Optional[Text]:
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        train_core_async(
            domain=domain,
            config=config,
            stories=stories,
            output=output,
            train_path=train_path,
            fixed_model_name=fixed_model_name,
            additional_arguments=additional_arguments,
        )
    )


async def train_core_async(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    additional_arguments: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Core model.

    Args:
        domain: Path to the domain file.
        config: Path to the config file for Core.
        stories: Path to the Core training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.
        fixed_model_name: Name of model to be stored.
        uncompress: If `True` the model will not be compressed.
        additional_arguments: Additional training parameters.

    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """

    file_importer = TrainingDataImporter.load_core_importer_from_config(
        config, domain, [stories]
    )
    domain = await file_importer.get_domain()
    if domain.is_empty():
        print_error(
            "Core training was skipped because no valid domain file was found. "
            "Please specify a valid domain using '--domain' argument or check if the provided domain file exists."
        )
        return None

    if not await file_importer.get_stories():
        print_error(
            "No stories given. Please provide stories in order to "
            "train a Rasa Core model using the '--stories' argument."
        )
        return

    return await _train_core_with_validated_data(
        file_importer,
        output=output,
        train_path=train_path,
        fixed_model_name=fixed_model_name,
        additional_arguments=additional_arguments,
    )


async def _train_core_with_validated_data(
    file_importer: TrainingDataImporter,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    additional_arguments: Optional[Dict] = None,
) -> Optional[Text]:
    """Train Core with validated training and config data."""

    import rasa.core.train

    with ExitStack() as stack:
        if train_path:
            # If the train path was provided, do nothing on exit.
            _train_path = train_path
        else:
            # Otherwise, create a temp train path and clean it up on exit.
            _train_path = stack.enter_context(TempDirectoryPath(tempfile.mkdtemp()))

        # normal (not compare) training
        print_color("Training Core model...", color=bcolors.OKBLUE)
        domain, config = await asyncio.gather(
            file_importer.get_domain(), file_importer.get_config()
        )
        await rasa.core.train(
            domain_file=domain,
            training_resource=file_importer,
            output_path=os.path.join(_train_path, DEFAULT_CORE_SUBDIRECTORY_NAME),
            policy_config=config,
            additional_arguments=additional_arguments,
        )
        print_color("Core model training completed.", color=bcolors.OKBLUE)

        if train_path is None:
            # Only Core was trained.
            new_fingerprint = await model.model_fingerprint(file_importer)
            return model.package_model(
                fingerprint=new_fingerprint,
                output_directory=output,
                train_path=_train_path,
                fixed_model_name=fixed_model_name,
                model_prefix="core-",
            )

        return _train_path


def train_nlu(
    config: Text,
    nlu_data: Text,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
) -> Optional[Text]:
    """Trains an NLU model.

    Args:
        config: Path to the config file for NLU.
        nlu_data: Path to the NLU training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.
        fixed_model_name: Name of the model to be stored.
        persist_nlu_training_data: `True` if the NLU training data should be persisted
                                   with the model.


    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """

    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        _train_nlu_async(
            config,
            nlu_data,
            output,
            train_path,
            fixed_model_name,
            persist_nlu_training_data,
        )
    )


async def _train_nlu_async(
    config: Text,
    nlu_data: Text,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
):
    if not nlu_data:
        print_error(
            "No NLU data given. Please provide NLU data in order to train "
            "a Rasa NLU model using the '--nlu' argument."
        )
        return

    # training NLU only hence the training files still have to be selected
    file_importer = TrainingDataImporter.load_nlu_importer_from_config(
        config, training_data_paths=[nlu_data]
    )

    training_datas = await file_importer.get_nlu_data()
    if training_datas.is_empty():
        print_error(
            f"Path '{nlu_data}' doesn't contain valid NLU data in it. "
            "Please verify the data format. "
            "The NLU model training will be skipped now."
        )
        return

    return await _train_nlu_with_validated_data(
        file_importer,
        output=output,
        train_path=train_path,
        fixed_model_name=fixed_model_name,
        persist_nlu_training_data=persist_nlu_training_data,
    )


async def _train_nlu_with_validated_data(
    file_importer: TrainingDataImporter,
    output: Text,
    train_path: Optional[Text] = None,
    fixed_model_name: Optional[Text] = None,
    persist_nlu_training_data: bool = False,
    retrain_nlu: Union[bool, List[Text]] = True
) -> Optional[Text]:
    """Train NLU with validated training and config data."""

    import rasa.nlu.train

    with ExitStack() as stack:
        models = {}
        from rasa.nlu import config as cfg_loader

        if train_path:
            # If the train path was provided, do nothing on exit.
            _train_path = train_path
        else:
            # Otherwise, create a temp train path and clean it up on exit.
            _train_path = stack.enter_context(TempDirectoryPath(tempfile.mkdtemp()))
        # bf mod
        config = await file_importer.get_nlu_config(retrain_nlu)
        for lang in config:
            if config[lang]:
                print_color("Start training {} NLU model ...".format(lang), color=bcolors.OKBLUE)
                _, models[lang], _ = await rasa.nlu.train(
                    config[lang],
                    file_importer,
                    _train_path,
                    fixed_model_name="nlu-{}".format(lang),
                    persist_nlu_training_data=persist_nlu_training_data,
                )
            else:
                print_color("NLU data for language <{}> didn't change, skipping training...".format(lang), color=bcolors.OKBLUE)
        # /bf mod
        print_color("NLU model training completed.", color=bcolors.OKBLUE)

        if train_path is None:
            # Only NLU was trained
            new_fingerprint = await model.model_fingerprint(file_importer)

            return model.package_model(
                fingerprint=new_fingerprint,
                output_directory=output,
                train_path=_train_path,
                fixed_model_name=fixed_model_name,
                model_prefix="nlu-",
            )

        return _train_path

[((116, 4, 119, 5), 'rasa.cli.utils.print_warning', 'print_warning', ({(117, 8, 118, 110): '"""Core training was skipped because no valid domain file was found. Only an nlu-model was created.Please specify a valid domain using \'--domain\' argument or check if the provided domain file exists."""'}, {}), '(\n    "Core training was skipped because no valid domain file was found. Only an nlu-model was created.Please specify a valid domain using \'--domain\' argument or check if the provided domain file exists."\n    )', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((169, 16, 169, 51), 'rasa.model.get_latest_model', 'model.get_latest_model', ({(169, 39, 169, 50): 'output_path'}, {}), '(output_path)', False, 'from rasa import model\n'), ((170, 29, 170, 87), 'rasa.model.FingerprintComparisonResult', 'FingerprintComparisonResult', (), '', False, 'from rasa.model import FingerprintComparisonResult\n'), ((275, 11, 275, 35), 'asyncio.get_event_loop', 'asyncio.get_event_loop', ({}, {}), '()', False, 'import asyncio\n'), ((317, 20, 319, 5), 'rasa.importers.importer.TrainingDataImporter.load_core_importer_from_config', 'TrainingDataImporter.load_core_importer_from_config', ({(318, 8, 318, 14): 'config', (318, 16, 318, 22): 'domain', (318, 24, 318, 33): '[stories]'}, {}), '(config, domain, [stories])', False, 'from rasa.importers.importer import TrainingDataImporter\n'), ((418, 11, 418, 35), 'asyncio.get_event_loop', 'asyncio.get_event_loop', ({}, {}), '()', False, 'import asyncio\n'), ((447, 20, 449, 5), 'rasa.importers.importer.TrainingDataImporter.load_nlu_importer_from_config', 'TrainingDataImporter.load_nlu_importer_from_config', (), '', False, 'from rasa.importers.importer import TrainingDataImporter\n'), ((86, 9, 86, 20), 'contextlib.ExitStack', 'ExitStack', ({}, {}), '()', False, 'from contextlib import ExitStack\n'), ((91, 24, 91, 76), 'rasa_addons.importers.BotfrontFileImporter', 'BotfrontFileImporter', ({(91, 45, 91, 51): 'config', (91, 53, 91, 59): 'domain', (91, 61, 91, 75): 'training_files'}, {}), '(config, domain, training_files)', False, 'from rasa_addons.importers import BotfrontFileImporter\n'), ((168, 28, 168, 66), 'rasa.model.model_fingerprint', 'model.model_fingerprint', ({(168, 52, 168, 65): 'file_importer'}, {}), '(file_importer)', False, 'from rasa import model\n'), ((172, 33, 174, 9), 'rasa.model.should_retrain', 'model.should_retrain', ({(173, 12, 173, 27): 'new_fingerprint', (173, 29, 173, 38): 'old_model', (173, 40, 173, 50): 'train_path'}, {}), '(new_fingerprint, old_model, train_path)', False, 'from rasa import model\n'), ((186, 8, 186, 102), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((202, 15, 207, 9), 'rasa.model.package_model', 'model.package_model', (), '', False, 'from rasa import model\n'), ((226, 40, 226, 69), 'rasa.model.FingerprintComparisonResult', 'FingerprintComparisonResult', ({}, {}), '()', False, 'from rasa.model import FingerprintComparisonResult\n'), ((260, 8, 263, 9), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((322, 8, 325, 9), 'rasa.cli.utils.print_error', 'print_error', ({(323, 12, 324, 114): '"""Core training was skipped because no valid domain file was found. Please specify a valid domain using \'--domain\' argument or check if the provided domain file exists."""'}, {}), '(\n    "Core training was skipped because no valid domain file was found. Please specify a valid domain using \'--domain\' argument or check if the provided domain file exists."\n    )', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((329, 8, 332, 9), 'rasa.cli.utils.print_error', 'print_error', ({(330, 12, 331, 69): '"""No stories given. Please provide stories in order to train a Rasa Core model using the \'--stories\' argument."""'}, {}), '(\n    "No stories given. Please provide stories in order to train a Rasa Core model using the \'--stories\' argument."\n    )', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((355, 9, 355, 20), 'contextlib.ExitStack', 'ExitStack', ({}, {}), '()', False, 'from contextlib import ExitStack\n'), ((364, 8, 364, 67), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((375, 8, 375, 75), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((440, 8, 443, 9), 'rasa.cli.utils.print_error', 'print_error', ({(441, 12, 442, 58): '"""No NLU data given. Please provide NLU data in order to train a Rasa NLU model using the \'--nlu\' argument."""'}, {}), '(\n    "No NLU data given. Please provide NLU data in order to train a Rasa NLU model using the \'--nlu\' argument."\n    )', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((453, 8, 457, 9), 'rasa.cli.utils.print_error', 'print_error', ({(454, 12, 456, 57): 'f"""Path \'{nlu_data}\' doesn\'t contain valid NLU data in it. Please verify the data format. The NLU model training will be skipped now."""'}, {}), '(\n    f"Path \'{nlu_data}\' doesn\'t contain valid NLU data in it. Please verify the data format. The NLU model training will be skipped now."\n    )', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((481, 9, 481, 20), 'contextlib.ExitStack', 'ExitStack', ({}, {}), '()', False, 'from contextlib import ExitStack\n'), ((506, 8, 506, 74), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((36, 19, 36, 43), 'asyncio.get_event_loop', 'asyncio.get_event_loop', ({}, {}), '()', False, 'import asyncio\n'), ((211, 18, 211, 44), 'os.path.abspath', 'os.path.abspath', ({(211, 34, 211, 43): 'old_model'}, {}), '(old_model)', False, 'import os\n'), ((237, 8, 242, 9), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((245, 8, 248, 9), 'rasa.cli.utils.print_color', 'print_color', (), '', False, 'from rasa.cli.utils import print_success, print_warning, print_error, bcolors, print_color\n'), ((380, 19, 386, 13), 'rasa.model.package_model', 'model.package_model', (), '', False, 'from rasa import model\n'), ((512, 19, 518, 13), 'rasa.model.package_model', 'model.package_model', (), '', False, 'from rasa import model\n'), ((38, 19, 38, 43), 'asyncio.new_event_loop', 'asyncio.new_event_loop', ({}, {}), '()', False, 'import asyncio\n'), ((39, 12, 39, 40), 'asyncio.set_event_loop', 'asyncio.set_event_loop', ({(39, 35, 39, 39): 'loop'}, {}), '(loop)', False, 'import asyncio\n'), ((87, 59, 87, 77), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n'), ((243, 14, 243, 75), 'rasa.model.update_model_with_new_domain', 'model.update_model_with_new_domain', ({(243, 49, 243, 62): 'file_importer', (243, 64, 243, 74): 'train_path'}, {}), '(file_importer, train_path)', False, 'from rasa import model\n'), ((379, 36, 379, 74), 'rasa.model.model_fingerprint', 'model.model_fingerprint', ({(379, 60, 379, 73): 'file_importer'}, {}), '(file_importer)', False, 'from rasa import model\n'), ((510, 36, 510, 74), 'rasa.model.model_fingerprint', 'model.model_fingerprint', ({(510, 60, 510, 73): 'file_importer'}, {}), '(file_importer)', False, 'from rasa import model\n'), ((361, 64, 361, 82), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n'), ((371, 24, 371, 81), 'os.path.join', 'os.path.join', ({(371, 37, 371, 48): '_train_path', (371, 50, 371, 80): 'DEFAULT_CORE_SUBDIRECTORY_NAME'}, {}), '(_train_path, DEFAULT_CORE_SUBDIRECTORY_NAME)', False, 'import os\n'), ((490, 64, 490, 82), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n')]