# w.c.s. - web application for online forms
# Copyright (C) 2005-2010  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

import random
import time

import qommon.sessions
from qommon.sessions import Session

class BasicSession(Session):

    anonymous_key = None
    magictokens = None
    anonymous_formdata_keys = None
    visiting_objects = None

    def has_info(self):
        return (self.anonymous_formdata_keys or self.anonymous_key or
                self.magictokens or self.visiting_objects or Session.has_info(self))
    is_dirty = has_info

    def get_anonymous_key(self, generate = False):
        if self.anonymous_key:
            return self.anonymous_key
        if generate:
            self.anonymous_key = random.randint(0, 1000000000)
        return self.anonymous_key

    def add_magictoken(self, token, data):
        if not self.magictokens:
            self.magictokens = {}
        self.magictokens[token] = data

    def get_by_magictoken(self, token, default = None):
        if not self.magictokens:
            return default
        return self.magictokens.get(token, default)

    def remove_magictoken(self, token):
        if not self.magictokens:
            return
        if token in self.magictokens:
            del self.magictokens[token]

    def mark_anonymous_formdata(self, formdata):
        if not self.anonymous_formdata_keys:
            self.anonymous_formdata_keys = {}
        self.anonymous_formdata_keys['%s-%s' % (formdata.formdef.id, formdata.id)] = True

    def is_anonymous_submitter(self, formdata):
        if not self.anonymous_formdata_keys:
            return False
        formdata_key = '%s-%s' % (formdata.formdef.id, formdata.id)
        return formdata_key in self.anonymous_formdata_keys

    def mark_visited_object(self, key):
        if not self.visiting_objects:
            self.visiting_objects = {}
        # first clean older objects
        current_timestamp = time.time()
        for object_key, object_timestamp in self.visiting_objects.items():
            if object_timestamp < (current_timestamp - 30*60):
                del self.visiting_objects[object_key]
        self.visiting_objects[key] = current_timestamp

    @classmethod
    def get_visited_objects(cls, exclude_user=None):
        # return the list of visited objects
        current_timestamp = time.time()
        visited_objects = {}
        for session in cls.select(ignore_errors=True):
            if session.user and session.user == exclude_user:
                continue
            visiting_objects = getattr(session, 'visiting_objects', None)
            if not visiting_objects:
                continue
            for object_key, object_timestamp in visiting_objects.items():
                if object_timestamp > (current_timestamp - 30*60):
                    visited_objects[object_key] = True
        return visited_objects.keys()

    @classmethod
    def get_object_visitors(cls, object_key):
        '''return tuples of (user_id, last_visit_timestamp)'''
        current_timestamp = time.time()
        visitors = {}
        for session in cls.select(ignore_errors=True):
            visiting_objects = getattr(session, 'visiting_objects', None)
            if not visiting_objects:
                continue
            object_timestamp = visiting_objects.get(object_key)
            if not object_timestamp:
                continue
            if object_timestamp > (current_timestamp - 30*60):
                visitors[session.user] = max(object_timestamp, visitors.get(session.user, 0))
        return visitors.items()

    def unmark_visited_object(self, object_key):
        # remove from current session
        if object_key in (getattr(self, 'visiting_objects', None) or {}):
            del self.visiting_objects[object_key]
        # and from others
        for session in self.__class__.select(ignore_errors=True):
            if session.id == self.id:
                continue
            visiting_objects = getattr(session, 'visiting_objects', None)
            if not visiting_objects:
                continue
            if object_key in visiting_objects:
                del session.visiting_objects[object_key]
                session.store()

qommon.sessions.BasicSession = BasicSession
StorageSessionManager = qommon.sessions.StorageSessionManager
