# appname/models.py
import datetime
import random, string

from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.contrib.auth.models import BaseUserManager

from allauth.socialaccount.models import SocialAccount



# django.contrib.auth.get_user_model()        get user model in views.py
# django.conf.settings.AUTH_USER_MODEL                    get user model in models.py

# User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
# User.objects.create_superuser('john', 'lennon@thebeatles.com', 'johnpassword')



class CustomUserManager(BaseUserManager):

    use_in_migrations = True

    def _create_user(self, username, email, password, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        if not username:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        username = self.model.normalize_username(username)
        user = self.model(username=username, email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(username, email, password, **extra_fields)

    # args order is username_field, required_fields, password
    def create_superuser(self, username, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(username, email, password, **extra_fields)
        
        

# Better reusability if you reference directly.
# in views:   User = django.contrib.auth.get_user_model()
# in models:  djagno.conf.settings.AUTH_USER_MODEL

class User(AbstractBaseUser, PermissionsMixin):
    """
    A fully featured User model with admin-compliant permissions that uses
    a full-length email field as the username.

    Email and password are required. Other fields are optional.
    """
    
    username = models.CharField(_('username'), max_length=30, unique=True)
    image = models.CharField(max_length=30, blank=True, null=True)
    email = models.EmailField(_('email address'), max_length=254, unique=True, blank=True, null=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_staff = models.BooleanField(_('staff status'), default=False,
        help_text=_('Designates whether the user can log into this admin '
                    'site.'))
    is_active = models.BooleanField(_('active'), default=True,
        help_text=_('Designates whether this user should be treated as '
                    'active. Unselect this instead of deleting accounts.'))
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    perma_code = models.CharField(max_length=60)
    temp_code = models.CharField(max_length=60, blank=True, null=True)
    temp_code_date = models.DateTimeField(blank=True, null=True)

    objects = CustomUserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']     # require when creating superuser

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
        
    def save(self, *args, **kwargs):
        if not self.perma_code:
            self.perma_code = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(60))
        super(User, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return "/users/%s/" % urlquote(self.email)

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        "Returns the short name for the user."
        return self.username
    
    def email_user(self, subject, message, from_email=None):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email])
        
    def get_image(self):
        fb_uid = SocialAccount.objects.filter(user_id=self.id, provider='facebook')
         
        if self.image:
            return '/static/uploads/' + self.image
        elif len(fb_uid):
            print(fb_uid[0].uid)
            return "http://graph.facebook.com/{}/picture?width=50&height=50".format(fb_uid[0].uid)
        else:
            return '/static/img/generic_user.png'
        
    def create_temp_code(self):
        temp_code = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(60))
        self.temp_code = temp_code
        self.temp_code_date = datetime.datetime.now()
        return temp_code
        