# -*- encoding: utf-8 -*-

import sys, os, datetime

from sqlalchemy.orm import mapper, relation, scoped_session, sessionmaker
from sqlalchemy.orm.collections import column_mapped_collection

import tables, strorder


class Part(object):

    def __init__(self, url, name, title):
       self.url = url
       self.name = name
       self.title = title

    def insert_session(self, pos, session):
        strorder.insert(self, pos, session)

    def append_session(self, session):
        strorder.append(self, session)


class SearchPart(object):

    def __init__(self, title, order):
        self.title = title
        self.order = order


class Session(object):

    def __init__(self, url, name, title, location, begin, end):
        self.url = url
        self.name = name
        self.title = title
        self.location = location
        self.begin = begin
        self.end = end

    def insert_paper(self, pos, paper):
        strorder.insert(self, pos, paper)

    def append_paper(self, paper):
        strorder.append(self, paper)


class Paper(object):

    def __init__(self, url, name, html_link, html_extra, html, latex, search, search_title, search_authors, search_presenter, search_session, search_location, type, part, joint, begin, end):
        self.url = url
        self.name = name
        self.html_link = html_link
        self.html_extra = html_extra
        self.html = html
        self.latex = latex
        self.search = search
        self.search_title = search_title
        self.search_authors = search_authors
        self.search_presenter = search_presenter
        self.search_session = search_session
        self.search_location = search_location
        self.type = type
        self.part = part
        self.joint = joint
        self.begin = begin
        self.end = end


class State(object):

    pass


class StateSession(object):

    def __init__(self, choice, state, session):
        self.choice = choice
        self.state = state
        self.session = session


class StatePaper(object):

    def __init__(self, choice, state, paper):
        self.choice = choice
        self.state = state
        self.paper = paper


class Save(object):

    def __init__(self, login, passwd_hash):
        self.login = login
        self.passwd_hash = passwd_hash


class SaveSession(object):

    def __init__(self, choice, save, session):
        self.choice = choice
        self.save = save
        self.session = session


class SavePaper(object):

    def __init__(self, choice, save, paper):
        self.choice = choice
        self.save = save
        self.paper = paper


class Update(object):

    def __init__(self, number, released):
        self.number = number
        self.released = released


class UpdateItem(object):

    def __init__(self, description, paper, update):
        self.description = description
        self.paper = paper
        self.update = update


mapper(StateSession, tables.state_session_table)

mapper(StatePaper, tables.state_paper_table)

mapper(State, tables.state_table,
       properties={'sessions': relation(StateSession,
                                        backref='state'),
                   'papers': relation(StatePaper,
                                      backref='state')})

mapper(SaveSession, tables.save_session_table)

mapper(SavePaper, tables.save_paper_table)

mapper(Save, tables.save_table,
       properties={'sessions': relation(SaveSession,
                                        backref='save'),
                   'papers': relation(SavePaper,
                                      backref='save')})

mapper(Part, tables.part_table,
       properties={'sessions': relation(Session,
                                        backref='part',
                                        order_by=[tables.session_table.c.part_order])})

mapper(Session, tables.session_table,
       properties={'papers': relation(Paper,
                                      backref='session',
                                      order_by=[tables.paper_table.c.session_order]),
                   'states': relation(StateSession,
                                      backref='session',
                                      collection_class=column_mapped_collection(tables.state_session_table.c.state_id)),
                   'save': relation(SaveSession,
                                    backref='session')})

mapper(Paper, tables.paper_table,
       properties={'states': relation(StatePaper,
                                      backref='paper',
                                      collection_class=column_mapped_collection(tables.state_paper_table.c.state_id)),
                   'save': relation(SavePaper,
                                    backref='paper'),
                   'updates': relation(UpdateItem,
                                       backref='paper')})

mapper(UpdateItem, tables.update_item_table)

mapper(Update, tables.update_table,
       properties={'items': relation(UpdateItem,
                                     backref='update')})

DB = scoped_session(sessionmaker(bind=tables.engine))
