import datetime
import sys
import shutil

import pytest

from quixote import cleanup, get_request
from wcs.qommon.http_request import HTTPRequest
from wcs import fields

from utilities import create_temporary_pub

def setup_module(module):
    cleanup()

    global pub

    pub = create_temporary_pub()

    req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
    pub._set_request(req)

@pytest.fixture
def user(request):
    pub.user_class.wipe()
    user = pub.user_class(name='user')
    user.id = 'user'
    user.email = 'test@example.net'
    get_request()._user = user
    return user

def teardown_module(module):
    shutil.rmtree(pub.APP_DIR)


def test_prefill_string():
    field = fields.Field()
    field.prefill = {'type': 'string', 'value': 'test'}
    assert field.get_prefill_value() == ('test', False)

def test_prefill_user(user):
    field = fields.Field()
    field.prefill = {'type': 'user', 'value': 'email'}
    assert field.get_prefill_value(user=get_request().user) == ('test@example.net', False)

def test_prefill_user_attribute(user):
    from wcs.admin.settings import UserFieldsFormDef
    formdef = UserFieldsFormDef(pub)
    formdef.fields = [fields.StringField(id='3', label='test', type='string', varname='plop')]
    formdef.store()

    field = fields.Field()
    field.prefill = {'type': 'user', 'value': '3'}
    assert field.get_prefill_value(user=get_request().user) == (None, False)

    user.form_data = {'3': 'Plop'}
    user.store()
    assert field.get_prefill_value(user=get_request().user) == ('Plop', False)

def test_prefill_verified_user_attribute(user):
    from wcs.admin.settings import UserFieldsFormDef
    formdef = UserFieldsFormDef(pub)
    formdef.fields = [fields.StringField(id='3', label='test', type='string', varname='plop')]
    formdef.store()

    field = fields.Field()
    field.prefill = {'type': 'user', 'value': '3'}
    assert field.get_prefill_value(user=get_request().user) == (None, False)

    user.form_data = {'3': 'Plop'}
    user.verified_fields = ['3']
    user.store()
    assert field.get_prefill_value(user=get_request().user) == ('Plop', True)

def test_prefill_formula():
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'str(2+5)'}
    assert field.get_prefill_value() == ('7', False)

def test_prefill_formula_with_error():
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'foobar'}
    assert field.get_prefill_value() == (None, False)

def test_prefill_formula_substitution_variable():
    pub.substitutions.get_context_variables = lambda: {'test': 'value'}
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'test'}
    assert field.get_prefill_value() == ('value', False)

def test_prefill_formula_date_value():
    pub.substitutions.get_context_variables = lambda: {}
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'utils.add_days(utils.today(), 10)'}
    in_ten_days = (datetime.date.today() + datetime.timedelta(days=10)).strftime('%Y-%m-%d')
    assert field.get_prefill_value() == (in_ten_days, False)

    field.prefill = {'type': 'formula', 'value': 'utils.add_days("2016-01-01", 10)'}
    assert field.get_prefill_value() == ('2016-01-11', False)

def test_prefill_formula_today_value():
    pub.substitutions.get_context_variables = pub.get_substitution_variables
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'today'}
    today = datetime.date.today().strftime('%Y-%m-%d')
    assert field.get_prefill_value() == (today, False)

def test_prefill_formula_time_value():
    pub.substitutions.get_context_variables = lambda: {}
    field = fields.Field()
    field.prefill = {'type': 'formula', 'value': 'utils.make_datetime("2018-04-04 10:05").time()'}
    assert field.get_prefill_value() == ('10:05 a.m.', False)
