#!/usr/bin/env python3

from cuchulu.libraries.i18n import I18n
from cuchulu.libraries.db.webmodel import WebModel
#from cuchulu.libraries.db.usermodel import UserModel
from cuchulu.libraries.db import corefields
from cuchulu.libraries.db.extrafields.emailfield import EmailField
from cuchulu.libraries.db.extrafields.passwordfield import PasswordField
from cuchulu.libraries.db.extrafields.langfield import LangField
from cuchulu.libraries.db.extrafields.ipfield import IpField
from cuchulu.libraries.db.extrafields.datetimefield import DateTimeField
from cuchulu.libraries.db.usermodel import UserModel


class ModuleField(corefields.CharField):
    
    def show_formatted(self, value):
        
        return I18n.lang('admin2', value, value)

class PrivilegesField2(corefields.IntegerField):

    def show_formatted(self, value):
        
        value=int(value)
        
        if value==0:
            return _('Without privileges')
        elif value==1:
            return _('Selected privileges')
        elif value==2:
            return _('Administrator')

class UserAdmin2(UserModel):
    
    #def create_fields(self):
    def __init__(self, connection=None):

        super().__init__(connection)

        # I can change other fields here, how the name.
        
        self.register(corefields.CharField('username'))
        
        self.fields['username'].required=True
        
        self.fields['username'].label=_('Username')
        
        self.register(PasswordField('password'))

        self.fields['password'].required=True
        
        self.register(EmailField('email'))

        self.fields['email'].required=True
        
        self.register(corefields.CharField('token_recovery'))

        self.register(corefields.CharField('token_login'))
        
        self.register(PasswordField('token_auth'))
        
        self.register(PasswordField('token_key'))
        
        self.register(PrivilegesField2('privileges'))
        
        self.register(LangField('lang', 20))
        
        self.register(corefields.BooleanField('disabled'))
        
        self.register(corefields.BooleanField('double_auth'))
        
        self.register(corefields.BooleanField('dark_theme'))
        
        #self.register(corefields.IntegerField('num_tries', 1))
        
        self.register(DateTimeField('last_login'))

class LoginTries2(WebModel):
    
    def __init__(self, connection=None):

        super().__init__(connection)
        self.register(IpField('ip'))
        self.register(corefields.IntegerField('num_tries', 1))
        self.register(DateTimeField('last_login'))
        
class PrivilegesModule2(WebModel):
    
    def __init__(self, connection=None):

        super().__init__(connection)
        self.register(corefields.ForeignKeyField('user_id', UserAdmin2(connection), 11, False, 'id', 'username', select_fields=[]), True)
        self.register(ModuleField('module'), True)
        
