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

from django.db import models
from django.utils.translation import ugettext_lazy as _
from shortuuidfield import ShortUUIDField
from TimeConvert import TimeConvert as tc

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


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

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

    WANTED = 0
    FETCHED = 1
    DELETED = 2

    PHOTO_STATUS = (
        (WANTED, u'待上传'),
        (FETCHED, u'已上传'),
        (DELETED, 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)

    prepay_id = models.CharField(_('prepay_id'), max_length=255, blank=True, null=True, help_text=u'预支付交易会话标识')
    transaction_id = models.CharField(_('transaction_id'), max_length=255, blank=True, null=True, help_text=u'微信订单号')

    group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
    session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识,同 PhotosInfo 表', 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'购买照片类型', db_index=True)
    photo_status = models.IntegerField(_(u'photo_status'), choices=PHOTO_STATUS, default=WANTED, help_text=_(u'购买照片状态, 标识用户是否已经获得照片'), db_index=True)

    from_uid = models.CharField(_(u'from_uid'), max_length=255, 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'支付时间'))

    reback_status = models.BooleanField(_(u'reback_status'), default=False, help_text=u'退款状态', db_index=True)
    reback_at = models.DateTimeField(_(u'reback_at'), blank=True, null=True, help_text=_(u'退款时间'))

    # 微信统一下单
    unifiedorder_result = models.TextField(_(u'unifiedorder_result'), blank=True, null=True, help_text=_(u'统一下单结果'))
    # 微信支付回调
    notify_msg = models.TextField(_('notify_msg'), 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(photo_id=self.photo_id)
        except GroupPhotoInfo.DoesNotExist:
            group_photo = None
        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_uid': self.to_uid,
            'body': self.body,
            'total_fee': self.total_fee,
            'pay_status': self.pay_status,
            'paid_at': tc.remove_microsecond(self.paid_at),
            'created_at': tc.remove_microsecond(self.created_at),
        }

    @property
    def lensdata(self, user_id=None):
        try:
            group_photo = GroupPhotoInfo.objects.get(photo_id=self.photo_id)
        except GroupPhotoInfo.DoesNotExist:
            group_photo = None
        return {
            'order_id': self.order_id,
            'session_id': self.session_id,
            'photo_id': self.lensman_photo_id,
            'group_photo_info': group_photo and group_photo.photo_info(user_id),
        }