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

# pylint: disable=E1101

from sqlalchemy import sql
from sqlalchemy import orm

from sqlalchemy.orm.exc import NoResultFound

from .. import Base

# http://www.iana.org/assignments/media-types/media-types.xhtml


class MimeMajor(Base):
    """Mime major"""

    def __init__(self, name):
        super().__init__()
        self.name = name


class Mime(Base):

    def __init__(self, name, template, major):
        super().__init__()
        self.name = name
        self.template = template
        self.major = major

    @property
    def full(self):
        return '{0}/{1}'.format(self.major.name, self.name)

    @staticmethod
    def q_major_minor(dbsession, major, minor):
        cond = sql.and_(
            MimeMajor.name == major,
            Mime.name == minor
        )

        result = dbsession.execute(
            sql.select(Mime).join(Mime.major).options(
                orm.contains_eager(Mime.major)
            ).filter(cond)
        ).scalar_one_or_none()

        return result

    ###########
    # Filters #
    ###########

    @classmethod
    def filter_mime(cls, value):
        (major, minor) = value.split('/')
        cond = sql.and_()
        cond.append(MimeMajor.name == major)
        if minor and minor != '*':
            cond.append(Mime.name == minor)
        return cond

[(37, 'sqlalchemy.sql.and_', 'sql.and_', False, 'from sqlalchemy import sql\n'), (57, 'sqlalchemy.sql.and_', 'sql.and_', False, 'from sqlalchemy import sql\n'), (44, 'sqlalchemy.orm.contains_eager', 'orm.contains_eager', False, 'from sqlalchemy import orm\n'), (43, 'sqlalchemy.sql.select', 'sql.select', False, 'from sqlalchemy import sql\n')]