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

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

from group.models import GroupPhotoInfo
from pai2.basemodels import CreateUpdateMixin


class OrderInfo(CreateUpdateMixin):
    NOMARK = 0
    ORIGIN = 1

    PHOTO_TYPE = (
        (NOMARK, u'去除水印'),
        (ORIGIN, u'获取高清图'),
    )

    """
    # Trade State of Wechat Query
    SUCCESS ——— 支付成功
    REFUND ——— 转入退款
    NOTPAY ——— 未支付
    CLOSED ——— 已关闭
    REVOKED ——— 已撤销(刷卡支付)
    USERPAYING ——— 用户支付中
    PAYERROR ——— 支付失败(其他原因,如银行返回失败)
    """

    WAITING_PAY = 0
    PAID = 1
    FAIL = 2
    # DELETED = 9

    PAY_STATUS = (
        (WAITING_PAY, u'待支付'),
        (PAID, u'已支付'),
        (FAIL, u'已失败'),
        # (DELETED, u'已删除'),
    )

    order_id = ShortUUIDField(_(u'order_id'), max_length=255, help_text=u'订单唯一标识', db_index=True)

    group_id = models.CharField(_(u'group_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)
    lensman_photo_id = models.CharField(_(u'lensman_photo_id'), max_length=255, blank=True, null=True, help_text=u'摄影师照片唯一标识,同 PhotosInfo 表', db_index=True)

    photo_type = models.IntegerField(_('photo_type'), choices=PHOTO_TYPE, default=NOMARK, help_text=u'购买照片类型')

    from_uid = models.CharField(_(u'from_uid'), max_length=255, help_text=u'付款用户唯一标识', db_index=True)
    to_lid = models.CharField(_(u'to_lid'), max_length=255, blank=True, null=True, help_text=u'收款摄影师唯一标识', db_index=True)
    to_uid = models.CharField(_(u'to_uid'), max_length=255, blank=True, null=True, help_text=u'收款用户唯一标识', db_index=True)

    body = models.CharField(_(u'body'), max_length=255, blank=True, null=True, help_text=u'商品描述')
    total_fee = models.IntegerField(_(u'total_fee'), default=0, help_text=u'总金额')

    trade_type = models.CharField(_('trade_type'), max_length=255, blank=True, null=True, help_text=u'支付方式')

    pay_status = models.IntegerField(_(u'pay_status'), choices=PAY_STATUS, default=WAITING_PAY, help_text=u'支付状态', db_index=True)
    paid_at = models.DateTimeField(_(u'paid_at'), blank=True, null=True, help_text=_(u'支付时间'))

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

    def __unicode__(self):
        return u'{0.pk}'.format(self)

    def data(self, user_id=None):
        try:
            group_photo = GroupPhotoInfo.objects.get(pk=self.photo_id)
        except GroupPhotoInfo.DoesNotExist:
            group_photo = {}
        return {
            'order_id': self.order_id,
            'from_uid': self.from_uid,
            'group_id': self.group_id,
            'photo_id': self.photo_id,
            'group_photo_info': group_photo and group_photo.photo_info(user_id),
            'to_lid': self.to_lid,
            'to_uid': self.to_uid,
            'body': self.body,
            'total_fee': self.total_fee,
            'pay_status': self.pay_status,
            'paid_at': self.paid_at and self.paid_at.replace(microsecond=0),
            'created_at': self.created_at.replace(microsecond=0),
        }