# SPDX-FileCopyrightText: 2022 Chris Heywood
#
# SPDX-License-Identifier: GPL-3.0-or-later

from datetime import datetime
import unittest
import warnings

from iotas.note import Note
from iotas.export_utils import (
    build_batch_dirname,
    build_default_filename,
    exporting_to_dir,
    parse_export_formats,
    sanitise_title_for_filename,
)

warnings.filterwarnings("ignore", "version")


class Test(unittest.TestCase):

    def test_build_default_filename(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"

        filename = build_default_filename(
            note, out_format="md", file_extension="md", add_timestamp=False
        )
        self.assertEqual(filename, "Test Note Title.md")

        filename = build_default_filename(
            note, out_format="odt", file_extension="odt", add_timestamp=False
        )
        self.assertEqual(filename, "Test Note Title.odt")

        filename = build_default_filename(
            note, out_format="pdf", file_extension="pdf", add_timestamp=False
        )
        self.assertEqual(filename, "Test Note Title.pdf")

        filename = build_default_filename(
            note, out_format="html", file_extension="html", add_timestamp=False
        )
        self.assertEqual(filename, "Test Note Title")

        filename = build_default_filename(
            note, out_format="md", file_extension="md", add_timestamp=True
        )
        parts = filename.split(" ")
        self.assertEqual(len(parts), 4)

        timestamp = None
        try:
            timestamp = datetime.strptime(parts[0], "%Y-%m-%dT%H:%M:%S")
        except ValueError:
            pass

        self.assertIsNotNone(timestamp)
        self.assertEqual(" ".join(parts[1:]), "Test Note Title.md")

    def test_exporting_to_dir(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"

        self.assertTrue(exporting_to_dir("html", note))
        self.assertFalse(exporting_to_dir("md", note))

        note.content = "![image](.attachments.1/image.png)"

        self.assertTrue(exporting_to_dir("md", note))

    def test_build_batch_dirname(self) -> None:
        now = datetime.now()
        name = build_batch_dirname(now)
        self.assertEqual(
            name, "{0} at {1} batch".format(now.strftime("%Y-%m-%d"), now.strftime("%H:%M:%S"))
        )

    def test_sanitise_title_for_filename(self) -> None:
        title = "!@#$%^&*()+word={}[]|\\?></,"
        sanitised = sanitise_title_for_filename(title)
        self.assertEqual(sanitised, "word")

    def test_parse_export_formats(self) -> None:
        self.assertIsNone(parse_export_formats(""))
        self.assertIsNone(parse_export_formats("plain"))

        json = """
        [
          {"pandocOutFormat": "docx", "fileExtension": "docx"},
          {"pandocOutFormat": "epub3", "fileExtension": "epub"}
        ]
        """
        parsed = parse_export_formats(json)
        assert parsed
        self.assertTrue(type(parsed) is dict)
        self.assertTrue("docx" in parsed)
        self.assertTrue("epub3" in parsed)
        self.assertFalse("epub" in parsed)
        self.assertEqual(len(parsed["docx"]), 2)
        self.assertEqual(parsed["epub3"]["format"], "epub3")
        self.assertEqual(parsed["epub3"]["extension"], "epub")

        invalid = """
        [
          {"pandocOutFormatx": "docx", "fileExtension": "docx"}
        ]
        """
        self.assertEqual(parse_export_formats(invalid), {})
