import unittest

import chess

from schach import notation


class NotationTest(unittest.TestCase):
    def test_san(self):
        self.assertEqual(notation.san("Nf3", "de"), "Sf3")
        self.assertEqual(notation.san("Bxc6", "de"), "Lxc6")
        self.assertEqual(notation.san("e8=Q+", "de"), "e8=D+")
        self.assertEqual(notation.san("Rfe1", "de"), "Tfe1")
        self.assertEqual(notation.san("O-O", "de"), "O-O")
        self.assertEqual(notation.san("Nf3", "en"), "Nf3")

    def test_label(self):
        board = chess.Board()
        self.assertEqual(notation.label(board, chess.Move.from_uci("g1f3"), "de"), "1. Sf3")
        board.push_san("e4")
        self.assertEqual(notation.label(board, chess.Move.from_uci("b8c6"), "en"), "1… Nc6")

    def test_text(self):
        german = "Sg5 wäre wilder. Nach Sxe4 holt Te1 den Bauern zurück, dann Lxc6 und De2. Plan Sbd2–f1–g3, e8=D."
        self.assertEqual(notation.text(german, "de"), german)
        self.assertEqual(
            notation.text(german, "en"),
            "Ng5 wäre wilder. Nach Nxe4 holt Re1 den Bauern zurück, dann Bxc6 und Qe2. Plan Nbd2–f1–g3, e8=Q.",
        )

    def test_text_leaves_words_alone(self):
        sentence = "Schwarz deckt e5. Der Läufer zielt auf f7 – Lehrerin, Dame, Turm, König."
        self.assertEqual(notation.text(sentence, "en"), sentence)


class ClockPartsTest(unittest.TestCase):
    def test_parts(self):
        from schach.board import clock_parts

        self.assertEqual(clock_parts(300000), ("05", "00"))
        self.assertEqual(clock_parts(61000), ("01", "01"))
        self.assertEqual(clock_parts(19450), ("00", "19"))
        self.assertEqual(clock_parts(1800000), ("30", "00"))
        self.assertEqual(clock_parts(3723000), ("1:02", "03"))
        self.assertEqual(clock_parts(-5), ("00", "00"))


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