""" Various 'n' sundry utilities for our tests """

# pylint: disable=E0401, R1721

from pathlib import Path
from typing import List

import pytest

from ingest_envoy.manifest_components import WEBLOG_FILENAME

EVLA_CAL_WANTED_FILENAMES = ["my_science_products.tar", WEBLOG_FILENAME]
UNWANTED = ["ignore_me.fits", "just_a_lotta_nothing", "uninteresting_metadata.xml"]

IMG_MANIFEST_INPUT_FILENAMES = [
    # additional metadata
    "image_metadata_2021_05_21_T10_17_19.180.json",
    # quicklook image
    "VLASS2.1.ql.T08t09.J055438-113000.10.2048.v1.I.iter1.image.pbcor.tt0.subim.fits",
    # quicklook RMS image
    "VLASS2.1.ql.T08t09.J055438-113000.10.2048.v1.I.iter1.image.pbcor.tt0.rms.subim.fits",
    # thumbnail
    "VLASS2.1.ql.T08t09.J055438_113000.10.2048.v1.I.iter1.image.pbcor.tt0.subim.png",
    # weblog
    WEBLOG_FILENAME,
    # ingestion artifacts tar -- to be created as side effect of ingestion manifest creation
    # pipeline artifacts tar
    "VLASS2.1.ql.T08t09.J055438-113000.10.2048.v1.tar",
]
CASA_BYPRODUCTS = [
    "unknown.pipeline_manifest.xml",
    "unknown.aux_products.tgz",
    "casa_commands.log",
    "casa_pipescript.py",
]

# this file gets created during construction of image ingestion manifest
IMG_ARTIFACTS_FILENAME = "ingestion_artifacts_2021_05_21_T10_17_19.275.tar"

# just to make things interesting
RANDOM_TAR = "totally_random_tar.tar"


@pytest.fixture(scope="function")
def ingest_path(tmpdir: Path) -> Path:
    """
    Make an "ingestion path" for tests

    :param tmpdir: temporary home for ingestion location
    :return:
    """

    # cast is necessary because otherwise we get a LocalPath, which doesn't work
    fake_ingest_path = Path(tmpdir / "ingestion")
    fake_ingest_path.mkdir()
    return fake_ingest_path


def find_example_manifest(manifest_name: str) -> Path:
    """
    Get this example manifest for comparison with one we've generated in a test.

    :param manifest_name: unique file identifier
    :return: full path to the manifest file
    """
    filename = manifest_name + ".json"
    for file in Path.cwd().rglob(filename):
        return file

    raise FileNotFoundError(filename)


def populate_fake_evla_cal_ingest_path(staging_dir: Path) -> List[Path]:
    """
    Create a directory containing fake calibration products, plus other stuff
    that we -don't- want to ingest.

    :param staging_dir: our temporary dir
    :return:
    """

    files = []
    filenames = [filename for filename in EVLA_CAL_WANTED_FILENAMES]
    for filename in UNWANTED:
        filenames.append(filename)

    for filename in filenames:
        path = staging_dir / filename
        path.touch()
        files.append(path)

    return files


def populate_fake_image_ingest_path(staging_dir: Path) -> List[Path]:
    """
    Create a directory containing fake image products, plus other stuff
    that we -don't- want to ingest.

    :param staging_dir: our temporary dir
    :return:
    """
    for filename in IMG_MANIFEST_INPUT_FILENAMES:
        file = staging_dir / filename
        file.touch()

    for filename in CASA_BYPRODUCTS:
        file = staging_dir / filename
        file.touch()

    file = staging_dir / RANDOM_TAR
    file.touch()

    return [file for file in staging_dir.iterdir()]