#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys
import time
sys.path.append('../..')

import caching
import report


report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])


class test_slow_data(object):
    _ONE = '1'
    _TWO = '2'
    _THREE = '_property_cache_data_version_'
    _FOUR = '_property_cache_uid_'
    _FIVE = '__property_cache_uid_'
    KEYS = [_ONE, _TWO, _THREE, _FOUR, _FIVE]
    VERS = 0.1

    def data_version(self):
        return self.VERS

    def one(self):
        return self.get(self._ONE)

    def two(self):
        return self.get(self._TWO)

    def three(self):
        return self.get(self._THREE)

    def four(self):
        return self.get(self._FOUR)

    def five(self):
        return self.get(self._FIVE)

    def get(self, key, default=None):
        def print_n_sleep(k):
            sys.stdout.write('slow get executed for %s\n' % k)
            time.sleep(3)
        if key == self._ONE:
            print_n_sleep(key)
            return 'one'
        if key == self._TWO:
            print_n_sleep(key)
            return 'two'
        if key == self._THREE:
            print_n_sleep(key)
            return 'three'
        if key == self._FOUR:
            print_n_sleep(key)
            return 'four'
        if key == self._FIVE:
            print_n_sleep(key)
            return 'five'
        return default

    def keys(self):
        return self.KEYS

    def uid(self):
        return None


class tsd_cache_json(test_slow_data):
    def __init__(self, *args, **kwargs):
        test_slow_data.__init__(self, *args, **kwargs)
        self._cached_data = caching.property_cache_json(test_slow_data(*args, **kwargs), 'cache.json', load_all_on_init=False)

    def two(self):
        return test_slow_data.get(self, self._TWO)

    def get(self, key, default=None):
        return self._cached_data.get(key, default)


data = tsd_cache_json()
print('Testing property_cache (json):\\n--------------------------------')
print(data.one())
print(data.two())
print(data.three())
print(data.four())
print(data.five())
