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

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, SexModelMixin
from jsonfield import JSONField

from kodo.basemodels import LensmanTypeBoolMixin
from mch.models import SaleclerkInfo
from sales.models import SalesResponsibilityInfo


class LensmanInfo(BaseModelMixin, LensmanTypeBoolMixin):
    MALE = 1
    FEMALE = 0

    SEX_TYPE = (
        (MALE, u'男'),
        (FEMALE, u'女'),
    )

    REFUSED = -1
    UNVERIFIED = 0
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3
    ASSIGN = 10

    USER_STATUS = (
        (REFUSED, u'已拒绝'),
        (UNVERIFIED, u'未验证'),
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
        (ASSIGN, u'已分配'),
    )

    lensman_id = models.CharField(_(u'lensman_id'), max_length=32, blank=True, null=True, help_text=u'摄影师唯一标识', db_index=True, unique=True)

    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 Union ID', db_index=True, unique=True)

    username = models.CharField(_(u'username'), max_length=255, blank=True, null=True, help_text=u'摄影师用户名', db_index=True, unique=True)
    password = models.CharField(_(u'password'), max_length=255, blank=True, null=True, help_text=u'摄影师密码')
    encryption = models.CharField(_(u'encryption'), max_length=255, blank=True, null=True, help_text=u'摄影师密码')

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'摄影师姓名')
    sex = models.IntegerField(_(u'sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.UNKNOWN, help_text=u'摄影师性别')
    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'摄影师电话', db_index=True)
    location = models.CharField(_(u'location'), max_length=255, blank=True, null=True, help_text=u'摄影师地址')

    proportion = models.FloatField(_(u'proportion'), default=1.0, help_text=u'摄影师分成比例(0.0 ~ 1.0)')

    nomark = models.IntegerField(_(u'nomark'), default=299, help_text=u'摄影师无水印价格(分)')
    origin = models.IntegerField(_(u'origin'), default=999, help_text=u'摄影师高清图价格(分)')

    balance = models.IntegerField(_(u'balance'), default=0, help_text=u'摄影师余额(分)')
    freeze_income_balance = models.IntegerField(_(u'freeze_income_balance'), default=0, help_text=u'摄影师收入冻结余额(分)')
    freeze_expense_balance = models.IntegerField(_(u'freeze_expense_balance'), default=0, help_text=u'摄影师支出冻结余额(分)')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=UNVERIFIED, help_text=u'普通摄影师审核状态')
    outtake_status = models.IntegerField(_(u'outtake_status'), choices=USER_STATUS, default=UNVERIFIED, help_text=u'花絮摄影师审核状态')
    refused_reason = models.TextField(_(u'refused_reason'), blank=True, null=True, help_text=u'审核拒绝原因')

    signup_ip = models.CharField(_(u'signup_ip'), max_length=32, blank=True, null=True, help_text=_(u'注册IP'))
    login_ip = models.CharField(_(u'login_ip'), max_length=32, blank=True, null=True, help_text=_(u'登录IP'))
    login_at = models.DateTimeField(_(u'login_at'), blank=True, null=True, help_text=_(u'登录时间'))

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

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

    def final_status(self, lensman_type):
        if lensman_type == self.COMMON:  # 普通摄影师校验
            return self.user_status
        elif lensman_type == self.OUTTAKE:  # 花絮摄影师校验
            return self.outtake_status
        return self.user_status

    def data(self, lensman_type):
        return {
            'name': self.name,
            'sex': self.sex,
            'phone': self.phone,
            'location': self.location,
            'status': self.final_status(lensman_type),
            'refused_reason': self.refused_reason,
        }

    def modified(self, lensman_type):
        if lensman_type == self.COMMON:  # 普通摄影师校验
            return self.user_status in [self.UNVERIFIED, self.REFUSED]
        elif lensman_type == self.OUTTAKE:  # 花絮摄影师校验
            return self.outtake_status in [self.UNVERIFIED, self.REFUSED]
        return False


class LensmanLoginLogInfo(BaseModelMixin):
    SUCCESS = 0
    PWD_ERROR = 1
    OTHER = 2

    LOGIN_RESULT = (
        (SUCCESS, u'登录成功'),
        (PWD_ERROR, u'密码错误'),
        (OTHER, u'其他'),
    )

    lensman_id = models.CharField(_(u'lensman_id'), max_length=32, blank=True, null=True, help_text=u'摄影师唯一标识', db_index=True)
    login_ip = models.CharField(_(u'login_ip'), max_length=32, blank=True, null=True, help_text=_(u'登录IP'))
    login_result = models.IntegerField(_(u'login_result'), choices=LOGIN_RESULT, default=SUCCESS)

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

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


