# 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 os
import time
import uuid

from .qommon import sessions
from .qommon.sessions import Session

class BasicSession(Session):

    magictokens = None
    anonymous_formdata_keys = None
    visiting_objects = None
    data_source_query_url_tokens = None

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

    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 list(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 select_recent(cls, seconds=30*60, **kwargs):
        objects_dir = cls.get_objects_dir()
        if not os.path.exists(objects_dir):
            return []
        ids = []
        time_limit = time.time() - seconds
        for filename in os.listdir(objects_dir):
            if filename[0] == '.':
                continue
            if os.stat(os.path.join(objects_dir, filename)).st_mtime > time_limit:
                ids.append(filename)
        return cls.get_ids(ids, **kwargs)

    @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_recent(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_sessions_with_visited_object(cls, object_key):
        for session in cls.select_recent(ignore_errors=True):
            visiting_objects = getattr(session, 'visiting_objects', None)
            if not visiting_objects:
                continue
            if not visiting_objects.get(object_key):
                continue
            yield session

    @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.get_sessions_with_visited_object(object_key):
            object_timestamp = session.visiting_objects.get(object_key)
            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_recent(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()

    def get_data_source_query_url_token(self, url):
        if not self.data_source_query_url_tokens:
            self.data_source_query_url_tokens = {}
        for key, value in self.data_source_query_url_tokens.items():
            if value == url:
                return key
        key = str(uuid.uuid4())
        self.data_source_query_url_tokens[key] = url
        self.store()
        return key

    def get_data_source_query_url_from_token(self, token):
        if not self.data_source_query_url_tokens:
            return None
        return self.data_source_query_url_tokens.get(token)

sessions.BasicSession = BasicSession
StorageSessionManager = sessions.StorageSessionManager
