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

Technology: Business Objects
Feature:    MTG Deck Business Object
"""

# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
from business_objects.tcg.mtg_deck_commander_bracket import MTG_Deck_Commander_Bracket
import lib.argument_validation as av
from extensions import db
from helpers.helper_app import Helper_App
# external
from dataclasses import dataclass
from typing import ClassVar
from sqlalchemy.types import Text, Boolean


class MTG_Deck(SQLAlchemy_ABC, Base):
    ATTR_DECK_ID: ClassVar[str] = 'deck_id'
    ATTR_COMMANDER_BRACKET_ID: ClassVar[str] = 'commander_bracket_id'
    FLAG_DECK: ClassVar[str] = 'deck'
    FLAG_IS_COMMANDER: ClassVar[str] = 'is_commander'
    FLAG_STATISTICS: ClassVar[str] = 'statistics'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_DECK_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME

    __tablename__ = 'tcg_mtg_deck'
    __table_args__ = { 'extend_existing': True }

    deck_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    is_commander = db.Column(db.Boolean)
    commander_bracket_id = db.Column(db.Integer)
    active = db.Column(db.Boolean)
    created_on = db.Column(db.DateTime)
    created_by_user_id = db.Column(db.Integer)
    updated_last_on = db.Column(db.DateTime)
    updated_last_by_user_id = db.Column(db.Integer)
    change_set_id = db.Column(db.Integer)

    # commander_bracket: MTG_Deck_Commander_Bracket

    def __init__(self):
        self.deck_id = 0
        self.is_new = False
        self.commander_bracket = None
        self.statistics = None
        super().__init__()

    @classmethod
    def from_db_mtg_deck(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_deck'
        deck = cls()
        deck.deck_id = query_row[0]
        deck.name = query_row[1]
        deck.is_commander = av.input_bool(query_row[2], cls.FLAG_IS_COMMANDER, _m)
        deck.commander_bracket_id = query_row[3]
        deck.active = av.input_bool(query_row[4], cls.FLAG_ACTIVE, _m)
        deck.created_on = query_row[5]
        deck.created_by_user_id = query_row[6]
        return deck

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        deck = cls()
        if json is None: return deck
        deck.deck_id = json.get(cls.ATTR_DECK_ID, -1)
        deck.name = json.get(cls.FLAG_NAME, None)
        deck.is_commander = av.input_bool(json.get(cls.FLAG_IS_COMMANDER, True), cls.FLAG_IS_COMMANDER, _m)
        deck.commander_bracket_id = json.get(cls.ATTR_COMMANDER_BRACKET_ID, None)
        deck.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        deck.created_on = json.get(cls.FLAG_CREATED_ON, None)
        deck.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        return deck

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_DECK_ID: self.deck_id
            , self.FLAG_NAME: self.name
            , self.FLAG_IS_COMMANDER: self.is_commander
            , self.ATTR_COMMANDER_BRACKET_ID: self.commander_bracket_id
            , self.FLAG_ACTIVE: self.active
            , self.FLAG_CREATED_ON: self.created_on
            , Base.ATTR_USER_ID: self.created_by_user_id
            , self.FLAG_STATISTICS: self.statistics
        }
        return as_json

    def __repr__(self):
        return f'''
{self.__class__.__name__}(
    {self.ATTR_DECK_ID}: {self.deck_id}
    {self.FLAG_NAME}: {self.name}
    {self.FLAG_IS_COMMANDER}: {self.is_commander}
    {self.ATTR_COMMANDER_BRACKET_ID}: {self.commander_bracket_id}
    {self.FLAG_ACTIVE}: {self.active}
    {self.FLAG_CREATED_ON}: {self.created_on}
    {Base.ATTR_USER_ID}: {self.created_by_user_id}
)
            '''


class Parameters_MTG_Deck(Get_Many_Parameters_Base):
    get_all_deck: bool
    get_inactive_deck: bool
    deck_ids: str
    deck_names: str
    commander_bracket_ids: str
    include_commander_option: bool
    created_by_user_ids: str
    get_all_game: bool
    get_inactive_game: bool
    game_ids: str
    filter_by_game_session_not_player: bool
    require_all_id_filters_met: bool
    require_any_id_filters_met: bool
    require_all_non_id_filters_met: bool
    require_any_non_id_filters_met: bool

    @classmethod
    def get_default(cls, user_id):
        return cls(
              get_all_deck = False
            , get_inactive_deck = False
            , deck_ids = ''
            , deck_names = ''
            , commander_bracket_ids = ''
            , include_commander_option = True
            , created_by_user_ids = str(user_id)
            , get_all_game = False
            , get_inactive_game = False
            , game_ids = ''
            , filter_by_game_session_not_player = False
            , require_all_id_filters_met = False
            , require_any_id_filters_met = True
            , require_all_non_id_filters_met = False
            , require_any_non_id_filters_met = False
        )

    @classmethod
    def from_json(cls, json):
        return cls(
              get_all_deck = json.get('a_get_all_deck', False)
            , get_inactive_deck = json.get('a_get_inactive_deck', False)
            , deck_ids = json.get('a_deck_ids', '')
            , deck_names = json.get('a_deck_names', '')
            , commander_bracket_ids = json.get('a_commander_bracket_ids', '')
            , include_commander_option = json.get('a_include_commander_option', True)
            , created_by_user_ids = json.get('a_created_by_user_ids', '')
            , get_all_game = json.get('a_get_all_game', False)
            , get_inactive_game = json.get('a_get_inactive_game', False)
            , game_ids = json.get('a_game_ids', '')
            , filter_by_game_session_not_player = json.get('a_filter_by_game_session_not_player', True)
            , require_all_id_filters_met = json.get('a_require_all_id_filters_met', True)
            , require_any_id_filters_met = json.get('a_require_any_id_filters_met', True)
            , require_all_non_id_filters_met = json.get('a_require_all_non_id_filters_met', False)
            , require_any_non_id_filters_met = json.get('a_require_any_non_id_filters_met', True)
        )

    def to_json(self):
        return {
              'a_get_all_deck': self.get_all_deck
            , 'a_get_inactive_deck': self.get_inactive_deck
            , 'a_deck_ids': self.deck_ids
            , 'a_deck_names': self.deck_names
            , 'a_commander_bracket_ids': self.commander_bracket_ids
            , 'a_include_commander_option': self.include_commander_option
            , 'a_created_by_user_ids': self.created_by_user_ids
            , 'a_get_all_game': self.get_all_game
            , 'a_get_inactive_game': self.get_inactive_game
            , 'a_game_ids': self.game_ids
            , 'a_filter_by_game_session_not_player': self.filter_by_game_session_not_player
            , 'a_require_all_id_filters_met': self.require_all_id_filters_met
            , 'a_require_any_id_filters_met': self.require_any_id_filters_met
            , 'a_require_all_non_id_filters_met': self.require_all_non_id_filters_met
            , 'a_require_any_non_id_filters_met': self.require_any_non_id_filters_met
        }

    @staticmethod
    def get_type_hints():
        return {
              'a_get_all_deck': Boolean
            , 'a_get_inactive_deck': Boolean
            , 'a_deck_ids': Text
            , 'a_deck_names': Text
            , 'a_commander_bracket_ids': Text
            , 'a_include_commander_option': Boolean
            , 'a_created_by_user_ids': Text
            , 'a_get_all_game': Boolean
            , 'a_get_inactive_game': Boolean
            , 'a_game_ids': Text
            , 'a_filter_by_game_session_not_player': Boolean
            , 'a_require_all_id_filters_met': Boolean
            , 'a_require_any_id_filters_met': Boolean
            , 'a_require_all_non_id_filters_met': Boolean
            , 'a_require_any_non_id_filters_met': Boolean
        }
