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

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin
from TimeConvert import TimeConvert as tc

from group.models import GroupPhotoInfo
from kodo.basemodels import PaiaiSrcMixin


class UserMessageInfo(BaseModelMixin):
    SYSTEM = 'system'
    COMMENT = 'comment'
    THUMBUP = 'thumbup'

    MESSAGE_TYPE = (
        (SYSTEM, u'系统'),
        (COMMENT, u'评论'),
        (THUMBUP, u'点赞'),
    )

    MESSAGE_TYPE_INFO = [
        {
            'msg_type': SYSTEM,
            'msg_type_desc': u'系统',
            'msg_avatar': settings.SYSTEM_MESSAGE_AVATAR
        }, {
            'msg_type': COMMENT,
            'msg_type_desc': u'评论',
            'msg_avatar': settings.COMMENT_MESSAGE_AVATAR
        }, {
            'msg_type': THUMBUP,
            'msg_type_desc': u'点赞',
            'msg_avatar': settings.THUMBUP_MESSAGE_AVATAR
        }
    ]

    from_uid = models.CharField(_(u'from_uid'), max_length=32, blank=True, null=True, help_text=u'发送消息用户唯一标识', db_index=True)
    from_nickname = models.CharField(_(u'from_nickname'), max_length=255, blank=True, null=True, help_text=u'发送消息用户昵称')
    from_avatar = models.CharField(_(u'from_avatar'), max_length=255, blank=True, null=True, help_text=u'发送消息用户头像')

    to_uid = models.CharField(_(u'to_uid'), max_length=32, blank=True, null=True, help_text=u'接收消息用户唯一标识', db_index=True)

    group_id = models.CharField(_(u'group_id'), max_length=32, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
    photo_id = models.CharField(_(u'photo_id'), max_length=32, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True)

    # TODO: IntegerField?
    msg_type = models.CharField(_(u'msg_type'), max_length=8, default='system', help_text=u'消息类型', db_index=True)
    msg_title = models.CharField(_(u'msg_title'), max_length=255, blank=True, null=True, help_text=u'消息标题')
    msg_content = models.TextField(_(u'msg_content'), blank=True, null=True, help_text=u'消息内容')
    read = models.BooleanField(_(u'read'), default=False, help_text=u'消息是否已读')

    class Meta:
        verbose_name = _('usermessageinfo')
        verbose_name_plural = _('usermessageinfo')

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

    def msg_info(self, user_id=None):
        try:
            group_photo = GroupPhotoInfo.objects.get(photo_id=self.photo_id)
        except GroupPhotoInfo.DoesNotExist:
            group_photo = {}
        return {
            'pk': self.pk,
            'from_uid': self.from_uid,
            'from_nickname': self.from_nickname,
            'from_avatar': self.from_avatar,
            'group_id': self.group_id,
            'photo_id': self.photo_id,
            'group_photo_info': group_photo and group_photo.photo_info(user_id),
            'msg_title': self.msg_title,
            'msg_content': self.msg_content,
            'read': self.read,
            'created_at': tc.remove_microsecond(self.created_at),
        }


class SystemMessageInfo(BaseModelMixin, PaiaiSrcMixin):
    title = models.CharField(_(u'title'), max_length=255, help_text=u'系统消息标题')
    content = models.TextField(_(u'content'), blank=True, null=True, help_text=u'系统消息内容')
    url = models.CharField(_(u'url'), max_length=255, blank=True, null=True, help_text=u'系统消息链接')

    class Meta:
        verbose_name = _('systemmessageinfo')
        verbose_name_plural = _('systemmessageinfo')

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

    def msg_info(self, user_id=None):
        return {
            'pk': self.pk,
            'title': self.title,
            'content': self.content,
            'url': self.url,
            'read': SystemMessageReadInfo.objects.filter(user_id=user_id, msg_id=self.pk, status=True).exists(),
        }


class SystemMessageReadInfo(BaseModelMixin):
    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    msg_id = models.CharField(_(u'msg_id'), max_length=32, blank=True, null=True, help_text=u'系统消息唯一标识', db_index=True)

    class Meta:
        verbose_name = _('systemmessagereadinfo')
        verbose_name_plural = _('systemmessagereadinfo')

        unique_together = (
            ('user_id', 'msg_id'),
        )

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


class SystemMessageDeleteInfo(BaseModelMixin):
    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    msg_id = models.CharField(_(u'msg_id'), max_length=32, blank=True, null=True, help_text=u'系统消息唯一标识', db_index=True)

    class Meta:
        verbose_name = _('systemmessagedeleteinfo')
        verbose_name_plural = _('systemmessagedeleteinfo')

        unique_together = (
            ('user_id', 'msg_id'),
        )

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