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

import datetime
import os
import time

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

from pai2.basemodels import CreateUpdateMixin, PlatformMixin, VersionMixin


def upload_path(instance, old_filename):
    extension = os.path.splitext(old_filename)[1].lower()
    today = datetime.datetime.today()
    return 'file/{year}{month}/{timestamp}{extension}'.format(
        year=today.year,
        month=today.month,
        timestamp=int(time.time()),
        extension=extension
    )


class LatestAppInfo(CreateUpdateMixin):
    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=255, 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=255, 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 u'{}{}'.format(settings.DOMAIN, self.latest_adr_app and self.latest_adr_app.url)

    @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 SplashInfo(CreateUpdateMixin):
    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 self.splash_image and (settings.DOMAIN + self.splash_image.url)

    @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(CreateUpdateMixin):
    user_id = models.CharField(_(u'user_id'), max_length=255, 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(CreateUpdateMixin, PlatformMixin, VersionMixin):

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