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

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

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


class MTG_Game(SQLAlchemy_ABC, Base):
    ATTR_GAME_ID: ClassVar[str] = 'game_id'
    FLAG_END_ON: ClassVar[str] = 'end_on'
    FLAG_GAME: ClassVar[str] = 'game'
    FLAG_IS_COMMANDER: ClassVar[str] = 'is_commander'
    FLAG_IS_DRAFT: ClassVar[str] = 'is_draft'
    FLAG_IS_SEALED: ClassVar[str] = 'is_sealed'
    FLAG_LOCATION_NAME: ClassVar[str] = 'location_name'
    FLAG_NOTES: ClassVar[str] = 'notes'
    FLAG_START_ON: ClassVar[str] = 'start_on'
    FLAG_STARTING_LIFE: ClassVar[str] = 'starting_life'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_GAME_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = FLAG_START_ON

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

    game_id = db.Column(db.Integer, primary_key=True)
    notes = db.Column(db.Text)
    is_commander = db.Column(db.Boolean)
    is_draft = db.Column(db.Boolean)
    is_sealed = db.Column(db.Boolean)
    location_name = db.Column(db.Text)
    start_on = db.Column(db.DateTime)
    end_on = db.Column(db.DateTime)
    starting_life = 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.game_id = 0
        self.is_new = False
        super().__init__()

    @classmethod
    def from_db_mtg_game(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_game'
        game = cls()
        game.game_id = query_row[0]
        game.notes = query_row[1]
        game.is_commander = av.input_bool(query_row[2], cls.FLAG_IS_COMMANDER, _m)
        game.is_draft = av.input_bool(query_row[3], cls.FLAG_IS_DRAFT, _m)
        game.is_sealed = av.input_bool(query_row[4], cls.FLAG_IS_SEALED, _m)
        game.location_name = query_row[5]
        game.start_on = query_row[6]
        game.end_on = query_row[7]
        game.starting_life = query_row[8]
        game.active = av.input_bool(query_row[9], cls.FLAG_ACTIVE, _m)
        game.created_on = query_row[10]
        game.created_by_user_id = query_row[11]
        game.updated_last_on = query_row[12]
        game.updated_last_by_user_id = query_row[13]
        return game

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        game = cls()
        if json is None: return game
        game.game_id = json.get(cls.ATTR_GAME_ID, -1)
        game.notes = json.get(cls.FLAG_NOTES, None)
        game.is_commander = av.input_bool(json.get(cls.FLAG_IS_COMMANDER, True), cls.FLAG_IS_COMMANDER, _m)
        game.is_draft = av.input_bool(json.get(cls.FLAG_IS_DRAFT, False), cls.FLAG_IS_DRAFT, _m)
        game.is_sealed = av.input_bool(json.get(cls.FLAG_IS_SEALED, False), cls.FLAG_IS_SEALED, _m)
        game.location_name = json.get(cls.FLAG_LOCATION_NAME, None)
        game.start_on = json.get(cls.FLAG_START_ON, None)
        game.starting_life = json.get(cls.FLAG_STARTING_LIFE, 40)
        game.end_on = json.get(cls.FLAG_END_ON, None)
        game.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        _created_on = json.get(Base.FLAG_CREATED_ON, None)
        if isinstance(_created_on, str):
            _created_on = datetime.strptime(_created_on, '%a, %d %b %Y %H:%M:%S %Z')
        game.created_on = _created_on
        game.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        _updated_last_on = json.get(Base.FLAG_UPDATED_LAST_ON, None)
        if isinstance(_updated_last_on, str):
            _updated_last_on = datetime.strptime(_updated_last_on, '%a, %d %b %Y %H:%M:%S %Z')
        game.updated_last_on = _updated_last_on
        game.updated_last_by_user_id = json.get(Base.FLAG_UPDATED_LAST_BY_USER_ID, None)
        return game

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_GAME_ID: self.game_id
            , self.FLAG_NOTES: self.notes
            , self.FLAG_IS_COMMANDER: self.is_commander
            , self.FLAG_IS_DRAFT: self.is_draft
            , self.FLAG_IS_SEALED: self.is_sealed
            , self.FLAG_LOCATION_NAME: self.location_name
            , self.FLAG_START_ON: self.start_on
            , self.FLAG_STARTING_LIFE: self.starting_life
            , self.FLAG_END_ON: self.end_on
            , self.FLAG_ACTIVE: self.active
            , self.FLAG_CREATED_ON: self.created_on
            , self.FLAG_CREATED_BY_USER_ID: self.created_by_user_id
            , self.FLAG_UPDATED_LAST_ON: self.updated_last_on
            , self.FLAG_UPDATED_LAST_BY_USER_ID: self.updated_last_by_user_id
        }
        return as_json

    def __repr__(self):
        return f'''
{self.__class__.__name__}(
    {self.ATTR_GAME_ID}: {self.game_id}
    {self.FLAG_NOTES}: {self.notes}
    {self.FLAG_IS_COMMANDER}: {self.is_commander}
    {self.FLAG_IS_DRAFT}: {self.is_draft}
    {self.FLAG_IS_SEALED}: {self.is_sealed}
    {self.FLAG_LOCATION_NAME}: {self.location_name}
    {self.FLAG_START_ON}: {self.start_on}
    {self.FLAG_END_ON}: {self.end_on}
    {self.FLAG_STARTING_LIFE}: {self.starting_life}
    {self.FLAG_ACTIVE}: {self.active}
    {self.FLAG_CREATED_ON}: {self.created_on}
    {self.FLAG_CREATED_BY_USER_ID}: {self.created_by_user_id}
    {self.FLAG_UPDATED_LAST_ON}: {self.updated_last_on}
    {self.FLAG_UPDATED_LAST_BY_USER_ID}: {self.updated_last_by_user_id}
)
            '''

class Parameters_MTG_Game(Get_Many_Parameters_Base):
    get_all_game: bool
    get_inactive_game: bool
    game_ids: str
    get_all_user: bool
    get_inactive_user: bool
    user_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 = ''
            , get_all_user = False
            , get_inactive_user = False
            , user_ids = '' if user_id_session is None else f'{user_id_session}'
            , 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', '')
            , get_all_user = json.get('a_get_all_user', False)
            , get_inactive_user = json.get('a_get_inactive_user', False)
            , user_ids = json.get('a_user_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': str(self.game_ids)
            , 'a_get_all_user': self.get_all_user
            , 'a_get_inactive_user': self.get_inactive_user
            , 'a_user_ids': str(self.user_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_get_all_user': Boolean
            , 'a_get_inactive_user': Boolean
            , 'a_user_ids': Text
            , 'a_require_all_id_filters_met': Boolean
            , 'a_require_any_id_filters_met': Boolean
        }