# -*- 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
from utils.redis.rgroup import get_group_photo_thumbup_flag
from utils.redis.rorder import get_lensman_order_record
from utils.url_utils import img_url, share_url


r = settings.REDIS_CACHE


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'用户唯一标识', db_index=True)
    group_name = models.CharField(_(u'group_name'), max_length=255, blank=True, null=True, help_text=u'群组名称')
    group_default_avatar = models.IntegerField(_(u'group_default_avatar'), default=0, help_text=u'群组默认头像，0 - 255，水果头像')
    group_avatar = models.CharField(_(u'group_avatar'), 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 group_avatar_url(self):
        return img_url(self.group_avatar)

    @property
    def data(self):
        return {
            'group_id': self.group_id,
            'admin_id': self.admin_id,
            'group_name': self.group_name,
            'group_default_avatar': self.group_default_avatar,
            'group_avatar': self.group_avatar_url,
            'group_desc': self.group_desc,
            'group_from': self.group_from,
            'group_lock': self.group_lock,
            'created_at': self.created_at.replace(microsecond=0),
        }

    def users(self, admin=True, user_id=None):
        all_users = GroupUserInfo.objects.filter(group_id=self.group_id, user_status__in=[GroupUserInfo.APPLYING, GroupUserInfo.PASSED])

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

        if admin and 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
    QUIT = 4

    USER_STATUS = (
        (APPLYING, u'申请中'),
        (PASSED, u'已通过'),
        (REFUSED, u'已拒绝'),
        (DELETED, u'已删除'),
        (QUIT, 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'用户唯一标识', db_index=True)
    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'删除时间'))
    quit_at = models.DateTimeField(_(u'quit_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):
    APP_GROUP = 0
    SESSION_GROUP = 1

    PHOTO_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)
    user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    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'用户头像')

    photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
    photo_w = models.IntegerField(_(u'photo_w'), default=0, help_text=u'照片宽度')
    photo_h = models.IntegerField(_(u'photo_h'), default=0, help_text=u'照片高度')

    # 双列: 540, 40-50K
    photo_thumbnail_path = models.CharField(_(u'photo_thumbnail_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
    photo_thumbnail_w = models.IntegerField(_(u'photo_thumbnail_w'), default=0, help_text=u'照片缩略图宽度')
    photo_thumbnail_h = models.IntegerField(_(u'photo_thumbnail_h'), default=0, help_text=u'照片缩略图高度')

    # 单列: 1080, xx-100K
    photo_thumbnail2_path = models.CharField(_(u'photo_thumbnail2_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
    photo_thumbnail2_w = models.IntegerField(_(u'photo_thumbnail2_w'), default=0, help_text=u'照片缩略图宽度')
    photo_thumbnail2_h = models.IntegerField(_(u'photo_thumbnail2_h'), default=0, help_text=u'照片缩略图高度')

    comment_num = models.IntegerField(_(u'comment_num'), default=0, help_text=u'照片评论数量')
    thumbup_num = models.IntegerField(_(u'thumbup_num'), default=0, help_text=u'照片点赞数量')

    photo_from = models.IntegerField(_(u'photo_from'), choices=PHOTO_FROM, default=APP_GROUP, help_text=u'照片来源')

    session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识，同 PhotosInfo 表', db_index=True)
    lensman_id = models.CharField(_(u'lensman_id'), max_length=255, blank=True, null=True, help_text=u'摄影师唯一标识，同 PhotosInfo 表', db_index=True)
    lensman_photo_id = models.CharField(_(u'lensman_photo_id'), max_length=255, blank=True, null=True, help_text=u'摄影师照片唯一标识，同 PhotosInfo 表', db_index=True)

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

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

    @property
    def photo_url(self):
        return img_url(self.photo_path)

    @property
    def photo_thumbnail_url(self):
        return img_url(self.photo_thumbnail_path)

    @property
    def photo_thumbnail2_url(self):
        return img_url(self.photo_thumbnail2_path)

    @property
    def photo_share_url(self):
        return share_url(self.pk)

    @property
    def photo_data(self):
        return {
            'photo_id': self.pk,
            'comment_num': self.comment_num,
            'thumbup_num': self.thumbup_num,
        }

    def photo_info(self, user_id):
        try:
            group = GroupInfo.objects.get(group_id=self.group_id)
        except GroupInfo.DoesNotExist:
            group = None
        porder = get_lensman_order_record(self.pk, user_id) if self.photo_from == GroupPhotoInfo.SESSION_GROUP else {}
        return {
            'group_id': group and group.group_id,
            'group_name': group and group.group_name,
            'group_default_avatar': group and group.group_default_avatar,
            'group_avatar': group and group.group_avatar_url,
            'group_from': group and group.group_from,
            'photo_id': self.pk,
            'photo_url': self.photo_url,
            'photo_w': self.photo_w,
            'photo_h': self.photo_h,
            'photo_thumbnail_url': self.photo_thumbnail_url,
            'photo_thumbnail_w': self.photo_thumbnail_w,
            'photo_thumbnail_h': self.photo_thumbnail_h,
            'photo_thumbnail2_url': self.photo_thumbnail2_url,
            'photo_thumbnail2_w': self.photo_thumbnail2_w,
            'photo_thumbnail2_h': self.photo_thumbnail2_h,
            'photo_share_url': self.photo_share_url,
            'user_id': self.user_id,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'comment_num': self.comment_num,
            'thumbup': get_group_photo_thumbup_flag(self.pk, user_id),
            'thumbup_num': self.thumbup_num,
            'photo_from': self.photo_from,
            'porder': porder,
            'created_at': self.created_at.replace(microsecond=0),
        }


class GroupPhotoOrderInfo(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'用户唯一标识', db_index=True)
    photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'照片唯一标识', db_index=True)

    m_photo_path = models.CharField(_(u'm_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径，控制器上传，无水印')
    l_photo_path = models.CharField(_(u'l_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径，美化大图')
    r_photo_path = models.CharField(_(u'r_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径，高清大图')

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

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

    @property
    def m_photo_url(self):
        return img_url(self.m_photo_path)

    @property
    def l_photo_url(self):
        return img_url(self.l_photo_path)

    @property
    def r_photo_url(self):
        return img_url(self.r_photo_path)

    @property
    def porder_info(self):
        return {
            'm_photo_url': self.m_photo_url,
            'l_photo_url': self.l_photo_url,
            'r_photo_url': self.r_photo_url,
        }


class PhotoCommentInfo(CreateUpdateMixin):
    photo_id = models.CharField(_(u'photo_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'用户唯一标识', db_index=True)
    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'用户头像')
    comment = models.TextField(_(u'comment'), blank=True, null=True, help_text=u'用户评论')

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

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

    @property
    def comment_info(self):
        return {
            'user_id': self.user_id,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'comment': self.comment,
            'created_at': self.created_at.replace(microsecond=0),
        }


class PhotoThumbUpInfo(CreateUpdateMixin):
    photo_id = models.CharField(_(u'photo_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'用户唯一标识', db_index=True)
    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'用户头像')
    thumbup = models.BooleanField(_(u'thumbup'), default=True, help_text=u'用户点赞', db_index=True)

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

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

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