import random
import unittest

import chess
import chess.engine

from schach import engines, lichess, wdl


class FromWhitePctTest(unittest.TestCase):
    def test_stockfish_bar_has_no_draw(self):
        w, d, l = wdl.from_white_pct(70.0)
        self.assertAlmostEqual(w, 0.7)
        self.assertEqual(d, 0.0)
        self.assertAlmostEqual(l, 0.3)
        self.assertEqual(wdl.from_white_pct(100.0), (1.0, 0.0, 0.0))
        self.assertIsNone(wdl.from_white_pct(None))


class GameRatingTest(unittest.TestCase):
    def test_most_played_game_perf_wins_puzzles_ignored(self):
        account = {"perfs": {"blitz": {"games": 40, "rating": 1310}, "rapid": {"games": 120, "rating": 1405},
                             "puzzle": {"games": 900, "rating": 1800}}}
        self.assertEqual(lichess.game_rating(account), 1405)

    def test_no_games(self):
        self.assertIsNone(lichess.game_rating({"perfs": {"blitz": {"games": 0, "rating": 1500}}}))
        self.assertIsNone(lichess.game_rating(None))


class FakeMaia:
    def __init__(self):
        self.options = None

    def analyse(self, board, limit, multipv, options):
        self.options = options
        return [{"wdl": chess.engine.PovWdl(chess.engine.Wdl(500, 200, 300), board.turn)}]


class MaiaWdlJobTest(unittest.TestCase):
    def test_self_elo_is_side_to_move(self):
        board = chess.Board()
        board.push_uci("e2e4")  # Schwarz am Zug
        engine = FakeMaia()
        result = engines.maia_wdl(board, 1400, 800)(engine)
        self.assertEqual((engine.options["SelfElo"], engine.options["OppoElo"]), (800, 1400))
        self.assertEqual(result, (0.3, 0.2, 0.5))  # aus Sicht von Weiß

    def test_other_jobs_set_both_elos(self):
        engine = FakeMaia()
        engines.maia_candidates(chess.Board(), 1100)(engine)
        self.assertEqual((engine.options["SelfElo"], engine.options["OppoElo"]), (1100, 1100))
        self.assertNotIn("Elo", engine.options)


class PovTest(unittest.TestCase):
    def test_black_to_move_is_flipped(self):
        self.assertEqual(wdl.from_pov((700, 200, 100), chess.BLACK), (0.1, 0.2, 0.7))

    def test_pov_wdl(self):
        pov = chess.engine.PovWdl(chess.engine.Wdl(600, 300, 100), chess.BLACK)
        self.assertEqual(wdl.from_pov(pov, chess.BLACK), (0.1, 0.3, 0.6))


class OutcomeTest(unittest.TestCase):
    def test_mate_and_stalemate(self):
        mate = chess.Board("7k/6Q1/6K1/8/8/8/8/8 b - - 0 1")
        self.assertEqual(wdl.outcome(mate), (1.0, 0.0, 0.0))
        stalemate = chess.Board("7k/5Q2/6K1/8/8/8/8/8 b - - 0 1")
        self.assertEqual(wdl.outcome(stalemate), (0.0, 1.0, 0.0))
        self.assertIsNone(wdl.outcome(chess.Board()))


class DrawOfferTest(unittest.TestCase):
    def test_worse_side_accepts_better_side_declines(self):
        rng = random.Random(1)
        black_better = (0.1, 0.2, 0.7)
        self.assertTrue(all(wdl.maia_accepts_draw(black_better, chess.WHITE, rng) for _ in range(50)))
        self.assertFalse(any(wdl.maia_accepts_draw(black_better, chess.BLACK, rng) for _ in range(50)))

    def test_balanced_position_is_a_coin_flip(self):
        rng = random.Random(2)
        answers = [wdl.maia_accepts_draw((0.3, 0.4, 0.3), chess.BLACK, rng) for _ in range(400)]
        self.assertTrue(100 < sum(answers) < 300)


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