# coding=utf-8

from django.db import models
from django.utils import timezone
from django.conf import settings
from django.contrib.auth import get_user_model

from utils.wx.wechat import WeChat
from utils.exceptions import CustomError
from utils.wx.WXBizDataCrypt import WXBizDataCrypt

from apps.WechatApplet.models import WechatApplet

User = get_user_model()


class Customer(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'用户', editable=False, related_name='customer_user', on_delete=models.PROTECT)
    is_distributor = models.BooleanField(verbose_name=u"分销商", default=False)

    name = models.CharField(max_length=100, verbose_name=u"姓名")
    tel = models.CharField(max_length=50, verbose_name=u'电话')
    gender = models.PositiveSmallIntegerField(choices=settings.GENDER_CHOICES, verbose_name=u'性别', null=True)
    face = models.CharField(max_length=200, verbose_name=u'头像', null=True)

    balance = models.BigIntegerField(verbose_name=u'余额', default=0, editable=False)
    points = models.IntegerField(verbose_name=u'积分', default=0, editable=False)

    success_count = models.IntegerField(verbose_name=u'成交次数', default=0, editable=False)
    total_amount = models.BigIntegerField(verbose_name=u'累计消费金额', default=0, editable=False)
    total_point = models.BigIntegerField(verbose_name=u'累计消费积分', default=0, editable=False)

    class Meta:
        db_table = "customer"
        verbose_name = u'客户管理'
        ordering = ['-id']
        index_together = (
            'name',
            'tel',
        )
        default_permissions = ()
        permissions = []

    def setInfo(self, appid, openid, encryptedData, iv):
        wx_customer = CustomerWechat.objects.filter(openid=openid, wechat_app__authorizer_appid=appid).first()
        if not wx_customer:
            raise CustomError(u'未找到相应的微信客户！')
        if wx_customer.customer and wx_customer.customer.id != self.id:
            raise CustomError(u'该微信已同步其他客户！')
        if not wx_customer.customer:
            wx_customer.customer = self
            wx_customer.save()
        pc = WXBizDataCrypt(wx_customer.wechat_app.authorizer_appid, wx_customer.session_key)
        result = pc.decrypt(encryptedData, iv)
        self.name = result['nickName']
        self.gender = result['gender']
        self.face = result['avatarUrl']
        self.save()
        return self.face, self.name


class SuperiorDistributor(models.Model):
    customer = models.ForeignKey(Customer, verbose_name=u'客户', editable=False, related_name='superior_distributor_customer', on_delete=models.PROTECT)
    one_level = models.ForeignKey(Customer, verbose_name=u'一级分销', related_name='superior_distributor_one_level', on_delete=models.PROTECT, null=True, blank=True)
    tow_level = models.ForeignKey(Customer, verbose_name=u'二级分销', related_name='superior_distributor_tow_level', on_delete=models.PROTECT, null=True, blank=True)
    three_level = models.ForeignKey(Customer, verbose_name=u'三级分销', related_name='superior_distributor_three_level', on_delete=models.PROTECT, null=True, blank=True)
    four_level = models.ForeignKey(Customer, verbose_name=u'四级分销', related_name='superior_distributor_four_level', on_delete=models.PROTECT, null=True, blank=True)
    five_level = models.ForeignKey(Customer, verbose_name=u'五级分销', related_name='superior_distributor_five_level', on_delete=models.PROTECT, null=True, blank=True)

    class Meta:
        db_table = "superior_distributor"
        verbose_name = u'上级分销'
        ordering = ['-id']
        default_permissions = ()
        permissions = []


