from django.utils.translation import gettext as _
import fractions
import geo
from ..models import get_item_type, TYPE_AUDIO, TYPE_FOLDER, TYPE_IMAGE, TYPE_VIDEO
from .userviews import base_view as base_userview


def get_wrapper_class(full_path):
    return {
        TYPE_FOLDER: folder_view,
        TYPE_IMAGE: image_view,
        TYPE_VIDEO: video_view,
        TYPE_AUDIO: audio_view,
    }.get(get_item_type(full_path), base_view)


def get_wrapper_instance(full_path, request):
    return get_wrapper_class(full_path)(request, full_path)


class base_view(base_userview):
    def __init__(self, *args, **kwargs):
        base_userview.__init__(self, *args, **kwargs)
        self.template = 'pygal/infoview.html'

    def context_adaption(self, context):
        base_userview.context_adaption(self, context)
        context['heading'] = {
            'general': _('General'),
            'information': _('Information'),
            'database': _('Database')
        }

    @property
    def database_information(self):
        rv = []
        if self.request.user.is_superuser:
            if self.item is not None:
                rv.append({'description': 'DB-Item', 'data': 'Item (%d)' % self.item.id, 'url': self.item.get_admin_url()})
            i = 0
            for t in self.item.tag_set.all():
                i += 1
                rv.append({'description': 'TAG%d - %s' % (i % 10, t.text), 'data': 'Tag (%d)' % t.id, 'url': t.get_admin_url()})
        return rv

    @property
    def item_information(self):
        return []

    @property
    def upload_information(self):
        rv = []
        return rv


class folder_view(base_view):
    def __init__(self, *args, **kwargs):
        base_view.__init__(self, *args, **kwargs)

    @property
    def item_information(self):
        rv = []
        rv.append({'description': _('Number of Folder(s)'), 'data': self.item.item_data.num_folders, 'url': None})
        rv.append({'description': _('Number of Audio File(s)'), 'data': self.item.item_data.num_audio, 'url': None})
        rv.append({'description': _('Number of Image(s)'), 'data': self.item.item_data.num_images, 'url': None})
        rv.append({'description': _('Number of Other File(s)'), 'data': self.item.item_data.num_other, 'url': None})
        rv.append({'description': _('Number of Video(s)'), 'data': self.item.item_data.num_videos, 'url': None})
        return rv


class image_view(base_view):
    def __init__(self, *args, **kwargs):
        base_view.__init__(self, *args, **kwargs)

    @property
    def item_information(self):
        rv = []
        rv.append({'description': _('Camera'), 'data': '%s' % (self.item.item_data.camera), 'url': None})
        rv.append({'description': _('Resolution'), 'data': '%d x %d' % (self.item.item_data.width, self.item.item_data.height), 'url': None})
        gps_data = self.item.item_data.gps
        if gps_data is not None:
            c = geo.gps.coordinate(**gps_data)
            rv.append({'description': _('GPS'), 'data': str(c), 'url': geo.osm.landmark_link(c)})
        rv.append({'description': _('Exposure Program'), 'data': self.item.item_data.exposure_program, 'url': None})
        f = fractions.Fraction(self.item.item_data.exposure_time).limit_denominator()
        rv.append({'description': _('Exposure Time'), 'data': '%d/%d s' % (f.numerator, f.denominator), 'url': None})
        rv.append({'description': _('ISO'), 'data': self.item.item_data.iso, 'url': None})
        rv.append({'description': _('Focal Length'), 'data': self.item.item_data.focal_length, 'url': None})
        rv.append({'description': _('Apeture'), 'data': self.item.item_data.f_number, 'url': None})
        rv.append({'description': _('Flash'), 'data': self.item.item_data.flash, 'url': None})
        rv.append({'description': _('Orientation'), 'data': self.item.item_data.orientation, 'url': None})
        return rv


class video_view(base_view):
    def __init__(self, *args, **kwargs):
        base_view.__init__(self, *args, **kwargs)

    @property
    def item_information(self):
        rv = []
        rv.append({'description': _('Duration'), 'data': self.__duration_txt__(self.item.item_data.duration), 'url': None})
        rv.append({'description': _('Resolution'), 'data': '%d x %d' % (self.item.item_data.width, self.item.item_data.height), 'url': None})
        rv.append({'description': _('Ratio'), 'data': self.item.item_data.ratio, 'url': None})
        return rv


class audio_view(base_view):
    def __init__(self, *args, **kwargs):
        base_view.__init__(self, *args, **kwargs)

    @property
    def item_information(self):
        rv = []
        rv.append({'description': _('Artist'), 'data': self.item.item_data.artist, 'url': None})
        rv.append({'description': _('Album'), 'data': self.item.item_data.album, 'url': None})
        rv.append({'description': _('Year'), 'data': self.item.item_data.year, 'url': None})
        rv.append({'description': _('Title'), 'data': self.item.item_data.title, 'url': None})
        rv.append({'description': _('Track'), 'data': self.item.item_data.track, 'url': None})
        rv.append({'description': _('Duration'), 'data': self.__duration_txt__(self.item.item_data.duration), 'url': None})
        rv.append({'description': _('Genre'), 'data': self.item.item_data.genre, 'url': None})
        rv.append({'description': _('Bitrate'), 'data': self.__size_txt__(self.item.item_data.bitrate), 'url': None})
        return rv