class LensmanIncomeExpensesInfo(BaseModelMixin):
    INCOME = 0
    EXPENSE = 1
    UNFREEZE = 2

    TYPE = (
        (INCOME, u'收入'),
        (EXPENSE, u'支出'),
        (UNFREEZE, u'解冻'),
    )

    lensman_id = models.CharField(_(u'lensman_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)

    type = models.IntegerField(_(u'type'), choices=TYPE, default=INCOME, help_text=u'收支类别')
    amount = models.IntegerField(_(u'amount'), default=0, help_text=u'余额增减数量(分)')
    balance = models.IntegerField(_(u'balance'), default=0, help_text=u'余额增减后数量(分)')
    freeze_income_amount = models.IntegerField(_(u'freeze_income_amount'), default=0, help_text=u'收入冻结余额增减数量(分)')
    freeze_income_balance = models.IntegerField(_(u'freeze_income_balance'), default=0, help_text=u'收入冻结余额增减后数量(分)')
    freeze_expense_amount = models.IntegerField(_(u'freeze_expense_amount'), default=0, help_text=u'支出冻结余额增减数量(分)')
    freeze_expense_balance = models.IntegerField(_(u'freeze_expense_balance'), default=0, help_text=u'支出冻结余额增减后数量(分)')

    remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')

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

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


class TourGuideInfo(BaseModelMixin):
    MALE = 1
    FEMALE = 0

    SEX_TYPE = (
        (MALE, u'男'),
        (FEMALE, u'女'),
    )

    REFUSED = -1
    UNVERIFIED = 0
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3
    ASSIGN = 10

    USER_STATUS = (
        (REFUSED, u'已拒绝'),
        (UNVERIFIED, u'未验证'),
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
        (ASSIGN, u'已分配'),
    )

    tourguide_id = models.CharField(_(u'tourguide_id'), max_length=32, blank=True, null=True, help_text=u'导游唯一标识', db_index=True, unique=True)

    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 Union ID', db_index=True, unique=True)

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'导游姓名')
    sex = models.IntegerField(_(u'sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.UNKNOWN, help_text=u'导游性别')
    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'导游电话', db_index=True)
    location = models.CharField(_(u'location'), max_length=255, blank=True, null=True, help_text=u'导游地址')

    no = models.CharField(_(u'no'), max_length=16, blank=True, null=True, help_text=u'导游证编号')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=UNVERIFIED)
    refused_reason = models.TextField(_(u'refused_reason'), blank=True, null=True, help_text=u'审核拒绝原因')

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

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

    @property
    def photo_url(self):
        return ''

    @property
    def data(self):
        return {
            'name': self.name,
            'sex': self.sex,
            'phone': self.phone,
            'location': self.location,
            'no': self.no,
            'photo': self.photo_url,
            'status': self.user_status,
            'refused_reason': self.refused_reason,
        }

    @property
    def modified(self):
        return self.user_status in [self.UNVERIFIED, self.REFUSED]


class WechatInfo(BaseModelMixin):
    MALE = 1
    FEMALE = 0

    SEX_TYPE = (
        (MALE, u'男'),
        (FEMALE, u'女'),
    )

    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 Union ID')
    openids = JSONField(_(u'openids'), blank=True, null=True, help_text=u'微信 Open IDs')
    sex = models.IntegerField(_(u'sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.UNKNOWN, help_text=u'用户性别')
    nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户昵称')
    headimgurl = models.CharField(_(u'headimgurl'), max_length=255, blank=True, null=True, help_text=u'用户头像')
    country = models.CharField(_(u'country'), max_length=255, blank=True, null=True, help_text=u'用户国家')
    province = models.CharField(_(u'province'), max_length=255, blank=True, null=True, help_text=u'用户省份')
    city = models.CharField(_(u'city'), max_length=255, blank=True, null=True, help_text=u'用户城市')
    location = models.CharField(_(u'location'), max_length=255, blank=True, null=True, help_text=u'用户地址')

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

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


class UserInfo(BaseModelMixin, LensmanTypeBoolMixin):
    APP_USER = 0
    WX_USER = 1
    USER_USER = 8
    GUEST_USER = 9
    LENSMAN_USER = 10
    TOURGUIDE_USER = 11
    MINIAPP_USER = 12
    OAUTH_USER = 13

    USER_FROM = (
        (APP_USER, u'APP 创建用户'),
        (WX_USER, u'微信授权用户'),
        (USER_USER, u'用户端用户'),
        (GUEST_USER, u'游客用户'),
        (LENSMAN_USER, u'摄影师端用户'),
        (TOURGUIDE_USER, u'导游端用户'),
        (MINIAPP_USER, u'小程序端用户'),
        (OAUTH_USER, u'网页授权用户'),
    )

    UNVERIFIED = 0
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3
    ASSIGN = 10

    USER_STATUS = (
        (UNVERIFIED, u'未验证'),
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
        (ASSIGN, u'已分配'),
    )

    MALE = 1
    FEMALE = 0

    SEX_TYPE = (
        (MALE, u'男'),
        (FEMALE, u'女'),
    )

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True, unique=True)

    user_from = models.IntegerField(_(u'user_from'), choices=USER_FROM, default=APP_USER, help_text=u'用户来源')
    uuid = models.CharField(_(u'uuid'), max_length=255, blank=True, null=True, help_text=u'通用唯一识别码 (Universally Unique Identifier)', db_index=True)
    # APP 创建用户
    username = models.CharField(_(u'username'), max_length=255, blank=True, null=True, help_text=u'用户用户名', db_index=True)
    password = models.CharField(_(u'password'), max_length=255, blank=True, null=True, help_text=u'用户密码')
    # 微信授权用户
    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 Unionid', db_index=True)
    openid = models.CharField(_(u'openid'), max_length=32, blank=True, null=True, help_text=u'微信 Openid，拍爱用户端', db_index=True)
    openid_lensman = models.CharField(_(u'openid_lensman'), max_length=32, blank=True, null=True, help_text=u'微信 Openid，拍爱摄影师端', db_index=True)
    openid_tourguide = models.CharField(_(u'openid_tourguide'), max_length=32, blank=True, null=True, help_text=u'微信 Openid，拍爱导游端', db_index=True)
    openid_miniapp = models.CharField(_(u'openid_miniapp'), max_length=32, blank=True, null=True, help_text=u'微信 Openid，拍爱小程序', db_index=True)
    openid_oauth = models.CharField(_(u'openid_oauth'), max_length=32, blank=True, null=True, help_text=u'微信 Openid，拍爱用户授权', db_index=True)
    # 用户基本信息
    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'用户姓名')
    sex = models.IntegerField(_(u'sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.UNKNOWN, help_text=u'用户性别')
    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'用户头像')
    phone = models.CharField(_(u'phone'), max_length=11, default='', blank=True, help_text=u'用户电话', db_index=True)
    country = models.CharField(_(u'country'), max_length=255, blank=True, null=True, help_text=u'用户国家')
    province = models.CharField(_(u'province'), max_length=255, blank=True, null=True, help_text=u'用户省份')
    city = models.CharField(_(u'city'), max_length=255, blank=True, null=True, help_text=u'用户城市')
    location = models.CharField(_(u'location'), max_length=255, blank=True, null=True, help_text=u'用户地址')

    # 用户身份
    islensman = models.BooleanField(_(u'islensman'), default=False, help_text=_(u'摄影师？'), db_index=True)
    istourguide = models.BooleanField(_(u'istourguide'), default=False, help_text=_(u'导游？'), db_index=True)

    balance = models.IntegerField(_(u'balance'), default=0, help_text=u'用户余额(分)')
    freeze_income_balance = models.IntegerField(_(u'freeze_income_balance'), default=0, help_text=u'用户收入冻结余额(分)')
    freeze_expense_balance = models.IntegerField(_(u'freeze_expense_balance'), default=0, help_text=u'用户支出冻结余额(分)')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=UNVERIFIED, help_text=u'用户状态')
    outtake_status = models.IntegerField(_(u'outtake_status'), choices=USER_STATUS, default=UNVERIFIED, help_text=u'花絮摄影师状态')

    assign_ip = models.CharField(_(u'assign_ip'), max_length=32, blank=True, null=True, help_text=_(u'分配IP'))
    assign_at = models.DateTimeField(_(u'assign_at'), blank=True, null=True, help_text=_(u'分配时间'))

    signup_ip = models.CharField(_(u'signup_ip'), max_length=32, blank=True, null=True, help_text=_(u'注册IP'))
    signup_at = models.DateTimeField(_(u'signup_at'), blank=True, null=True, help_text=_(u'注册时间'))

    login_ip = models.CharField(_(u'login_ip'), max_length=32, blank=True, null=True, help_text=_(u'登录IP'))
    login_at = models.DateTimeField(_(u'login_at'), blank=True, null=True, help_text=_(u'登录时间'))

    has_membercard = models.BooleanField(_(u'has_membercard'), default=False, help_text=_(u'是否激活会员卡'), db_index=True)
    membercardid = models.CharField(_(u'membercardid'), max_length=32, default='', blank=True, help_text=_(u'会员卡编号'), db_index=True)
    memberusercardcode = models.CharField(_(u'memberusercardcode'), max_length=32, default='', blank=True, help_text=_(u'用户会员卡编号'), db_index=True)

    clerk_id = models.CharField(_(u'clerk_id'), max_length=32, blank=True, null=True, help_text=u'店员唯一标识', db_index=True)

    test_user = models.BooleanField(_(u'test_user'), default=False, help_text=_(u'是否为测试用户'), db_index=True)

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

        unique_together = (
            ('brand_id', 'unionid')
        )

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

    @property
    def final_nickname(self):
        if self.user_from == self.APP_USER:
            return self.username
        elif self.user_from == self.WX_USER:
            return self.nickname
        elif self.user_from == self.GUEST_USER:
            return self.nickname
        elif self.user_from == self.LENSMAN_USER:
            return self.name
        elif self.user_from == self.TOURGUIDE_USER:
            return self.name
        elif self.user_from == self.MINIAPP_USER:
            return self.nickname
        return self.nickname

    @property
    def data(self):
        return {
            'user_id': self.user_id,
            'name': self.name,
            'username': self.username,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'phone': self.phone,
        }

    @property
    def cardList(self):
        return [
            {
                'cardId': self.membercardid,
                'code': self.memberusercardcode
            }
        ]

    @property
    def srinfo(self):
        try:
            sr = SalesResponsibilityInfo.objects.get(brand_id=self.brand_id, unionid=self.unionid, user_status=SalesResponsibilityInfo.ACTIVATED)
        except SalesResponsibilityInfo.DoesNotExist:
            sr = None
        return sr.base_data if sr else {
            'sr_id': '',
            'is_sr': False,
            'is_super_sr': False,
        }

    @property
    def brandata(self):
        if self.clerk_id:
            try:
                saleclerk = SaleclerkInfo.objects.get(clerk_id=self.clerk_id, status=True)
            except SaleclerkInfo.DoesNotExist:
                saleclerk = None
            saleclerk_info = saleclerk.data if saleclerk and saleclerk.is_auth else {}
        else:
            saleclerk_info = {}
        return {
            'user_id': self.user_id,
            'name': self.name,
            'username': self.username,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'phone': self.phone,
            're_membercard': True if self.has_membercard and not self.memberusercardcode else False,
            'has_membercard': self.has_membercard,
            'membercardid': self.membercardid,
            'memberusercardcode': self.memberusercardcode,
            'cardList': self.cardList,
            'saleclerk': bool(saleclerk_info),
            'saleclerk_info': saleclerk_info,
            'sr_info': self.srinfo,
        }

    @property
    def cardata(self):
        return {
            'brand_id': self.brand_id,
            'card_id': self.membercardid,
            'code': self.memberusercardcode,
        }


class UserLoginLogInfo(BaseModelMixin):
    SUCCESS = 0
    PWD_ERROR = 1
    OTHER = 2

    LOGIN_RESULT = (
        (SUCCESS, u'登录成功'),
        (PWD_ERROR, u'密码错误'),
        (OTHER, u'其他'),
    )

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    login_ip = models.CharField(_(u'login_ip'), max_length=32, blank=True, null=True, help_text=_(u'登录IP'))
    login_result = models.IntegerField(_(u'login_result'), choices=LOGIN_RESULT, default=SUCCESS)

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

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


class UserIncomeExpensesInfo(BaseModelMixin):
    INCOME = 0
    EXPENSE = 1
    UNFREEZE = 2

    TYPE = (
        (INCOME, u'收入'),
        (EXPENSE, u'支出'),
        (UNFREEZE, u'解冻'),
    )

    user_id = models.CharField(_(u'user_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)

    type = models.IntegerField(_(u'type'), choices=TYPE, default=INCOME, help_text=u'收支类别')
    amount = models.IntegerField(_(u'amount'), default=0, help_text=u'余额增减数量(分)')
    balance = models.IntegerField(_(u'balance'), default=0, help_text=u'余额增减后数量(分)')
    freeze_income_amount = models.IntegerField(_(u'freeze_income_amount'), default=0, help_text=u'收入冻结余额增减数量(分)')
    freeze_income_balance = models.IntegerField(_(u'freeze_income_balance'), default=0, help_text=u'收入冻结余额增减后数量(分)')
    freeze_expense_amount = models.IntegerField(_(u'freeze_expense_amount'), default=0, help_text=u'支出冻结余额增减数量(分)')
    freeze_expense_balance = models.IntegerField(_(u'freeze_expense_balance'), default=0, help_text=u'支出冻结余额增减后数量(分)')

    remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')

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

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