from __future__ import annotations

from pathlib import Path
import sys
import unittest
from unittest.mock import patch


SRC_ROOT = Path(__file__).resolve().parents[2] / "src"

if str(SRC_ROOT) not in sys.path:
    sys.path.insert(0, str(SRC_ROOT))


from ffx import screen_support  # noqa: E402


class StaticConfig:
    def __init__(self, data):
        self._data = data

    def getData(self):
        return self._data


class ScreenSupportTests(unittest.TestCase):
    def make_context(self):
        return {
            "config": StaticConfig(
                {
                    "metadata": {
                        "signature": {"RECODED_WITH": "FFX"},
                        "remove": ["VERSION-eng"],
                        "ignore": ["ENCODER"],
                        "streams": {
                            "remove": ["BPS"],
                            "ignore": ["language"],
                        },
                    }
                }
            ),
            "database": {"session": object()},
        }

    def test_build_screen_bootstrap_extracts_metadata_filters(self):
        context = self.make_context()

        bootstrap = screen_support.build_screen_bootstrap(context)

        self.assertIs(context, bootstrap.context)
        self.assertEqual({"RECODED_WITH": "FFX"}, bootstrap.signature_tags)
        self.assertEqual(["VERSION-eng"], bootstrap.remove_global_keys)
        self.assertEqual(["ENCODER"], bootstrap.ignore_global_keys)
        self.assertEqual(["BPS"], bootstrap.remove_track_keys)
        self.assertEqual(["language"], bootstrap.ignore_track_keys)

    def test_build_screen_controllers_only_creates_requested_instances(self):
        context = self.make_context()

        with (
            patch.object(screen_support, "PatternController", side_effect=lambda context: ("pattern", context)),
            patch.object(screen_support, "ShowController", side_effect=lambda context: ("show", context)),
            patch.object(screen_support, "TmdbController", side_effect=lambda: "tmdb"),
            patch.object(screen_support, "ShiftedSeasonController", side_effect=lambda context: ("shifted", context)),
        ):
            controllers = screen_support.build_screen_controllers(
                context,
                pattern=True,
                show=True,
                tmdb=True,
                shifted_season=True,
            )

        self.assertEqual(
            {
                "pattern": ("pattern", context),
                "show": ("show", context),
                "tmdb": "tmdb",
                "shifted_season": ("shifted", context),
            },
            controllers,
        )


if __name__ == "__main__":
    unittest.main()
