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

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, upload_file_url, upload_path

from kodo.basemodels import PaiaiSrcMixin, PlatformMixin, VersionMixin


class LatestAppInfo(BaseModelMixin, PaiaiSrcMixin):
    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 = _('latestappinfo')
        verbose_name_plural = _('latestappinfo')

    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,
        }


class PatchInfo(BaseModelMixin, PlatformMixin, PaiaiSrcMixin):
    version = models.CharField(_(u'version'), max_length=16, blank=True, null=True, help_text=u'版本（1.0.0）')
    patch = models.FileField(_(u'patch'), upload_to=upload_path, blank=True, null=True, help_text=u'补丁')

    class Meta:
        verbose_name = _('patchinfo')
        verbose_name_plural = _('patchinfo')

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

    @property
    def patch_url(self):
        return upload_file_url(self.patch)

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


class APPSettingsInfo(BaseModelMixin, PlatformMixin, PaiaiSrcMixin):
    channel = models.CharField(_(u'channel'), max_length=16, blank=True, null=True, help_text=u'渠道')
    version = models.CharField(_(u'version'), max_length=16, blank=True, null=True, help_text=u'版本（1.0.0）')

    online = models.BooleanField(_(u'online'), default=True, help_text=u'是否上线')

    class Meta:
        verbose_name = _('appsettingsinfo')
        verbose_name_plural = _('appsettingsinfo')

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

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


class SplashInfo(BaseModelMixin, PaiaiSrcMixin):
    splash_image = models.ImageField(_(u'splash_image'), upload_to=upload_path, blank=True, null=True, help_text=u'启动页面图片')
    spalash_image_airtime = models.DateTimeField(_(u'spalash_image_airtime'), blank=True, null=True, help_text=u'启动页面图片开始日期')
    spalash_image_deadline = models.DateTimeField(_(u'spalash_image_deadline'), blank=True, null=True, help_text=u'启动页面图片截止日期')

    class Meta:
        verbose_name = _('splashinfo')
        verbose_name_plural = _('splashinfo')

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

    @property
    def splash_image_url(self):
        return upload_file_url(self.splash_image)

    @property
    def data(self):
        return {
            'splash_image_url': self.splash_image_url,
            'spalash_image_airtime': self.spalash_image_airtime,
            'spalash_image_deadline': self.spalash_image_deadline,
        }


class FeedbackInfo(BaseModelMixin, PaiaiSrcMixin):
    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识')
    feedback = models.TextField(_(u'feedback'), blank=True, null=True, help_text=u'用户反馈')

    class Meta:
        verbose_name = _('feedbackinfo')
        verbose_name_plural = _('feedbackinfo')

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


class GuestEntranceControlInfo(BaseModelMixin, PlatformMixin, VersionMixin, PaiaiSrcMixin):

    class Meta:
        verbose_name = _('guestentrancecontrolinfo')
        verbose_name_plural = _('guestentrancecontrolinfo')

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

    @property
    def data(self):
        return {
            'platform': self.platform,
            'min_adr': self.min_adr,
            'min_ios': self.min_ios,
            'max_adr': self.max_adr,
            'max_ios': self.max_ios,
        }


class BoxProgramVersionInfo(BaseModelMixin):
    srv_version_code = models.IntegerField(_(u'srv_version_code'), default=0, help_text=u'Box Server 版本号')
    srv_version_name = models.CharField(_(u'srv_version_name'), max_length=255, blank=True, null=True, help_text=u'Box Server 版本名')
    srv_sha1 = models.CharField(_(u'srv_sha1'), max_length=255, blank=True, null=True, help_text=u'Box Server Commit SHA1')

    proc_version_code = models.IntegerField(_(u'proc_version_code'), default=0, help_text=u'Box Proc 版本号')
    proc_version_name = models.CharField(_(u'proc_version_name'), max_length=255, blank=True, null=True, help_text=u'Box Proc 版本名')
    proc_sha1 = models.CharField(_(u'proc_sha1'), max_length=255, blank=True, null=True, help_text=u'Box Proc Commit SHA1')

    class Meta:
        verbose_name = _('boxprogramversioninfo')
        verbose_name_plural = _('boxprogramversioninfo')

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

    @property
    def data(self):
        return {
            'srv_version_code': self.srv_version_code,
            'srv_version_name': self.srv_version_name,
            'srv_sha1': self.srv_sha1,
            'proc_version_code': self.proc_version_code,
            'proc_version_name': self.proc_version_name,
            'proc_sha1': self.proc_sha1,
        }
