# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division, absolute_import, print_function

import logging

from sqlalchemy import asc, and_

from zerrphix.db.tables import TABLES
from zerrphix.util.text import construct_dune_ui_entity_return_list

log = logging.getLogger(__name__)


def film_collections_all(self, **kwargs):
    return_list = []
    ZP_USER_ID = kwargs['ZP_USER_ID']
    #films = self.session.query(TABLES.ZP_FILM_COLLECTION_TITLE.TITLE,
    #                           TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.HASH,
    #                           TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID,
    #                           TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.TEMPLATE_NAME).filter(
    #    TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_TITLE_ID == TABLES.ZP_FILM_COLLECTION_TITLE.ID,
    #    TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_ID == TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID,
    #    TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID == ZP_USER_ID,
    #    TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ID == TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH_ID,
    #    TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_IMAGE_TYPE_ID == 2,
    #    TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_ID == TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_ID,
    #    TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_USER_ID == TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID).order_by(
    #    asc(TABLES.ZP_FILM_COLLECTION_TITLE.TITLE)).all()


    films = self.session.query(
        TABLES.ZP_FILM_COLLECTION_TITLE.TITLE,
        TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.HASH,
        TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID,
        TABLES.ZP_DUNE_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.EXT,
        TABLES.ZP_TEMPLATE.REF_NAME
    ).join(
        TABLES.ZP_USER_FILM_COLLECTION_ENTITY_XREF,
        and_(
            TABLES.ZP_USER_FILM_COLLECTION_ENTITY_XREF.ZP_FILM_COLLECTION_ENTITY_ID == \
            TABLES.ZP_FILM_COLLECTION_TITLE.ID,
            TABLES.ZP_USER_FILM_COLLECTION_ENTITY_XREF.ZP_FILM_COLLECTION_ENTITY_TYPE_ID == 1
        )
    ).join(
        TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH,
        TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_ID ==
        TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID
    ).join(
        TABLES.ZP_DUNE_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF,
        and_(
            TABLES.ZP_DUNE_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH_ID ==
            TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ID,
            TABLES.ZP_DUNE_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_DUNE_ID == kwargs['ZP_DUNE_ID']
        )
    ).join(
        TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF,
        TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_ID ==
        TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID
    ).join(
        ##
        TABLES.ZP_TEMPLATE, TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_TEMPLATE_ID == TABLES.ZP_TEMPLATE.ID
    ).join(
        ##
        TABLES.ZP_IMAGE_TYPE, TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_IMAGE_TYPE_ID ==
                              TABLES.ZP_IMAGE_TYPE.ID
    ).filter(
        TABLES.ZP_USER_FILM_COLLECTION_ENTITY_XREF.ZP_USER_ID == ZP_USER_ID,
        TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_USER_ID == \
        TABLES.ZP_USER_FILM_COLLECTION_ENTITY_XREF.ZP_USER_ID,
        # TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_IMAGE_TYPE_ID == 2,
        TABLES.ZP_IMAGE_TYPE.NAME == kwargs['image_type'],
        TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_IMAGE_TYPE_ID ==
        TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_IMAGE_TYPE_ID,
        TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH_ID ==
        TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ID
    ).order_by(
        asc(TABLES.ZP_FILM_COLLECTION_TITLE.TITLE)
    ).all()

        #                       ).filter(
        #TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_TITLE_ID == TABLES.ZP_FILM_COLLECTION_TITLE.ID,
        #TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_ID == TABLES.ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID,
        #TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID == ZP_USER_ID,
        #TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ID == TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH_ID,
        #TABLES.ZP_FILM_COLLECTION_IMAGE_RENDER_HASH.ZP_FILM_COLLECTION_IMAGE_TYPE_ID == 2,
        #TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_FILM_COLLECTION_ID == TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_ID,
        #TABLES.ZP_USER_FILM_COLLECTION_IMAGE_RENDER_HASH_XREF.ZP_USER_ID == TABLES.ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID).order_by(
        #asc(TABLES.ZP_FILM_COLLECTION_TITLE.TITLE)).all()
    for result in films:
        return_list.append({r'::TITLE::': result.TITLE,
                            r'::icon_url::': '{0}/film_collection/{1}/{2}/.{3}.{4}.{5}'.format(
                                kwargs['root_render_image_url_http'],
                                result.REF_NAME,
                                result.ZP_FILM_COLLECTION_ID,
                                kwargs['image_type'],
                                result.HASH,
                                result.EXT
                            ),
                            r'::menu::': 'film_list',
                            r'::args::': 'special=film_collection&ZP_FILM_COLLECTION_ID={0}'.format(
                                result.ZP_FILM_COLLECTION_ID)})
    return return_list


