# coding=utf-8

from django.db import models

from utils.exceptions import CustomError

from apps.images.models import Images

class Brand(models.Model):
    name = models.CharField(max_length=100, verbose_name=u"名称")
    enabled = models.BooleanField(verbose_name=u"在售", default=True)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    delete = models.BooleanField(verbose_name=u"删除", default=False, editable=False)

    class Meta:
        db_table = "vehicle_brand"
        verbose_name = u"品牌"
        ordering = [ '-id']
        index_together = (
            'name',
        )
        default_permissions = ()


class Series(models.Model):
    brand = models.ForeignKey(Brand, verbose_name=u"品牌", on_delete=models.PROTECT)
    thumbnail = models.ForeignKey(Images, verbose_name=u'缩略图', on_delete=models.PROTECT, null=True)
    name = models.CharField(max_length=100, verbose_name=u"名称")
    min_price = models.FloatField(verbose_name=u'最低指导价',default=0)
    enabled = models.BooleanField(verbose_name=u"在售", default=True)
    is_hot = models.BooleanField(verbose_name=u"热门", default=False)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    delete = models.BooleanField(verbose_name=u"删除", default=False, editable=False)

    class Meta:
        db_table = "vehicle_series"
        verbose_name = u"车系管理"
        ordering = ['-id']
        index_together = (
            'name',
        )
        default_permissions = ()
        permissions = [
            ('browse_vehicle_series', u'查看'),
            ('add_vehicle_series', u'添加'),
            ('edit_vehicle_series', u'修改'),
            ('delete_vehicle_series', u'删除'),
        ]


class Model(models.Model):
    series = models.ForeignKey(Series, related_name='model_ref_series', verbose_name=u"车系", on_delete=models.PROTECT)
    name = models.CharField(max_length=100, verbose_name=u"名称")
    price = models.BigIntegerField(verbose_name=u'指导价')
    sale_price = models.BigIntegerField(verbose_name=u'售价')
    engine = models.CharField(max_length=50, verbose_name=u'发动机',)
    car_body = models.CharField(max_length=500, verbose_name=u'车身结构',)
    transmission = models.CharField(max_length=500, verbose_name=u'变速箱',)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    enabled = models.BooleanField(verbose_name=u'在用', default=True)
    delete = models.BooleanField(verbose_name=u"删除", default=False, editable=False)

    class Meta:
        db_table = "vehicle_model"
        verbose_name = u"车型管理"
        ordering = ['-id']
        index_together = (
            'name',
        )
        default_permissions = ()
        permissions = [
            ('browse_vehicle_model', u'查看'),
            ('add_vehicle_model', u'添加'),
            ('edit_vehicle_model', u'修改'),
            ('delete_vehicle_model', u'删除'),
        ]

    @staticmethod
    def getById(id):
        try:
            id = int(id)
        except:
            raise CustomError(u'无效的车型id')

        instance = Model.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的车型')
        return instance

    def canSale(self):
        series = self.series
        brand = series.brand
        if self.delete or series.delete or brand.delete:
            return False
        if not self.enabled or not series.enabled or not brand.enabled:
            return False
        return True


class VehicleImg(models.Model):
    vehicle_model = models.ForeignKey(Model, verbose_name=u'车型', on_delete=models.PROTECT)
    img = models.ForeignKey(Images, verbose_name=u'图片', on_delete=models.PROTECT)

    class Meta:
        db_table = 'vehicle_img'
        verbose_name = u'整车图片'
        default_permissions = ()

class VehicleModelImg(models.Model):
    vehicle_model = models.ForeignKey(Model, verbose_name=u'车型', on_delete=models.PROTECT)
    img = models.ForeignKey(Images, verbose_name=u'图片', on_delete=models.PROTECT)

    class Meta:
        db_table = 'vehicle_model_img'
        verbose_name = u'车型图片'
        default_permissions = ()

