# -*- coding: utf-8 -*-

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _

from pai2.basemodels import CreateUpdateMixin


class GroupInfo(CreateUpdateMixin):
    APP_GROUP = 0
    SESSION_GROUP = 1

    GROUP_FROM = (
        (APP_GROUP, u'APP 建群'),
        (SESSION_GROUP, u'SESSION 建群'),
    )

    group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True, unique=True)
    admin_id = models.CharField(_(u'admin_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
    group_name = models.CharField(_(u'group_name'), max_length=255, blank=True, null=True, help_text=u'群组名称')
    group_desc = models.TextField(_(u'group_desc'), blank=True, null=True, help_text=u'群组描述')
    group_from = models.IntegerField(_(u'group_from'), choices=GROUP_FROM, default=APP_GROUP, help_text=u'群组来源')
    session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识', db_index=True)
    group_lock = models.BooleanField(_(u'group_lock'), default=False, help_text=u'群组锁定')

    class Meta:
        verbose_name = _(u'groupinfo')
        verbose_name_plural = _(u'groupinfo')

    def __unicode__(self):
        return unicode(self.group_id)

    @property
    def data(self):
        return {
            'group_id': self.group_id,
            'group_name': self.group_name,
            'group_desc': self.group_desc,
            'group_from': self.group_from,
            'group_lock': self.group_lock,
            'created_at': self.created_at,
        }

    def users(self, user_id):
        all_users = GroupUserInfo.objects.filter(group_id=self.group_id)

        passed_users = all_users.filter(user_status=GroupUserInfo.PASSED)
        passed_count = passed_users.count()
        passed = [passed.user_info for passed in passed_users]

        if self.admin_id != user_id:
            return {
                'passed_count': passed_count,
                'passed': passed,
            }

        applying_users = all_users.filter(user_status=GroupUserInfo.APPLYING)
        applying_count = applying_users.count()
        applying = [applying.user_info for applying in applying_users]

        return {
            'applying_count': applying_count,
            'passed_count': passed_count,
            'applying': applying,
            'passed': passed,
        }


class GroupUserInfo(CreateUpdateMixin):
    APPLYING = 0
    PASSED = 1
    REFUSED = 2
    DELETED = 3

    USER_STATUS = (
        (APPLYING, u'申请中'),
        (PASSED, u'已通过'),
        (REFUSED, u'已拒绝'),
    )

    group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
    user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
    current_id = models.IntegerField(_(u'current_id'), default=-1, help_text=u'当前群组照片ID')
    nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
    avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
    admin = models.BooleanField(_(u'admin'), default=False, help_text=u'群组管理员')
    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=APPLYING)
    passed_at = models.DateTimeField(_(u'passed_at'), blank=True, null=True, help_text=_(u'通过时间'))
    refused_at = models.DateTimeField(_(u'refused_at'), blank=True, null=True, help_text=_(u'拒绝时间'))
    deleted_at = models.DateTimeField(_(u'deleted_at'), blank=True, null=True, help_text=_(u'删除时间'))

    class Meta:
        verbose_name = _(u'groupuserinfo')
        verbose_name_plural = _(u'groupuserinfo')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def user_info(self):
        return {
            'user_id': self.user_id,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'admin': self.admin,
        }


class GroupPhotoInfo(CreateUpdateMixin):
    group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
    user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
    nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
    photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
    photo_thumbnail_path = models.CharField(_(u'photo_thumbnail_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')

    class Meta:
        verbose_name = _(u'groupphotoinfo')
        verbose_name_plural = _(u'groupphotoinfo')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def photo_url(self):
        return u'{0}/{1}'.format(settings.IMG_DOMAIN, self.photo_path) if self.photo_path else ''

    @property
    def photo_thumbnail_url(self):
        return u'{0}/{1}'.format(settings.IMG_DOMAIN, self.photo_thumbnail_path) if self.photo_thumbnail_path else ''

    @property
    def photo_info(self):
        return {
            'photo_id': self.pk,
            'photo_path': self.photo_url,
            'photo_thumbnail_path': self.photo_thumbnail_url,
        }