# -*- coding: utf8 -*-
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator
from pyramid.renderers import JSON, JSONP
from pyramid.session import SignedCookieSessionFactory
from pyramid.events import BeforeRender
from sqlalchemy import engine_from_config
from pyramid.renderers import render_to_response
from .models import DBSession, get_user, get_sponsors, get_exposants
from .security import EntryFactory, groupfinder
from pyramid_mailer import mailer_factory_from_settings
from pyramid_mailer.message import Attachment, Message
import locale
from .helpers import Sejour_helpers, Orga_helpers
from apscheduler.schedulers.background import BackgroundScheduler
# Database access imports
from pyramid.request import Request
from mako.template import Template
from .models import User
from jm2l.const import CurrentYear
from models import JM2L_Year

import logging

def add_renderer_globals(event):
    event['mytrip'] = Sejour_helpers(event)
    event['myorga'] = Orga_helpers(event)
    event['CurrentYear'] = CurrentYear

#@sched.scheduled_job('cron', day_of_week='sun', hour=22, minute=07)
def mailer_tasks(config):
    # Send the Welcome Mail
    mailer = config.registry['mailer']
    Contact = DBSession.query(User).filter(User.uid==1).one()
    request = Request.blank('/', base_url='http://jm2l.linux-azur.org')
    request.registry = config.registry
    for StaffUser in DBSession.query(User).filter(User.Staff == True):
        # Skip mail to contact
        if StaffUser==Contact:
            continue
        # Skip those that have no task assigned
        if len(filter(lambda k:not k.closed, StaffUser.task_assoc))==0:
            continue
        
        # Prepare Plain Text Message :
        Mail_template = Template(filename='jm2l/templates/mail_plain.mako')
        mail_plain = Mail_template.render(request=request, User=StaffUser, Contact=Contact, action="Tasks")
        # Prepare HTML Message :
        Mail_template = Template(filename='jm2l/templates/mail_html.mako')
        mail_html = Mail_template.render(request=request, User=StaffUser, Contact=Contact, action="Tasks")
        # Prepare Message
        message = Message(subject="[JM2L] Le mail de rappel pour les JM2L !",
                          sender="contact@jm2l.linux-azur.org",
                          recipients=[StaffUser.mail],
                          body=mail_plain, html=mail_html)
    
        message.add_bcc("spam@style-python.fr")
        
        mailer.send_immediately(message)

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    # Extract secrets from configuration file if any
    CookiesPasswd = settings.get('secret_Cookies', 'itsthefirstseekreet')
    AuthTktPasswd = settings.get('secret_AuthTkt', 'itsthesecondseekreet')
    my_session_factory = SignedCookieSessionFactory(CookiesPasswd)
    authentication_policy = AuthTktAuthenticationPolicy(AuthTktPasswd,
                    callback=groupfinder, hashalg='sha512', debug=True)
    authorization_policy = ACLAuthorizationPolicy()
    config = Configurator(settings=settings,
                          root_factory='.security.RootFactory',
                          authentication_policy=authentication_policy,
                          authorization_policy=authorization_policy
                          )
    config.add_subscriber(add_renderer_globals, BeforeRender) 
    config.registry['mailer'] = mailer_factory_from_settings(settings)
    config.registry['event_date'] = JM2L_Year.get_latest_jm2l_startdate()
    sched = BackgroundScheduler()
    sched.add_job(mailer_tasks, 'cron', day_of_week='fri', hour=18, args=[ config ])
    sched.start()        # start the scheduler
    config.add_renderer('json', JSON(indent=4))
    config.add_renderer('jsonp', JSONP(param_name='callback'))
    config.set_session_factory(my_session_factory)
    config.add_request_method(get_user, 'user', reify=True)
    config.add_request_method(get_sponsors, 'sponsors', reify=False)
    config.add_request_method(get_exposants, 'exposants', reify=False)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view('img', 'static/img', cache_max_age=3600)
    config.add_static_view('css', 'static/css', cache_max_age=3600)
    config.add_static_view('js', 'static/js', cache_max_age=3600)
    config.add_static_view('vendor', 'static/vendor', cache_max_age=3600)
    config.add_static_view('upload', 'upload', cache_max_age=3600)
    config.add_static_view('resources', 'resources', cache_max_age=3600)
    
    # ICal Routes
    config.add_route('progr_iCal', '/{year:\d+}/JM2L.ics')
    config.add_route('progr_dyn_iCal', '/{year:\d+}/JM2L_dyn.ics')

    # JSON Routes
    config.add_route('users_json', '/json-users')
    config.add_route('tiers_json', '/json-tiers')
    config.add_route('progr_json', '/{year:\d+}/le-prog-json')
    config.add_route('timeline_json', '/{year:\d+}/timeline-json')      
    
    # Session setting Routes
    config.add_route('year', '/year/{year:\d+}')
    config.add_route('vote_logo', '/vote_logo/{num:\d+}')
    
    # HTML Routes - Staff
    config.add_route('Live',             '/Live')    
    config.add_route('list_expenses',     '/{year:\d+}/Staff/compta')
    config.add_route('list_task',        '/{year:\d+}/Staff')
    config.add_route('handle_pole',      '/{year:\d+}/Staff/poles{sep:/*}{pole_id:(\d+)?}')
    config.add_route('handle_task',      '/{year:\d+}/Staff/tasks{sep:/*}{task_id:(\d+)?}')
    config.add_route('action_task',      '/{year:\d+}/Staff/{action:(\w+)}/{task_id:(\d+)}')
    config.add_route('action_task_area', '/{year:\d+}/Staff/pole/{action:(\w+)}/{pole_id:(\d+)}')

    config.add_route('list_salles',      '/ListSalles')
    config.add_route('handle_salle',     '/Salles{sep:/*}{salle_id:(\d+)?}')
    config.add_route('handle_salle_phy', '/PhySalles{sep:/*}{salle_id:(\d+)?}')
    config.add_route('action_salle',     '/Salles/{action:(\w+)}/{salle_id:(\d+)}')
    config.add_route('pict_salle',       '/salle_picture/{salle_id:(\d+)}')

    config.add_route('list_users',      '/{year:\d+}/ListParticipant')
    config.add_route('list_orga',       '/{year:\d+}/ListOrga')

    # HTML Routes - Public
    config.add_route('home', '/{year:(\d+/)?}')
    config.add_route('edit_index', '/{year:\d+}/edit')
    config.add_route('presse', '/{year:\d+}/dossier-de-presse')
    config.add_route('edit_presse', '/{year:\d+}/dossier-de-presse/edit')    
    config.add_route('programme', '/{year:\d+}/le-programme')
    config.add_route('plan', 'nous-rejoindre')
    config.add_route('participer', 'participer-l-evenement')
    config.add_route('captcha', '/captcha')
    
    ## Events
    config.add_route('event', '/event/{year:\d+}/{event_id:([\w-]+)?}')
    config.add_route('link_event_user', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_user')
    config.add_route('delete_link_u', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_user')
    config.add_route('link_event_tiers', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_tiers')
    config.add_route('delete_link_t', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_tiers')
    config.add_route('edit_event', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}')
    config.add_route('delete_event', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}/delete')
    
    ## Entities
    config.add_route('entities', '/entities') #{sep:/*}{Nature:\w+?}')
    config.add_route('add_entity',  '/entity')
    config.add_route('delete_entity', '/entity/{entity_id:(\d+)}/delete')
    config.add_route('show_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)?}')
    config.add_route('edit_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)}/edit')
    config.add_route('edit_entity_cat', '/categorie/entity')
    
    ## Users
    config.add_route('pict_user', '/user_picture')
    config.add_route('show_user', '/user/{user_slug:([\w-]+)?}')
    config.add_route('badge_user', '/user/{user_slug:([\w-]+)?}/badge')
    config.add_route('all_badges', '/badges')
    config.add_route('place_print', '/place_print')
    config.add_route('stand_print', '/stand_print')    
    
    # HTML Routes - Logged
    #config.add_route('profil', 'MesJM2L')    
    config.add_route('jm2l', '/MesJM2L')
    config.add_route('miam', '/MonMiam')
    config.add_route('sejour', '/MonSejour')
    config.add_route('orga', '/MonOrga')
    config.add_route('modal', '/{year:\d+}/modal/{modtype:\w+}/{id:(\d+)}')    
    
    # Handle exchanges
    config.add_route('exchange', '/{year:\d+}/exchange/{modtype:\w+}/{id:(\d+)}/{action:\w+}')

    # Handle authentication
    config.add_route('register', '/register')
    config.add_route('auth', '/sign/{action}')
    config.add_route('bymail', '/sign/jm2l/{hash}')
    
    # Handle Multimedia and Uploads
    config.add_route('media_view', '/image/{media_table:\w+}/{uid:\d+}/{name:.+}')
    config.add_route('media_upload', '/uploader/{media_table:\w+}/{uid:\d+}/proceed{sep:/*}{name:.*}')

    config.scan()
    return config.make_wsgi_app()


