# -*- 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, ProvinceShortModelMixin, SexModelMixin, upload_file_path,
                               upload_file_url, upload_path)
from shortuuidfield import ShortUUIDField


class AdministratorInfo(BaseModelMixin):
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3

    USER_STATUS_TUPLE = (
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
    )

    admin_id = ShortUUIDField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'管理员唯一标识', db_index=True, unique=True)

    phone = models.CharField(_(u'phone'), max_length=16, 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'管理员密码')
    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'管理员姓名')

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

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=ACTIVATED, help_text=u'管理员状态', db_index=True)

    class Meta:
        verbose_name = _(u'管理员信息')
        verbose_name_plural = _(u'管理员信息')

    def __unicode__(self):
        return u'{}-{}'.format(self.name, self.phone)


class OperatorInfo(BaseModelMixin):
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3

    USER_STATUS_TUPLE = (
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
    )

    operator_id = ShortUUIDField(_(u'operator_id'), max_length=32, blank=True, null=True, help_text=u'操作员唯一标识', db_index=True, unique=True)

    phone = models.CharField(_(u'phone'), max_length=16, 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'操作员密码')
    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'操作员姓名')

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

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=ACTIVATED, help_text=u'操作员状态', db_index=True)

    class Meta:
        verbose_name = _(u'操作员信息')
        verbose_name_plural = _(u'操作员信息')

    def __unicode__(self):
        return u'{}-{}'.format(self.name, self.phone)

    @property
    def data(self):
        return {
            'operator_id': self.operator_id,
            'phone': self.phone,
            'name': self.name,
        }

    @property
    def kododata(self):
        return {
            'optor_id': self.operator_id,
            'brand_domain': self.brand_domain or settings.KODO_DEFAULT_BRAND_DOMAIN,
        }


class BrandInfo(BaseModelMixin):
    brand_id = ShortUUIDField(_(u'brand_id'), max_length=32, help_text=u'品牌唯一标识', db_index=True, unique=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')
    brand_descr = models.TextField(_(u'brand_descr'), max_length=255, blank=True, null=True, help_text=u'品牌描述')

    brand_logo = models.ImageField(_(u'brand_logo'), upload_to=upload_path, blank=True, null=True, help_text=u'品牌商标')
    brand_domain = models.CharField(_(u'brand_domain'), max_length=255, blank=True, null=True, help_text=u'品牌域名')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'品牌信息')
        verbose_name_plural = _(u'品牌信息')

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

    @property
    def brand_logo_url(self):
        return upload_file_url(self.brand_logo)

    @property
    def data(self):
        return {
            'brand_id': str(self.pk),
            'brand_name': self.brand_name,
            'brand_descr': self.brand_descr,
        }


class ModelInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_id = ShortUUIDField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True, unique=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')
    model_full_name = models.CharField(_(u'model_full_name'), max_length=255, blank=True, null=True, help_text=u'型号全名称')
    model_descr = models.TextField(_(u'model_descr'), max_length=255, blank=True, null=True, help_text=u'型号描述')

    image = models.ImageField(_(u'image'), upload_to=upload_path, blank=True, null=True, help_text=u'图片')
    url = models.CharField(_(u'url'), max_length=255, blank=True, null=True, help_text=u'链接')

    factory_yuan = models.FloatField(_(u'factory_yuan'), default=1000, help_text=u'出厂价(元)')
    factory_fee = models.IntegerField(_(u'factory_fee'), default=100000, help_text=u'出厂价(分)')

    integral = models.IntegerField(_(u'integral'), default=100, help_text=u'积分')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'型号信息')
        verbose_name_plural = _(u'型号信息')

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

    @property
    def image_path(self):
        return upload_file_path(self.image)

    @property
    def image_url(self):
        return upload_file_url(self.image)

    @property
    def data(self):
        return {
            'model_id': str(self.pk),
            'model_name': self.model_name,
            'model_descr': self.model_descr,
        }

    @property
    def imgdata(self):
        return {
            'image_url': self.image_url,
            'url': self.url,
        }

    @property
    def images(self):
        return [self.imgdata] if self.image else []
        # imgs = ModelImageInfo.objects.filter(model_id=self.model_id, status=True)
        # return [img.data for img in imgs]

    @property
    def admindata(self):
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            'model_full_name': self.model_full_name,
            'image_path': self.image_path,
            'image_url': self.image_url,
            'factory_yuan': self.factory_yuan,
            'integral': self.integral,
        }


class ModelImageInfo(BaseModelMixin):
    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

    image = models.ImageField(_(u'image'), upload_to=upload_path, blank=True, null=True, help_text=u'图片')
    url = models.CharField(_(u'url'), max_length=255, blank=True, null=True, help_text=u'链接')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'型号图片信息')
        verbose_name_plural = _(u'型号图片信息')

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

    @property
    def image_url(self):
        return upload_file_url(self.image)

    @property
    def data(self):
        return {
            'image_url': self.image_url,
            'url': self.url,
        }


class DistributorInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    distributor_id = ShortUUIDField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True, unique=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')
    distributor_descr = models.TextField(_(u'distributor_descr'), max_length=255, blank=True, null=True, help_text=u'经销商描述')

    distributor_province_code = models.CharField(_(u'distributor_province_code'), max_length=6, blank=True, null=True, help_text=u'经销商所在省份编码')
    distributor_province_name = models.CharField(_(u'distributor_province_name'), max_length=3, choices=ProvinceShortModelMixin.PROVINCE_NAME_TUPLE, default=ProvinceShortModelMixin.PROVINCE_DEFAULT_NAME, blank=True, null=True, help_text=u'经销商所在省份名称')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'经销商信息')
        verbose_name_plural = _(u'经销商信息')

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

    @property
    def data(self):
        return {
            'distributor_id': str(self.pk),
            'distributor_name': self.distributor_name,
            'distributor_descr': self.distributor_descr,
        }

    @property
    def admindata(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'distributor_descr': self.distributor_descr,
            'province_code': self.distributor_province_code,
            'province_name': self.distributor_province_name,
        }


class SaleclerkInfo(BaseModelMixin, SexModelMixin):
    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'已分配'),
    )

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

    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, blank=True, null=True, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=32, blank=True, null=True, help_text=u'经销商名称')

    clerk_id = ShortUUIDField(_(u'clerk_id'), max_length=32, help_text=u'店员唯一标识', db_index=True, unique=True)
    clerk_name = models.CharField(_(u'clerk_name'), max_length=32, blank=True, null=True, help_text=u'店员名称')
    clerk_sex = models.IntegerField(_(u'clerk_sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.MALE, help_text=u'店员性别', db_index=True)
    clerk_phone = models.CharField(_(u'clerk_phone'), max_length=11, 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)

    integral = models.IntegerField(_(u'integral'), default=0, help_text=u'积分')
    total_integral = models.IntegerField(_(u'total_integral'), default=0, help_text=u'全部积分')

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

    is_auth = models.BooleanField(_(u'is_auth'), default=False, 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'经销商销售员信息')
        verbose_name_plural = _(u'经销商销售员信息')
        unique_together = (
            ('unionid', 'brand_id'),
        )

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

    @property
    def admindata(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'clerk_id': self.clerk_id,
            'clerk_name': self.clerk_name,
            'clerk_sex': self.clerk_sex,
            'clerk_phone': self.clerk_phone,
            'integral': self.integral,
            'status': self.user_status,
            'refused_reason': self.refused_reason,
            'is_auth': self.is_auth,
        }

    data = admindata


class BrandModelDistributorPriceInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, blank=True, null=True, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')

    factory_yuan = models.FloatField(_(u'factory_yuan'), default=1000, help_text=u'出厂价(元)')
    factory_fee = models.IntegerField(_(u'factory_fee'), default=100000, help_text=u'出厂价(分)')

    integral = models.IntegerField(_(u'integral'), default=100, help_text=u'积分')

    class Meta:
        verbose_name = _(u'品牌/型号/代理商价格信息')
        verbose_name_plural = _(u'品牌/型号/代理商价格信息')
        unique_together = (
            ('brand_id', 'model_id', 'distributor_id'),
        )

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


class LatestAppInfo(BaseModelMixin):
    latest_adr_version_code = models.IntegerField(_(u'latest_adr_version_code'), default=0, help_text=u'最新安卓版本号')
    latest_adr_version_name = models.CharField(_(u'latest_adr_version_name'), max_length=16, blank=True, null=True, help_text=u'最新安卓版本名')
    latest_adr_app = models.FileField(_(u'latest_adr_app'), upload_to=upload_path, blank=True, null=True, help_text=u'最新版安卓 APP')
    latest_adr_url = models.URLField(_(u'latest_adr_url'), max_length=255, blank=True, null=True, help_text=u'最新版 APP 链接')

    latest_ios_version_code = models.IntegerField(_(u'latest_ios_version_code'), default=0, help_text=u'最新 iOS 版本号')
    latest_ios_version_name = models.CharField(_(u'latest_ios_version_name'), max_length=16, blank=True, null=True, help_text=u'最新 iOS 版本名')
    latest_ios_url = models.URLField(_(u'latest_ios_url'), max_length=255, blank=True, null=True, help_text=u'最新版 iOS 链接')

    class Meta:
        verbose_name = _(u'升级配置信息')
        verbose_name_plural = _(u'升级配置信息')

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

    @property
    def final_latest_adr_url(self):
        return self.latest_adr_url or upload_file_url(self.latest_adr_app)

    @property
    def data(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
            'latest_ios_version_code': self.latest_ios_version_code,
            'latest_ios_version_name': self.latest_ios_version_name,
            'latest_ios_url': self.latest_ios_url,
        }

    @property
    def adr(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
        }


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

    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'用户手机', db_index=True)
    iv = models.CharField(_(u'iv'), max_length=255, blank=True, null=True, help_text=u'iv')
    encryptedData = models.CharField(_(u'encryptedData'), max_length=255, blank=True, null=True, help_text=u'encryptedData')

    lat = models.FloatField(_(u'lat'), default=1.0, help_text=u'纬度')
    lon = models.FloatField(_(u'lon'), default=1.0, help_text=u'经度')

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

    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, blank=True, null=True, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')

    serialNo = models.CharField(_(u'serialNo'), max_length=16, blank=True, null=True, help_text=u'序列号', db_index=True)

    verifyResult = models.IntegerField(_(u'verifyResult'), default=0, help_text=u'验证结果')

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

    class Meta:
        verbose_name = _(u'消费者信息提交记录')
        verbose_name_plural = _(u'消费者信息提交记录')

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