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

Technology: View Models
Feature:    MTG Decks View Model

Description:
Data model for MTG decks view
"""

# internal
from business_objects.tcg.mtg_deck_commander_bracket import MTG_Deck_Commander_Bracket, Parameters_MTG_Deck_Commander_Bracket
from business_objects.tcg.mtg_deck import MTG_Deck, Parameters_MTG_Deck
from business_objects.tcg.statistic import Statistic, Parameters_Statistic
from datastores.datastore_mtg import DataStore_MTG
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_Decks(Model_View_MTG_Base):
    FLAG_STATISTICS: ClassVar[str] = 'statistics'
    commander_brackets: list = None
    decks: list = None
    form_filters: object = None
    parameters_deck: Parameters_MTG_Deck = None
    statistics: list = None

    def __init__(self, parameters_deck=None, hash_page_current=Model_View_MTG_Base.HASH_PAGE_MTG_DECKS):
        _m = 'Model_View_MTG_Decks.__init__'
        # Helper_App.console_log(f'{_m}\nstarting...')
        super().__init__(hash_page_current=hash_page_current)
        self._title = 'MTG Decks'
        datastore = DataStore_MTG()

        # Get all decks
        parameters_deck = Parameters_MTG_Deck.get_default(self.user.user_id)
        self.decks, errors = datastore.get_many_mtg_deck(parameters_deck)

        # Helper_App.console_log(f'Decks IDs: {self.decks}')
        # Get all commander brackets
        parameters_commander_bracket = Parameters_MTG_Deck_Commander_Bracket.get_default()
        parameters_commander_bracket.get_all_commander_bracket = True
        parameters_commander_bracket.require_all_id_filters_met = False
        parameters_commander_bracket.require_any_id_filters_met = False
        self.commander_brackets, errors = datastore.get_many_mtg_deck_commander_bracket(parameters_commander_bracket = parameters_commander_bracket)

        # Helper_App.console_log(f'Brackets: {self.commander_brackets}')
        commander_bracket_index = {}
        for index_commander_bracket in range(len(self.commander_brackets)):
            bracket = self.commander_brackets[index_commander_bracket]
            commander_bracket_index[bracket.commander_bracket_id] = bracket
        # Helper_App.console_log(f'Bracket IDs: {commander_bracket_index}')

        deck_index = {}
        for index_deck in range(len(self.decks)):
            bracket = commander_bracket_index[self.decks[index_deck].commander_bracket_id]
            self.decks[index_deck].commander_bracket = bracket
            deck_index[self.decks[index_deck].deck_id] = self.decks[index_deck]

        # Get all statistics
        parameters_statistic = Parameters_Statistic.get_default()
        parameters_statistic.get_all_statistic = False
        parameters_statistic.entity_type_codes = Model_View_MTG_Decks.DECK_ENTITY_TYPE_CODE
        parameters_statistic.entity_record_ids = ','.join([str(d.deck_id) for d in self.decks])
        parameters_statistic.require_all_id_filters_met = True
        parameters_statistic.require_any_id_filters_met = True
        self.statistics, errors = datastore.get_many_statistic(parameters_statistic = parameters_statistic)

        for index_statistic in range(len(self.statistics)):
            deck = deck_index[int(self.statistics[index_statistic].entity_record_id)]
            self.statistics[index_statistic].entity_record_name = deck.name
            

