import os
from datetime import timedelta
from moviepy.editor import VideoFileClip
from random import randrange

from django.contrib import admin
from curation.models import Video, Playlist, PlaylistVideo, \
    Artist, VideoArtistProxy, Transition

from instavision.settings import MEDIA_ROOT


class PlaylistVideoInline(admin.TabularInline):
    model = PlaylistVideo
    extra = 1
    ordering = ('play_order',)


class VideoInline(admin.TabularInline):
    model = VideoArtistProxy
    extra = 1
    # ordering = ('title',)
    #fields = ('',)

    verbose_name = "Video"
    verbose_name_plural = "Videos"


class PlaylistAdmin(admin.ModelAdmin):
    inlines = (PlaylistVideoInline,)
    
    list_display = ('id', 'title', 'play_order', 'get_num_videos', 'length_field', 'active',)
    list_display_links = ('id', 'title',)
    ordering = ('play_order',)
    readonly_fields = ('auto',)
    
    # Playlist length field
    def length_field(self, obj):
        return str(timedelta(seconds=obj.get_length().seconds))
    length_field.short_description = 'Length'
    
    # Playlsit videos field
    def get_num_videos(self, obj):
        return obj.get_num_videos()
    get_num_videos.short_description = 'Videos'
    

class VideoAdmin(admin.ModelAdmin):

    list_display = ('id', 'title', 'get_byline', 'length_field', 'type', 'active')
    list_display_links = ('id', 'title',)
    readonly_fields = ('length', 'plays')
    search_fields = ('title', 'artist__name')

    # Playlist length field
    def length_field(self, obj):
        return str(timedelta(seconds=obj.length.seconds))
    length_field.short_description = 'Length'
    
    def get_byline(self, obj):
        return obj.get_byline()
    get_byline.short_description = 'Artist'
    
    def save_model(self, request, obj, form, change):
        obj.save()
        if obj.video_file != obj._Video__original_video_file:
            # determine clip length
            file_location = '%s%s' % (MEDIA_ROOT, obj.video_file)
            clip = VideoFileClip(str(file_location))
            obj.length = timedelta(seconds=clip.duration)
            obj.save()


class ArtistAdmin(admin.ModelAdmin):
    inlines = (VideoInline,)
    list_display = ('id', 'name', 'country', 'num_videos')
    list_display_links = ('id', 'name',)
    search_fields = ('name', 'country')
    
    def num_videos(self, obj):
        return obj.num_videos()
    num_videos.short_description = 'Videos'
    

class TransitionAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'frequency', 'active')
    list_display_links = ('id', 'title',)
    readonly_fields = ('length', 'plays')
    search_fields = ('title',)
    
    def save_model(self, request, obj, form, change):
        obj.save()
        if obj.video_file != obj._Transition__original_video_file:
            # determine clip length
            file_location = '%s%s' % (MEDIA_ROOT, obj.video_file)
            clip = VideoFileClip(str(file_location))
            obj.length = timedelta(seconds=clip.duration)
            obj.save()


admin.site.register(Video, VideoAdmin)
admin.site.register(Playlist, PlaylistAdmin)
admin.site.register(Artist, ArtistAdmin)
admin.site.register(Transition, TransitionAdmin)

