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

import os

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

from pai2.basemodels import CreateUpdateMixin, PlatformMixin, VersionMixin


def upload_path(instance, old_filename):
    return 'file/{ym}/{stamp}{ext}'.format(
        ym=tc.local_string(format='%Y%m'),
        stamp=tc.local_timestamp(ms=True),
        ext=os.path.splitext(old_filename)[1].lower(),
    )


class LatestAppInfo(CreateUpdateMixin):
    PAIAI_USER = 0
    PAIAI_LENSMAN = 1
    PAIAI_TOURGUIDE = 2

    SRC = (
        (PAIAI_USER, u'拍爱用户端'),
        (PAIAI_LENSMAN, u'拍爱摄影师端'),
        (PAIAI_TOURGUIDE, u'拍爱导游端'),
    )

    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 链接')

    src = models.IntegerField(_(u'src'), choices=SRC, default=PAIAI_USER, help_text=u'最新版来源', db_index=True)

    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 PatchInfo(CreateUpdateMixin, PlatformMixin):
    PAIAI_USER = 0
    PAIAI_LENSMAN = 1
    PAIAI_TOURGUIDE = 2

    SRC = (
        (PAIAI_USER, u'拍爱用户端'),
        (PAIAI_LENSMAN, u'拍爱摄影师端'),
        (PAIAI_TOURGUIDE, u'拍爱导游端'),
    )

    version = models.CharField(_(u'version'), max_length=255, 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'补丁')

    src = models.IntegerField(_(u'src'), choices=SRC, default=PAIAI_USER, help_text=u'最新版来源', db_index=True)

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

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

    @property
    def patch_url(self):
        return u'{}{}'.format(settings.DOMAIN, self.patch and self.patch.url)

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


class APPSettingsInfo(CreateUpdateMixin, PlatformMixin):
    channel = models.CharField(_(u'channel'), max_length=255, blank=True, null=True, help_text=u'渠道')
    version = models.CharField(_(u'version'), max_length=255, 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(CreateUpdateMixin):
    PAIAI_USER = 0
    PAIAI_LENSMAN = 1
    PAIAI_TOURGUIDE = 2

    SRC = (
        (PAIAI_USER, u'拍爱用户端'),
        (PAIAI_LENSMAN, u'拍爱摄影师端'),
        (PAIAI_TOURGUIDE, u'拍爱导游端'),
    )

    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'启动页面图片截止日期')
    src = models.IntegerField(_(u'src'), choices=SRC, default=PAIAI_USER, help_text=u'启动页面来源', db_index=True)

    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):
    PAIAI_USER = 0
    PAIAI_LENSMAN = 1
    PAIAI_TOURGUIDE = 2

    SRC = (
        (PAIAI_USER, u'拍爱用户端'),
        (PAIAI_LENSMAN, u'拍爱摄影师端'),
        (PAIAI_TOURGUIDE, u'拍爱导游端'),
    )

    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'用户反馈')
    src = models.IntegerField(_(u'src'), choices=SRC, default=PAIAI_USER, help_text=u'用户反馈来源', db_index=True)

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