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

Technology: Business Objects
Feature:    MTG Game Player 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 import MTG_Deck
from business_objects.tcg.mtg_game import MTG_Game
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 import Uuid
from sqlalchemy.types import Text, Boolean
# import uuid

class MTG_Game_Player(SQLAlchemy_ABC, Base):
    ATTR_PLAYER_ID: ClassVar[str] = 'player_id'
    FLAG_PLAYER: ClassVar[str] = 'player'
    FLAG_NOTES: ClassVar[str] = 'notes'
    FLAG_DISPLAY_ORDER: ClassVar[str] = 'display_order'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_PLAYER_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME

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

    player_id = db.Column(db.Integer, primary_key=True)
    game_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    deck_id = db.Column(db.Integer)
    name = db.Column(db.Text)
    notes = db.Column(db.Text)
    display_order = 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)

    def __init__(self):
        self.player_id = 0
        self.is_new = False
        super().__init__()

    @classmethod
    def from_db_mtg_game_player(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_game_player'
        player = cls()
        player.player_id = query_row[0]
        player.game_id = query_row[1]
        player.user_id = query_row[2]
        player.deck_id = query_row[3]
        player.name = query_row[4]
        player.notes = query_row[5]
        player.display_order = query_row[6]
        player.active = av.input_bool(query_row[7], cls.FLAG_ACTIVE, _m)
        player.created_on = query_row[8]
        player.created_by_user_id = query_row[9]
        return player

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        player = cls()
        if json is None: return player
        player.player_id = json.get(cls.ATTR_PLAYER_ID, -1)
        player.game_id = json.get(MTG_Game.ATTR_GAME_ID, None)
        player.user_id = json.get(Base.ATTR_USER_ID, None)
        player.deck_id = json.get(MTG_Deck.ATTR_DECK_ID, None)
        player.name = json.get(cls.FLAG_NAME, None)
        player.notes = json.get(cls.FLAG_NOTES, None)
        player.display_order = json.get(cls.FLAG_DISPLAY_ORDER, 0)
        player.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        player.created_on = json.get(cls.FLAG_CREATED_ON, None)
        player.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        return player

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_PLAYER_ID: self.player_id
            , MTG_Game.ATTR_GAME_ID: self.game_id
            , Base.ATTR_USER_ID: self.user_id
            , MTG_Deck.ATTR_DECK_ID: self.deck_id
            , self.FLAG_NAME: self.name
            , self.FLAG_NOTES: self.notes
            , self.FLAG_DISPLAY_ORDER: self.display_order
            , self.FLAG_ACTIVE: self.active
            , self.FLAG_CREATED_ON: self.created_on
        }
        return as_json

    def __repr__(self):
        return f'''
{self.__class__.__name__}(
    {self.ATTR_PLAYER_ID}: {self.player_id}
    {MTG_Game.ATTR_GAME_ID}: {self.game_id}
    {Base.ATTR_USER_ID}: {self.user_id}
    {MTG_Deck.ATTR_DECK_ID}: {self.deck_id}
    {self.FLAG_NAME}: {self.name}
    {self.FLAG_NOTES}: {self.notes}
    {self.FLAG_DISPLAY_ORDER}: {self.display_order}
    {self.FLAG_ACTIVE}: {self.active}
    {self.FLAG_CREATED_ON}: {self.created_on}
)
            '''

class MTG_Game_Player_Temp(db.Model, Base):
    __tablename__ = 'tcg_mtg_game_player_temp'
    __table_args__ = { 'extend_existing': True }
    temp_id = db.Column(db.Integer, primary_key=True)
    player_id = db.Column(db.Integer)
    game_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    deck_id = db.Column(db.Integer)
    name = db.Column(db.Text)
    notes = db.Column(db.Text)
    display_order = db.Column(db.Integer)
    active = db.Column(db.Boolean)
    created_on = db.Column(db.DateTime)
    guid = db.Column(Uuid) #, default = uuid.uuid4)

    def __init__(self):
        super().__init__()

    @classmethod
    def from_player(cls, player, guid):
        _m = 'MTG_Game_Player_Temp.from_player'
        temp = cls()
        temp.player_id = player.player_id
        temp.game_id = player.game_id
        temp.user_id = player.user_id
        temp.deck_id = player.deck_id
        temp.name = player.name
        temp.notes = player.notes
        temp.display_order = player.display_order
        temp.active = player.active
        temp.created_on = player.created_on
        temp.guid = guid
        return temp


class Parameters_MTG_Game_Player(Get_Many_Parameters_Base):
    get_all_game: bool
    get_inactive_game: bool
    game_ids: str
    require_all_id_filters_met: bool
    require_any_id_filters_met: bool

    @classmethod
    def get_default(cls, user_id_session):
        return cls(
              get_all_game = True
            , get_inactive_game = False
            , game_ids = ''
            , require_all_id_filters_met = True
            , require_any_id_filters_met = True
        )

    @classmethod
    def from_json(cls, json):
        return cls(
              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', '')
            , 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)
        )

    def to_json(self):
        return {
              'a_get_all_game': self.get_all_game
            , 'a_get_inactive_game': self.get_inactive_game
            , 'a_game_ids': self.game_ids
            , 'a_require_all_id_filters_met': self.require_all_id_filters_met
            , 'a_require_any_id_filters_met': self.require_any_id_filters_met
        }

    @staticmethod
    def get_type_hints():
        return {
              'a_get_all_game': Boolean
            , 'a_get_inactive_game': Boolean
            , 'a_game_ids': Text
            , 'a_require_all_id_filters_met': Boolean
            , 'a_require_any_id_filters_met': Boolean
        }
