from django.db import models
from BaseModels.base_models import BaseModel
from django.utils.translation import gettext_lazy as _
from AuthApp.models import User

game_status_choices = (
    ('waiting_users', 'Ожидание пользователей'),
    ('game', 'В процессе'),
    ('finished', 'Завершена')
)


class Game(BaseModel):
    level = models.IntegerField(verbose_name=_('Уровень игры'), default=1)
    status = models.CharField(max_length=100, verbose_name=_('Статус'), default='waiting_users')
    time_for_waiting_users = models.IntegerField(verbose_name=_('Время ожидания пользователей (сек)'), default=15)
    cur_lap = models.IntegerField(verbose_name=_('Текущий круг'), default=0)

    winner = models.OneToOneField(User, verbose_name=_(u'id пользователя'), related_name='rel_games_for_user',
                                null=True, blank=True, on_delete=models.CASCADE)

    comment = models.TextField(verbose_name=_('Комментарий'), null=True, blank=True)

    def __str__(self):
        if self.name:
            return f'{self.name}'
        else:
            return str(self.id)


    class Meta:

        verbose_name = _('Игра')
        verbose_name_plural = _('Игры')
        # ordering = ('user__last_name', 'user__first_name')


user_in_game_status_choices = (
    ('waiting_users', 'Ожидание пользователей'),
    ('in_game', 'В игре'),
    ('lose', 'Выбыл'),
    ('finish', 'Завершил игру')
)

class UserInGame(BaseModel):
    game = models.OneToOneField(
        Game, verbose_name=_('Игра'), related_name='rel_userInGame_for_game', on_delete=models.CASCADE)
    user = models.OneToOneField(
        User, verbose_name=_('Игрок'), related_name='rel_userInGame_for_user', on_delete=models.CASCADE)

    last_lap = models.IntegerField(verbose_name=_('Последний круг'), default=0)

    status = models.CharField(max_length=100, verbose_name=_('Статус'), default='waiting_users')

    def __str__(self):
        if self.name:
            return f'{self.name}'
        else:
            return str(self.id)


    class Meta:
        verbose_name = _('Пользователь в игре')
        verbose_name_plural = _('Пользователи в игре')
        # ordering = ('question')


question_in_game_status_choices = (
    ('wait', 'Ожидание ответа'),
    ('answered', 'Ответил'),
)

class QuestionInGameForUser(BaseModel):
    from QuestionsApp.models import Question
    user_in_game = models.OneToOneField(
        UserInGame, verbose_name=_('Игра'), related_name='rel_questionInGame_for_userInGame', on_delete=models.CASCADE)
    question = models.OneToOneField(
        Question, verbose_name=_('Вопрос'), related_name='rel_questionInGame_for_question', on_delete=models.CASCADE)

    status = models.CharField(max_length=100, verbose_name=_('Статус'), default='wait')
    answer_right = models.BooleanField(verbose_name=_('Верный ответ'), default=False)
    use_time_for_answer = models.IntegerField(verbose_name=_('Потрачено времени на ответ (сек)'), default=0)

    def __str__(self):
        if self.name:
            return f'{self.name}'
        else:
            return str(self.id)


    class Meta:
        verbose_name = _('Вопрос в игре')
        verbose_name_plural = _('Вопросы в игре')
        # ordering = ('question')