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

import os

from django.db import models
from django.utils.translation import ugettext_lazy as _
from shortuuidfield import ShortUUIDField
from TimeConvert import TimeConvert as tc

from course.basemodels import CreateUpdateMixin
from utils.url_utils import upload_file_url


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 CourseInfo(CreateUpdateMixin):
    course_id = ShortUUIDField(_(u'course_id'), max_length=255, help_text=u'课程唯一标识', db_index=True, unique=True)
    course_name = models.CharField(_(u'course_name'), max_length=255, blank=True, null=True, help_text=u'课程标题')
    course_time = models.IntegerField(_(u'course_time'), default=0, help_text=u'课程时间')
    course_cover = models.ImageField(_(u'course_cover'), upload_to=upload_path, blank=True, null=True, help_text=u'课程视频缩略图')
    course_position = models.IntegerField(_(u'course_position'), default=0, help_text=u'课程排序')

    class Meta:
        verbose_name = _(u'courseinfo')
        verbose_name_plural = _(u'courseinfo')

    def __unicode__(self):
        return unicode(self.course_name)

    @property
    def course_cover_url(self):
        return upload_file_url(self.course_cover)

    @property
    def data(self):
        return {
            'course_id': self.course_id,
            'course_name': self.course_name,
            'course_time': self.course_time,
            'course_cover_url': self.course_cover_url,
        }


class CourseVideoInfo(CreateUpdateMixin):
    course = models.ForeignKey(CourseInfo, verbose_name=_(u'course'), blank=True, null=True, help_text=u'课程', db_index=True)
    course_video_id = ShortUUIDField(_(u'course_video_id'), max_length=255, help_text=u'课程视频唯一标识', db_index=True, unique=True)
    course_video_type = models.CharField(_(u'course_video_type'), max_length=255, blank=True, null=True, help_text=u'课程视频类型')
    course_video_name = models.CharField(_(u'course_video_name'), max_length=255, blank=True, null=True, help_text=u'课程视频标题')
    course_video_desc = models.TextField(_(u'course_video_desc'), blank=True, null=True, help_text=u'课程视频描述')
    course_video_time = models.IntegerField(_(u'course_video_time'), default=0, help_text=u'课程视频时间')
    course_video_cover = models.ImageField(_(u'course_video_cover'), upload_to=upload_path, blank=True, null=True, help_text=u'课程视频缩略图')
    course_video = models.FileField(_(u'course_video'), upload_to=upload_path, blank=True, null=True, help_text=u'课程视频')
    course_video_position = models.IntegerField(_(u'course_video_position'), default=0, help_text=u'课程视频排序')

    class Meta:
        verbose_name = _(u'coursevideoinfo')
        verbose_name_plural = _(u'coursevideoinfo')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def course_video_cover_url(self):
        return upload_file_url(self.course_video_cover)

    @property
    def course_video_url(self):
        return upload_file_url(self.course_video)

    @property
    def data(self):
        return {
            'course_id': self.course.course_id,
            'course_video_id': self.course_video_id,
            'course_video_type': self.course_video_type,
            'course_video_name': self.course_video_name,
            'course_video_desc': self.course_video_desc,
            'course_video_time': self.course_video_time,
            'course_video_cover_url': self.course_video_cover_url,
            'course_video_url': self.course_video_url,
            'course_video_position': self.course_video_position,
        }
