"""
Project:    PARTS Website
Author:     Edward Middleton-Smith
            Precision And Research Technology Systems Limited

Technology: View Models
Feature:    MTG Games View Model

Description:
Data model for MTG games view
"""

# internal
from business_objects.tcg.mtg_deck import MTG_Deck, Parameters_MTG_Deck
from business_objects.tcg.mtg_game import MTG_Game, Parameters_MTG_Game
from business_objects.tcg.mtg_game_player import MTG_Game_Player, Parameters_MTG_Game_Player
from business_objects.tcg.mtg_game_round import MTG_Game_Round, Parameters_MTG_Game_Round
from business_objects.tcg.mtg_game_round_player_damage import MTG_Game_Round_Player_Damage, Parameters_MTG_Game_Round_Player_Damage
from business_objects.tcg.user import User, Parameters_User
from datastores.datastore_mtg import DataStore_MTG
from datastores.datastore_user import DataStore_User
from models.model_view_mtg_base import Model_View_MTG_Base
from helpers.helper_app import Helper_App
import lib.argument_validation as av

# external
from pydantic import BaseModel
from typing import ClassVar


class Model_View_MTG_Game(Model_View_MTG_Base):
    ATTR_RECEIVED_FROM_COMMANDER_PLAYER_ID: ClassVar[str] = MTG_Game_Round_Player_Damage.ATTR_RECEIVED_FROM_COMMANDER_PLAYER_ID
    FLAG_DAMAGE_LOG: ClassVar[str] = 'damage-log'
    FLAG_IS_ELIMINATED: ClassVar[str] = MTG_Game_Round_Player_Damage.FLAG_IS_ELIMINATED
    # ID_INPUT_ROUND: ClassVar[str] = 'input-round'
    FLAG_ROUND_DISPLAY_ORDER_BUTTON: ClassVar[str] = 'btn-round-display-order'
    FLAG_ROUND_DISPLAY_ORDER_MINUS: ClassVar[str] = 'round-display-order-minus'
    FLAG_ROUND_DISPLAY_ORDER_PLUS: ClassVar[str] = 'round-display-order-plus'
    GAME_DATA_OUTDATED_LABEL_ID: ClassVar[str] = 'game-data-outdated-label'
    ID_OVERLAY_GAME_OUTDATED: ClassVar[str] = 'overlay-game-outdated'
    PLAYER_SETUP_WRAPPER_TEMPLATE_ID: ClassVar[str] = 'player-setup-wrapper-template'

    damage_records: list = None
    decks: list = None
    game: MTG_Game = None
    players: list = None
    rounds: list = None
    users: list = None
    """
    parameters_player: Parameters_MTG_Game_Player = None
    parameters_round: Parameters_MTG_Game_Round = None
    parameters_damage: Parameters_MTG_Game_Round_Player_Damage = None
    parameters_deck: Parameters_MTG_Deck = None
    """

    def __init__(self, game_id, hash_page_current=Model_View_MTG_Base.HASH_PAGE_MTG_GAME):
        _m = 'Model_View_MTG_Game.__init__'
        # Helper_App.console_log(f'{_m}\nstarting...')
        super().__init__(hash_page_current=hash_page_current)
        self._title = 'MTG Game'
        datastore = DataStore_MTG()
        user_session = datastore.get_user_session()

        # Get the specific game
        parameters_game = Parameters_MTG_Game.get_default(user_session.user_id)
        parameters_game.game_ids = str(game_id)
        parameters_game.get_all_game = False
        games, errors = datastore.get_many_mtg_game(parameters_game)
        if len(games) > 0:
            self.game = games[0]

        # Get all decks
        parameters_deck = Parameters_MTG_Deck.get_default(user_session.user_id)
        parameters_deck.get_all_deck = True
        parameters_deck.require_all_id_filters_met = False
        parameters_deck.require_any_id_filters_met = False
        parameters_deck.require_all_non_id_filters_met = False
        parameters_deck.require_any_non_id_filters_met = False
        self.decks, errors = datastore.get_many_mtg_deck(parameters_deck)

        # Get all users
        parameters_user = Parameters_User.get_default()
        parameters_user.get_all_user = True
        parameters_user.require_all_id_filters_met = False
        parameters_user.require_any_id_filters_met = False
        self.users, errors = DataStore_User.get_many_user(parameters_user)

        # Get players for this game
        parameters_player = Parameters_MTG_Game_Player.get_default(user_session.user_id)
        parameters_player.get_all_game = False
        parameters_player.game_ids = str(game_id)
        self.players, errors = datastore.get_many_mtg_game_player(parameters_player)

        # Get rounds for this game
        parameters_round = Parameters_MTG_Game_Round.get_default(user_session.user_id)
        parameters_round.get_all_game = False
        parameters_round.game_ids = str(game_id)
        self.rounds, errors = datastore.get_many_mtg_game_round(parameters_round)

        # Get damage for this game
        parameters_damage = Parameters_MTG_Game_Round_Player_Damage.get_default(user_session.user_id)
        parameters_damage.get_all_game = False
        parameters_damage.game_ids = str(game_id)
        self.damage_records, errors = datastore.get_many_mtg_game_round_player_damage(parameters_damage)