def film_collection(self, **kwargs):
    """
    SELECT zpfgx.ZP_FILM_ID as ZP_FILM_ID, zpft.TITLE as TITLE
    FROM ZP_FILM_GENRE_XREF zpfgx
    JOIN ZP_FILM_TITLE zpft
    ON zpft.ZP_FILM_ID = zpfgx.ZP_FILM_ID
    WHERE zpfgx.ZP_GENRE_ID = ZP_GENRE_ID
    """
    return_list = []
    ZP_USER_ID = kwargs['ZP_USER_ID']
    #films = self.session.query(TABLES.ZP_FILM_TITLE.TITLE,
    #                           TABLES.ZP_FILM_IMAGE_RENDER_HASH.HASH,
    #                           TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
    #                           TABLES.ZP_FILM_IMAGE_RENDER_HASH.TEMPLATE_NAME).filter(
    #    TABLES.ZP_FILM_COLLECTION_XREF.ZP_FILM_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ID,
    #    TABLES.ZP_FILM_COLLECTION_XREF.ZP_FILM_COLLECTION_ID == kwargs['ZP_FILM_COLLECTION_ID'][0],
    #    TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ENTITY_ID == TABLES.ZP_FILM_TITLE.ID,
    #    TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ENTITY_TYPE_ID == 1,
    #    TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
    #    TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_USER_ID == ZP_USER_ID,
    #    TABLES.ZP_FILM_IMAGE_RENDER_HASH.ID == TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_IMAGE_RENDER_HASH_ID,
    #    TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_IMAGE_TYPE_ID == 2,
    #    TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ID,
    #    TABLES.ZP_FILM_FILEFOLDER.ZP_FILM_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ID,
    #    TABLES.ZP_FILM_FILEFOLDER.ID == TABLES.ZP_FILM.ZP_FILM_FILEFOLDER_ID,
    #    TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_USER_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_USER_ID).order_by(
    #    asc(TABLES.ZP_FILM_TITLE.TITLE)).all()

    entity_search_results = self.session.query(
        TABLES.ZP_FILM_TITLE.TITLE,
        TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
        TABLES.ZP_TEMPLATE.REF_NAME,
        TABLES.ZP_FILM_IMAGE_RENDER_HASH.HASH,
        TABLES.ZP_DUNE_FILM_IMAGE_RENDER_HASH_XREF.EXT,
        TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_IMAGE_SUB_TYPE,
        TABLES.ZP_USER_FILM_WATCHED.DATETIME,
        TABLES.ZP_IMAGE_SUB_TYPE.POST_IMAGE_TYPE_TEXT,
        TABLES.ZP_IMAGE_SUB_TYPE.ID.label('ZP_IMAGE_SUB_TYPE_ID'),
        TABLES.ZP_FILM.ZP_FILM_COLLECTION_ID
    ).join(
        TABLES.ZP_USER_FILM_ENTITY_XREF,
        and_(
            TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
            TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ENTITY_ID == TABLES.ZP_FILM_TITLE.ID,
            TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_FILM_ENTITY_TYPE_ID == 1,
            TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_USER_ID == ZP_USER_ID
        )
    ).join(
        TABLES.ZP_FILM_IMAGE_RENDER_HASH,
        TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID
    ).join(
        TABLES.ZP_DUNE_FILM_IMAGE_RENDER_HASH_XREF,
        and_(
            TABLES.ZP_DUNE_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_IMAGE_RENDER_HASH_ID ==
            TABLES.ZP_FILM_IMAGE_RENDER_HASH.ID,
            TABLES.ZP_DUNE_FILM_IMAGE_RENDER_HASH_XREF.ZP_DUNE_ID == kwargs['ZP_DUNE_ID']
        )
    ).join(
        TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF,
        and_(
            TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
            TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_USER_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_USER_ID,
            TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_IMAGE_TYPE_ID ==
            TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_IMAGE_TYPE_ID,
            TABLES.ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_IMAGE_RENDER_HASH_ID == \
            TABLES.ZP_FILM_IMAGE_RENDER_HASH.ID
        )
    ).join(
        TABLES.ZP_FILM_FILEFOLDER,
        and_(
            TABLES.ZP_FILM_FILEFOLDER.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
            TABLES.ZP_FILM_FILEFOLDER.ENABLED == 1
        )
    ).join(
        TABLES.ZP_TEMPLATE, TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_TEMPLATE_ID == TABLES.ZP_TEMPLATE.ID
    ).join(
        TABLES.ZP_IMAGE_TYPE,
        and_(
            TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_IMAGE_TYPE_ID == TABLES.ZP_IMAGE_TYPE.ID,
            TABLES.ZP_IMAGE_TYPE.NAME == kwargs['image_type']
        )
    ).join(
        TABLES.ZP_IMAGE_SUB_TYPE,
        TABLES.ZP_IMAGE_SUB_TYPE.ID == TABLES.ZP_FILM_IMAGE_RENDER_HASH.ZP_IMAGE_SUB_TYPE
    ).join(
        TABLES.ZP_FILM,
        and_(
            TABLES.ZP_FILM.ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
            TABLES.ZP_FILM.ZP_FILM_FILEFOLDER_ID == TABLES.ZP_FILM_FILEFOLDER.ID
        )
    ).outerjoin(
        TABLES.ZP_USER_FILM_WATCHED,
        and_(
            TABLES.ZP_USER_FILM_WATCHED.ZP_FILM_ID == TABLES.ZP_FILM_TITLE.ZP_FILM_ID,
            TABLES.ZP_USER_FILM_WATCHED.ZP_USER_ID == TABLES.ZP_USER_FILM_ENTITY_XREF.ZP_USER_ID
        )
    ##
    ).filter(
        TABLES.ZP_FILM.ZP_FILM_COLLECTION_ID == kwargs['ZP_FILM_COLLECTION_ID'][0]
    ##
    ).order_by(
        TABLES.ZP_FILM_TITLE.TITLE.asc(), TABLES.ZP_IMAGE_SUB_TYPE.ID.asc()
    ).all()
    return_list = construct_dune_ui_entity_return_list(
        entity_search_results, 'ZP_FILM_ID', **kwargs)
    return return_list
        # return sorted(return_list, key=lambda k: k['::TITLE::'])

    """
    SELECT *
    FROM `ZP_FILM_TITLE`
    left join ZP_USER_FILM_TITLE_XREF
    on `ZP_USER_FILM_TITLE_XREF`.`ZP_FILM_TITLE_ID` = `ZP_FILM_TITLE`.`ID`
    left join ZP_FILM_IMAGE_RENDER_HASH
    on ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_ID = `ZP_FILM_TITLE`.ZP_FILM_ID
    left join ZP_FILM_RATING
    on ZP_FILM_RATING.ZP_FILM_ID = `ZP_FILM_TITLE`.ZP_FILM_ID
    left join ZP_USER_FILM_IMAGE_RENDER_HASH_XREF
    on ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_ID = `ZP_FILM_TITLE`.ZP_FILM_ID
    left join ZP_FILM_FILEFOLDER
    on ZP_FILM_FILEFOLDER.ZP_FILM_ID = `ZP_FILM_TITLE`.ZP_FILM_ID
    left join ZP_FILM
    on ZP_FILM.ID = `ZP_FILM_TITLE`.ZP_FILM_ID
    left outer join ZP_FILM_COLLECTION_XREF
    on ZP_FILM_COLLECTION_XREF.ZP_FILM_ID = `ZP_FILM_TITLE`.`ZP_FILM_ID`
    left join ZP_FILM_COLLECTION_TITLE
    on ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID = ZP_FILM_COLLECTION_XREF.ZP_FILM_COLLECTION_ID
    left join ZP_USER_FILM_COLLECTION_TITLE_XREF
    on ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_FILM_COLLECTION_ID = ZP_FILM_COLLECTION_TITLE.ZP_FILM_COLLECTION_ID

    where ZP_USER_FILM_TITLE_XREF.ZP_USER_ID = 1
    and ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_USER_ID = ZP_USER_FILM_TITLE_XREF.ZP_USER_ID
    and (ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID = 1 or ZP_USER_FILM_COLLECTION_TITLE_XREF.ZP_USER_ID is null)
    and ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_IMAGE_TYPE_ID = 2
    and ZP_USER_FILM_IMAGE_RENDER_HASH_XREF.ZP_FILM_IMAGE_TYPE_ID = ZP_FILM_IMAGE_RENDER_HASH.ZP_FILM_IMAGE_TYPE_ID
    and (substr(ZP_FILM_TITLE.TITLE, 1, 3) != substr(ZP_FILM_COLLECTION_TITLE.TITLE, 1, 3) or ZP_FILM_COLLECTION_TITLE.TITLE is null)
    order by ZP_FILM_TITLE.TITLE desc
    """
    return return_list
