# coding: utf-8
from .autopath import do_autopath
do_autopath()

from py.test import raises

from tiramisu.error import ConfigError
from tiramisu.config import Config
from tiramisu.option import BoolOption, OptionDescription, MasterSlaves
from tiramisu.setting import groups, owners
from tiramisu.storage import list_sessions, delete_session


def test_non_persistent():
    b = BoolOption('b', '')
    o = OptionDescription('od', '', [b])
    Config(o, session_id='test_non_persistent')


#def test_list():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    c = Config(o, session_id='test_non_persistent')
#    c.cfgimpl_get_settings().remove('cache')
#    c.b = True
#    assert 'test_non_persistent' in list_sessions('config')
#    del(c)
#    assert 'test_non_persistent' not in list_sessions('config')
#
#
#def test_create_persistent():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#
#
#def test_create_delete_not_persistent():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        raises(ValueError, "delete_session('option', 'test_persistent')")
#
#
#def test_list_sessions_persistent():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.b = True
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert 'test_persistent' in list_sessions('config')
#
#
#def test_delete_session_persistent():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert 'test_persistent' in list_sessions('config')
#        delete_session('config', 'test_persistent')
#        assert 'test_persistent' not in list_sessions('config')
#
#
#def test_create_persistent_retrieve():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert c.b is None
#        c.b = True
#        assert c.b is True
#        del(c)
#        c = Config(o, session_id='test_persistent', persistent=True)
#        assert c.b is True
#        assert 'test_persistent' in list_sessions('config')
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#        c = Config(o, session_id='test_persistent', persistent=True)
#        assert c.b is None
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#
#
#def test_two_persistent():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        c.cfgimpl_get_settings().remove('cache')
#        c2 = Config(o, session_id='test_persistent', persistent=True)
#        c2.cfgimpl_get_settings().remove('cache')
#        assert c.b is None
#        assert c2.b is None
#        c.b = False
#        assert c.b is False
#        assert c2.b is False
#        c2.b = True
#        assert c.b is True
#        assert c2.b is True
#        delete_session('config', 'test_persistent')
#
#
#def test_create_persistent_retrieve_owner():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert c.getowner(b) == owners.default
#        c.b = True
#        assert c.b is True
#        assert c.getowner(b) == owners.user
#        owners.addowner('persistentowner')
#        c.cfgimpl_get_values().setowner(b, owners.persistentowner)
#        assert c.getowner(b) == owners.persistentowner
#        del(c)
#        #
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.cfgimpl_get_values().setowner(b, owners.persistentowner)
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#        #
#        c = Config(o, session_id='test_persistent', persistent=True)
#        assert c.b is None
#        assert c.getowner(b) == owners.default
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#
#
#def test_create_persistent_retrieve_owner_masterslaves():
#    a = BoolOption('a', '', multi=True)
#    b = BoolOption('b', '', multi=True)
#    o = MasterSlaves('a', '', [a, b])
#    #o.impl_set_group_type(groups.master)
#    o1 = OptionDescription('a', '', [o])
#    try:
#        c = Config(o1, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert c.getowner(a) == owners.default
#        assert c.getowner(b) == owners.default
#        c.a.a = [True]
#        c.a.a.append(False)
#        c.a.b[1] = True
#        assert c.getowner(a) == owners.user
#        assert c.getowner(b, 0) == owners.default
#        assert c.getowner(b, 1) == owners.user
#        owners.addowner('persistentowner2')
#        c.cfgimpl_get_values().setowner(b, owners.persistentowner2, 1)
#        c.a.b[0] = True
#        assert c.getowner(b, 0) == owners.user
#        assert c.getowner(b, 1) == owners.persistentowner2
#        del(c)
#        #
#        c = Config(o1, session_id='test_persistent', persistent=True)
#        assert c.getowner(b, 0) == owners.user
#        assert c.getowner(b, 1) == owners.persistentowner2
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#        #
#        c = Config(o1, session_id='test_persistent', persistent=True)
#        assert c.a.b == []
#        assert c.getowner(b) == owners.default
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#
#
#def test_two_persistent_owner():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.cfgimpl_get_settings().remove('cache')
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        c2 = Config(o, session_id='test_persistent', persistent=True)
#        c2.cfgimpl_get_settings().remove('cache')
#        assert c.getowner(b) == owners.default
#        assert c2.getowner(b) == owners.default
#        c.b = False
#        assert c.getowner(b) == owners.user
#        assert c2.getowner(b) == owners.user
#        owners.addowner('persistent')
#        c.cfgimpl_get_values().setowner(b, owners.persistent)
#        assert c.getowner(b) == owners.persistent
#        assert c2.getowner(b) == owners.persistent
#        delete_session('config', 'test_persistent')
#
#
#def test_create_persistent_retrieve_information():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        c.impl_set_information('info', 'string')
#        assert c.impl_get_information('info') == 'string'
#        del(c)
#        #
#        c = Config(o, session_id='test_persistent', persistent=True)
#        assert c.impl_get_information('info') == 'string'
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#        #
#        c = Config(o, session_id='test_persistent', persistent=True)
#        assert c.impl_get_information('info', None) == None
#        delete_session('config', c.impl_getsessionid())
#        del(c)
#
#
#def test_two_persistent_information():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.cfgimpl_get_settings().remove('cache')
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        c.impl_set_information('info', 'string')
#        assert c.impl_get_information('info') == 'string'
#        c2 = Config(o, session_id='test_persistent', persistent=True)
#        c2.cfgimpl_get_settings().remove('cache')
#        c2.cfgimpl_get_settings().remove('cache')
#        assert c2.impl_get_information('info') == 'string'
#        delete_session('config', 'test_persistent')
#
#
#def test_two_different_persistents():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.cfgimpl_get_settings().remove('cache')
#        d = Config(o, session_id='test_persistent2', persistent=True)
#        d.cfgimpl_get_settings().remove('cache')
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        c.cfgimpl_get_settings()[b].append('test')
#        assert str(c.cfgimpl_get_settings()[b]) in ["['test']", "[u'test']"]
#        assert str(d.cfgimpl_get_settings()[b]) == "[]"
#        assert c.b is None
#        assert d.b is None
#        c.b = True
#        assert c.b == True
#        assert d.b is None
#
#        delete_session('config', 'test_persistent')
#        delete_session('config', 'test_persistent2')
#
#
#def test_two_different_information():
#    b = BoolOption('b', '')
#    o = OptionDescription('od', '', [b])
#    try:
#        c = Config(o, session_id='test_persistent', persistent=True)
#        c.impl_set_information('a', 'a')
#        d = Config(o, session_id='test_persistent2', persistent=True)
#        d.impl_set_information('a', 'b')
#    except ValueError:
#        # storage is not persistent
#        pass
#    else:
#        assert c.impl_get_information('a') == 'a'
#        assert d.impl_get_information('a') == 'b'
#
#        delete_session('config', 'test_persistent')
#        delete_session('config', 'test_persistent2')