import doctest
import importlib
import json
import sys
import tempfile
import unittest
from pathlib import Path

import ocyan.core.utils.appextension
import ocyan.core.utils.cache
import ocyan.core.utils.csp
import ocyan.core.utils.currency
import ocyan.core.utils.dirtyhacks
import ocyan.core.fender
import ocyan.core.utils.fixture
import ocyan.core.utils.itertools
import ocyan.core.utils.rudiments
import ocyan.core.utils.theme
import ocyan.core.utils.typeconversion
from configtype.jsonconfig import JsonFinder, setup_search_paths


class FenderConfigTests(unittest.TestCase):
    def tearDown(self):
        sys.meta_path[:] = [
            finder for finder in sys.meta_path if not isinstance(finder, JsonFinder)
        ]
        sys.modules.pop("ocyan.core.json", None)
        importlib.reload(ocyan.core.fender)

    def test_import_without_search_paths_uses_defaults(self):
        sys.meta_path[:] = [
            finder for finder in sys.meta_path if not isinstance(finder, JsonFinder)
        ]
        sys.modules.pop("ocyan.core.json", None)
        module = importlib.reload(ocyan.core.fender)

        self.assertEqual(module.config.settings, {})
        self.assertEqual(module.config.ocyan_plugins, [])
        self.assertTrue(module.config.is_webshop)

    def test_import_with_search_paths_loads_ocyan_json(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            config_path = Path(tmpdir) / "ocyan.json"
            config_path.write_text(
                json.dumps(
                    {
                        "settings": {
                            "django": {
                                "name": "Core test project",
                            },
                            "i18n": {"enabled": True},
                        },
                        "ocyan_plugins": ["barebone", "ocyan.plugin.django"],
                    }
                )
            )

            setup_search_paths(tmpdir)
            sys.modules.pop("ocyan.core.json", None)
            module = importlib.reload(ocyan.core.fender)

            self.assertEqual(module.config.get("django", "name"), "Core test project")
            self.assertTrue(module.config.has_section("i18n"))
            self.assertFalse(module.config.is_webshop)
            self.assertFalse(module.config.i18n_enabled)


def load_tests(loader, tests, ignore):  # pylint: disable=W0613
    tests.addTests(loader.loadTestsFromTestCase(FenderConfigTests))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.appextension))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.cache))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.csp))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.currency))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.dirtyhacks))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.fixture))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.itertools))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.theme))
    tests.addTests(doctest.DocTestSuite(ocyan.core.utils.typeconversion))
    return tests
