from pathlib import Path

from websites_test_framework import __version__, WebsiteTest
from websites_test_framework.param import LOG_DIR_NAME, DEFAULT_SUCCESS_MESSAGE
from websites_test_framework.testing_framework import (
    CollectTestsResults,
    run_and_collect,
)
from websites_test_framework.custom_types import TestName

from tempfile import TemporaryDirectory

from websites_test_framework.website import Website


def test_version():
    assert __version__ == "0.1.0"


def get_sample_path(relative_path: str):
    return (Path(__file__).parent / "samples" / relative_path).resolve()


def test_run():
    website_path = get_sample_path("R102")
    assert website_path.is_dir()
    tester = WebsiteTest(website_path)
    assert hasattr(tester, "test_utf8")
    assert hasattr(tester.test_utf8, "is_test")
    assert tester.test_utf8.is_test
    assert hasattr(tester.test_utf8, "title")
    assert hasattr(tester.test_utf8, "weight")
    results = tester.run()
    assert "test_utf8" in results, results
    assert results[TestName("test_utf8")]["score"] == 1
    assert results[TestName("test_xml")]["score"] == 1
    assert results[TestName("test_not_to_much_div_and_span")]["score"] == 1
    log = results[TestName("test_xml")]["log"].strip()
    assert log == DEFAULT_SUCCESS_MESSAGE, repr(log)


def test_empty_website():
    website_path = get_sample_path("empty_web_site")
    assert website_path.is_dir()
    collector = CollectTestsResults([website_path])
    for test_name, result in collector.scores[website_path].items():
        assert result == 0, f"{test_name} result is {result}"
    assert collector.global_scores[website_path] == 0


def test_absolute_path():
    website_path = get_sample_path("R102")
    assert website_path.is_dir()
    tester = WebsiteTest(website_path, path_on_server="/R102")
    score, log = tester.test_broken_url_in_html()
    assert score == 1, score


def test_log():
    website_path = get_sample_path("R102")
    collecter = CollectTestsResults([website_path])
    with TemporaryDirectory() as temp_dir:
        collecter.write_log(temp_dir)
        log_files = list(Path(temp_dir).glob(f"{LOG_DIR_NAME}/*.md"))
        assert len(log_files) == 1, list(Path(temp_dir).glob("**/*"))


def test_xlsx():
    website_path = get_sample_path("R102")
    collecter = CollectTestsResults([website_path])
    with TemporaryDirectory() as temp_dir:
        xlsx_file = Path(temp_dir) / "scores.xslx"
        collecter.write_xlsx_file(xlsx_file)
        assert xlsx_file.is_file()


def test_used_css_files():
    website = Website(get_sample_path("R102"))
    assert {file.path.name for file in website.css_files} == {
        "td.css",
        "imported_stylesheet.css",
        "unused_stylesheet.css",
    }
    assert {file.path.name for file in website.used_css_files} == {
        "td.css",
        "imported_stylesheet.css",
    }


def test_run_and_collect():
    website_path = get_sample_path("R102")
    with TemporaryDirectory() as temp_dir:
        run_and_collect(input_dir_or_glob=[website_path], output_file_or_dir=Path(temp_dir))


if __name__ == "__main__":
    print("Testing...")
    test_absolute_path()
