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

from django.db import models
from django.utils.translation import ugettext_lazy as _

from utils.version_utils import is_version_match


class CreateUpdateMixin(models.Model):
    status = models.BooleanField(_(u'status'), default=True, help_text=_(u'状态'), db_index=True)
    created_at = models.DateTimeField(_(u'created_at'), auto_now_add=True, editable=True, help_text=_(u'创建时间'))
    updated_at = models.DateTimeField(_(u'updated_at'), auto_now=True, editable=True, help_text=_(u'更新时间'))

    class Meta:
        abstract = True


class PlatformMixin(models.Model):
    BOTH = 0
    ADR = 1
    IOS = 2

    SUPPORT_PLATFORM = (
        (BOTH, u'全平台'),
        (ADR, u'Android'),
        (IOS, u'iOS'),
    )

    platform = models.IntegerField(_(u'plat'), choices=SUPPORT_PLATFORM, default=BOTH, help_text=u'支持平台', db_index=True)

    class Meta:
        abstract = True


class VersionMixin(models.Model):
    min_adr = models.CharField(_(u'min_adr'), max_length=255, blank=True, null=True, help_text=u'Adr 最低版本')
    min_ios = models.CharField(_(u'min_ios'), max_length=255, blank=True, null=True, help_text=u'iOS 最低版本')
    max_adr = models.CharField(_(u'max_adr'), max_length=255, blank=True, null=True, help_text=u'Adr 最高版本')
    max_ios = models.CharField(_(u'max_ios'), max_length=255, blank=True, null=True, help_text=u'iOS 最高版本')

    def version_match(self, request):
        return is_version_match(request, {
            'min_adr': self.min_adr,
            'min_ios': self.min_ios,
            'max_adr': self.max_adr,
            'max_ios': self.max_ios,
        })

    class Meta:
        abstract = True
