"""
Command to help development of pdf generation
"""
import datetime
import os

from django.conf import settings
from django.core.management.base import NoArgsCommand
from django.template import RequestContext
from django.template.loader import render_to_string
from rldjango.apps.destinfo.views import make_pdf
from rldjango.apps.destinfo.utils import (RequestFactory, prepare_toc_entry,
                                                prepare_outline_entry, create_guide
                                                )

 

class Command(NoArgsCommand):
    """
    Used only for development
    """
    def handle_noargs(self, **options):
        """
        Just generate poi pdf
        """
        request = RequestFactory().get('/')

        path = {
            'font': os.path.join(settings.MEDIA_ROOT, 'destinfo', 'fonts'),
            'pdf': os.path.join(settings.MEDIA_ROOT, 'destinfo', 'pdf'),
            'img': os.path.join(settings.MEDIA_ROOT, 'destinfo', 'img')
            }

        images = {
            'cover': os.path.join(path['pdf'], 'cover_en.pdf'),
            }

        toc = {
            'name': 'test toc title'
            }

        created = {
            'name': 'Created',
            'content': datetime.date.today().strftime('%d-%m-%y')
            }

        page = {
            'name': 'Page',
            'mode': 'UseOutlines',
            'size': '(842, 595.5)',
            }

        font = {
            'heading': 'MrsEavesItalic',
            'body': 'BlissLight'
            
            }

        context = RequestContext(request, {
            'IMADJ_ROOT': settings.IMADJ_ROOT,
            'font': font,
            'path': path,
            'images': images,
            'toc': toc,
            'created': created,
            'page': page,
            })

        from rldjango.apps.destinfo.models import Location, Narrative
        from project.stb.models import Accomodation
        accomodations = Accomodation.objects.search('Berjaya Beau Vallon')
        # accomodations = Accomodation.objects.search('hotel')        
        locations = {}
        for acc in accomodations:
            locations.setdefault(acc.location.id, {
                'location': acc.location,
                'accomodations': []
                })
            locations[acc.location.id]['accomodations'].append(acc)

        brochure = []
        toc = []

        guides = []

        for guide in Narrative.tree.root_nodes(): # pylint: disable-msg=E1101
            create_guide(context, brochure, toc, guide, outline_level=0, toc_level=0)


        # brochure.append({
        #     'new_page': {
        #         'before': False,
        #         'template': 'accommodation',
        #         'after': False,
        #         },
        #     'toc_entry': '',
        #     'outline_entry': prepare_outline_entry(text='Locations', level=0),
        #     'keep_in_frame': False,
        #     'obj': None
        #     })


        # for location_dict in locations.values():

        #     location = location_dict['location']
            
        #     toc_entry = prepare_toc_entry(text=location.name, toc_id=location.get_uniq_id(),
        #                                   context=context)
        #     toc.append(toc_entry)
        #     brochure.append({
        #         'new_page': {
        #             'before': True,
        #             'template': 'accommodation',
        #             'after': False,
        #             },
        #         'toc_entry': toc_entry,
        #         'outline_entry': prepare_outline_entry(text=location.name, level=1),
        #         'keep_in_frame': False,
        #         'obj': location
        #         })
            
        #     brochure.append({
        #         'new_page': {
        #             'before': False,
        #             'template': 'accommodation',
        #             'after': False,
        #             },
        #         'toc_entry': '',
        #         'outline_entry': prepare_outline_entry(text='Accomodations', level=2),
        #         'keep_in_frame': False,
        #         'obj': None
        #         })

        #     for acc in location_dict['accomodations']:
        #         toc_entry = prepare_toc_entry(text=acc.name, toc_id=acc.get_uniq_id(),
        #                                   context=context, level=1)
        #         toc.append(toc_entry)

        #         brochure.append({
        #             'new_page': {
        #                 'before': True,
        #                 'template': 'accommodation',
        #                 'after': False,
        #                 },
        #             'toc_entry': toc_entry,
        #             'outline_entry': prepare_outline_entry(text=acc.name, level=3),
        #             'keep_in_frame': False,
        #             'obj': acc
        #             })

        #     brochure.append({
        #         'new_page': {
        #             'before': True,
        #             'template': 'accommodation2column',
        #             'after': False,
        #             },
        #         'toc_entry': '',
        #         'outline_entry': prepare_outline_entry(text='Points of interest', level=2),
        #         'keep_in_frame': False,
        #         'obj': None
        #         })


        #     for poi in location.poi_set.all().order_by('category'):
        #         if poi.get_to_rml_images():
        #             brochure.append({
        #                 'new_page': {
        #                     'before': False,
        #                     'template': 'accommodation2column',
        #                     'after': False,
        #                     },
        #                 'next_frame': {
        #                     'before': False,
        #                     'after': True,
        #                     },
        #                 'toc_entry': '',
        #                 'outline_entry': prepare_outline_entry(text=poi.name, level=3),
        #                 'keep_in_frame': False,
        #                 'obj': poi
        #                 })
    
        
        context['brochure'] = brochure
        context['toc'] = toc
        context['context_ref'] = context
   
        rml = render_to_string('rml/brochure.rml', context).encode('utf-8')
        make_pdf(rml)

