from typing import Dict
from langchain.schema.document import Document

from qdrant import QwinQdrantClient
from db import QwinCasinoDao
from qwin_chat_bot_domain import GameType, GameCategory

game_details_template = '''- Game id: {}, game time: {}
- Staked: {} qwinB, returned: {} qwinB 
'''

slot_game_details_template = '''- Lines: {}, coins per line: {}, coin value: {} qwinB
- Slot state in symbol codes: {} 
  For answering use symbol names from symbol paytable above instead of symbol codes  
- Winning lines: {}
- Freespins: {}
- Bonus games: {}
'''

def game_details(game: Dict):
    return game_details_template.format(
        game['id'],
        game['c_date'].strftime('%Y-%m-%d %H:%M:%S'),
        game['staked'],
        game['returned']
    )

def slot_game_details(game: Dict):
    state = game['states'][0]
    state_reels = [str(reel) for reel in state['stateList']]
    
    winning_lines = 'no'
    if 'winnings' in state['winList']:
        winning_lines = [w['line'] for w in state['winList']]
    
    bonus_games = 0
    if 'bonusGames' in state['winList']:
        bonus_games = len(state['winList']['bonusGames'])
    
    return slot_game_details_template.format(
        game['lines'],
        game['coins_per_line'],
        game['coin_value'],
        ' '.join(state_reels),
        winning_lines,
        game['free_spins'],
        bonus_games
    )

game_details_func_holder = {
    GameCategory.SLOT: slot_game_details
}

class PlayerGameInfoProvider:
    def __init__(self, user_id: str, qdrant: QwinQdrantClient, db: QwinCasinoDao):
        self.user_id = user_id
        self.qdrant = qdrant
        self.db = db
    
    def game_summary(self, query: Dict) -> [Document]:
        game_type = GameType.by_code(query['game_codes'][0])
        if not game_type:
            return [Document(page_content='Ask the user to clarify game name, game id and all details he can provide. It can be found on QWIN web site in "History" section')]
        
        game_category = game_type.category
        
        game_id = query['game_id']
        if not game_id:
            return [Document(page_content='Ask the user to clarify game id. It can be found on QWIN web site in "History" section')]
        
        if not self.user_id.isdigit():
            return [Document('User is not logged. Ask user to log in to continying')]
        
        if game_id == 'last':
            games = self.db.get_last_game(self.user_id, game_type)
            if not games:
                return [Document(page_content='Provided game id is incorrect. Ask the user to clarify game id. It can be found on QWIN web site in "History" section')]
            
        else:
            games = self.db.get_game(self.user_id, game_type, game_id)
            if not games:
                return [Document(page_content='Provided game id is incorrect. Ask the user to clarify game id. It can be found on QWIN web site in "History" section')]
        
        game = games[0]
        docs = []
        
        q_search = self.qdrant.search_game_docs(game_codes=[game_type.code])
        docs.append(Document(page_content=q_search[0]))
        
        
        docs.append(Document(page_content=game_details(game)))
        
        details_func = game_details_func_holder[game_category]
        docs.append(Document(page_content=details_func(game)))
        
        return docs
        
        
        # return [Document(page_content='User lost because he is looser. He will never win.')]