class CustomerWechat(models.Model):
    wechat_app = models.ForeignKey(WechatApplet, verbose_name=u'小程序', on_delete=models.PROTECT, editable=False)
    customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT, editable=False, null=True)
    openid = models.CharField(max_length=512, verbose_name=u"openid")
    session_key = models.CharField(max_length=512, verbose_name=u'session_key',null=True)

    class Meta:
        db_table = "customer_wechat"
        verbose_name = u'微信客户'
        unique_together = [
            ('openid')
        ]
        default_permissions = ()

    @staticmethod
    def login(code, appid):
        '''
        微信自动登录小程序
        :param code:
        :param appid:
        :return:
        '''
        wx = WechatApplet.getByAppid(appid)
        res = WeChat.code2Session(appid, wx.secret, code)
        instance = CustomerWechat.objects.filter(openid=res['openid'], wechat_app=wx).first()
        if not instance:
            instance = CustomerWechat.objects.create(wechat_app=wx, openid=res['openid'], session_key=res['session_key'])
        else:
            instance.session_key = res['session_key']
            instance.save()
        return instance

    @staticmethod
    def bindWechat(appid, openid, phoneEncryptedData, phoneIv):
        '''
        微信登录小程序
        判断是否有推荐人，如果有且推荐人是分销商，就要创建上级分销表，同时查询推荐人是否存在上级分销表 如果存在就要将推荐人的上级分销相应的添加到本人的上级分销表里
        :param appid:
        :param openid:
        :param phoneEncryptedData:
        :param phoneIv:
        :return:
        '''
        wx_customer = CustomerWechat.objects.filter(openid=openid, wechat_app__authorizer_appid=appid).first()
        if not wx_customer:
            raise CustomError(u'未找到相应的微信客户！')

        pc = WXBizDataCrypt(appid, wx_customer.session_key)
        phon_data = pc.decrypt(phoneEncryptedData, phoneIv)
        tel = phon_data['purePhoneNumber']
        if wx_customer.customer:
            if wx_customer.customer.user.username != tel:
                raise CustomError(u'微信绑定的手机号与系统记录的不符!')
            else:
                return wx_customer.customer
        user = User.objects.filter(username=tel).first()
        if not user:
            user = User.objects.create_customer(tel, password=tel, **{'is_active': True})
        customer = Customer.objects.filter(user=user).first()
        if not customer:
            customer = Customer.objects.create(user=user, name=tel, tel=tel)
        wx_customer.customer = customer
        wx_customer.save()
        return customer


class Area(models.Model):
    PROVINCE = 0
    CITY = 1
    COUNTY = 2
    LEVEL_CHOICES = (
        (PROVINCE, u'省级'),
        (CITY, u'市级'),
        (COUNTY, u'县区级'),
    )

    name = models.CharField(verbose_name=u'名称', max_length=50, blank=True, null=True)
    level = models.IntegerField(verbose_name=u'层级', choices=LEVEL_CHOICES)
    province_name = models.CharField(u'所属省', max_length=50, blank=True, null=True)
    city_name = models.CharField(u'所属地市', max_length=50, blank=True, null=True)
    province = models.ForeignKey('Area', verbose_name=u'所属省', related_name='province_children', on_delete=models.PROTECT, blank=True, null=True)
    city = models.ForeignKey('Area', verbose_name=u'所属地市', related_name='city_children', on_delete=models.PROTECT, blank=True, null=True)

    def __unicode__(self):
        return '%s' % (self.name)

    class Meta:
        verbose_name = u"地区"
        db_table = "area"


class CustomerAddress(models.Model):
    customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT, editable=False)
    name = models.CharField(verbose_name=u'收货人姓名', max_length=20)
    tel = models.CharField(verbose_name=u'收货人电话', max_length=11)
    province = models.ForeignKey(Area, verbose_name=u'省', related_name='address_province', on_delete=models.PROTECT)
    city = models.ForeignKey(Area, verbose_name=u'地市', related_name='address_city', null=True, on_delete=models.PROTECT)
    county = models.ForeignKey(Area, verbose_name=u'区县', related_name='address_county', null=True, on_delete=models.PROTECT)
    address = models.CharField(verbose_name=u'详细地址', max_length=500)
    default = models.BooleanField(verbose_name=u'默认', default=False)
    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)
    create_time = models.DateTimeField(verbose_name=u'创建时间', default=timezone.now, editable=False)

    class Meta:
        db_table = 'customer_address'
        verbose_name = u'收货地址'
        ordering = ['-default', '-id']
        default_permissions = ()

    def get_address(self):
        return '{}{}{}{}'.format(self.province.name, self.city.name, self.county.name, self.address)

    def destory(self, queryset):
        if self.default:
            self.default = False
            default = queryset.first()
            if default:
                default.default = True
                default.save()
        self.delete = True
        self.save()
