from http import HTTPStatus

import requests
from loguru import logger

from ..src.game import MAX_OVERTIME, SUDDEN_DEATH_COLOR, SUDDEN_DEATH_LABEL, SwimmingSquidBattle, remap

from ..src.game_state import EndingState, OpeningState, RunningState, TransitionState
def test_set_game_state():
    game = SwimmingSquidBattle()
    game.set_game_state(RunningState.OPENING)
    assert isinstance(game.current_state,OpeningState)
    game.set_game_state(RunningState.TRANSITION)
    assert isinstance(game.current_state,TransitionState)
    game.set_game_state(RunningState.ENDING)
    assert isinstance(game.current_state,EndingState)

def test_set_game_state_invalid():
    game = SwimmingSquidBattle()
    game.set_game_state(RunningState.RESET)
    assert game.current_state is None
    game.set_game_state(RunningState.PLAYING)
    assert game.current_state is None


def _enter_sudden_death(game: SwimmingSquidBattle, score: int = 5):
    game._score_to_pass = 999
    game._overtime_count = MAX_OVERTIME
    game.frame_count = game._frame_limit
    game.squid1._score = score
    game.squid2._score = score
    assert game.time_out is False
    assert game._sudden_death is True


def test_overtime_limit_enters_sudden_death():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)
    assert game.squid1.score == 5
    assert game.squid2.score == 5


def test_sudden_death_zero_score_raised_to_one():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=0)
    assert game.squid1.score == 1
    assert game.squid2.score == 1


def test_sudden_death_does_not_reset_nonzero_score():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)
    game.frame_count = game._frame_limit
    assert game.time_out is False
    assert game.squid1.score == 5
    assert game.squid2.score == 5


def test_sudden_death_raises_zero_again_on_later_extension():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)
    game.squid1._score = 0
    game.squid2._score = 0
    game.frame_count = game._frame_limit
    assert game.time_out is False
    assert game.squid1.score == 1
    assert game.squid2.score == 1


def test_sudden_death_first_score_wins():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)

    game.squid1._score = 6
    assert game.is_running is False
    game.update_winner()
    assert game._winner[-1] == "1P"


def test_sudden_death_score_drop_loses():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)

    game.squid1._score = 4
    assert game.is_running is False
    game.update_winner()
    assert game._winner[-1] == "2P"


def test_sudden_death_keeps_adding_foods():
    game = SwimmingSquidBattle()
    before = sum(game._foods_max_num)
    _enter_sudden_death(game, score=5)
    assert sum(game._foods_max_num) == before + 2

    after_enter = sum(game._foods_max_num)
    game.frame_count = game._frame_limit
    assert game.time_out is False
    assert sum(game._foods_max_num) == after_enter + 2


def test_sudden_death_banner_is_persistent_gold():
    game = SwimmingSquidBattle()
    _enter_sudden_death(game, score=5)
    game._running_state = RunningState.PLAYING
    game.current_state = None

    scene = game.get_scene_progress_data()
    banners = [
        obj for obj in scene["foreground"]
        if obj.get("type") == "text" and obj.get("content") == SUDDEN_DEATH_LABEL
    ]
    assert len(banners) == 1
    assert banners[0]["color"] == SUDDEN_DEATH_COLOR


def test_remap():
    assert remap(100, 0, 100, 0, 1000) == 1000
    assert remap(0, 0, 100, 0, 1000) == 0
    assert remap(50, 0, 50, 0, 1000) == 1000
    assert remap(-10, 0, 50, 0, 1000) == 0


def test_resource_available():
    game = SwimmingSquidBattle()
    scene_info = game.get_scene_init_data()
    asset = scene_info['assets']
    for obj in asset:
        resp = requests.get(obj['url'])
        assert resp.status_code == HTTPStatus.OK , obj['url'] +"\n"+ resp.text
        logger.info(f"get {obj['url']} successfully")