# -*- coding: utf-8 -*-

from utils import login, get_table, get_ods_table, get_ods_document

from bijoe.visualization.ods import OFFICE_NS, TABLE_NS


def test_simple(schema1, app, admin):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    assert 'big-msg-info' in response
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'innersubcategory')
    response = form.submit('visualize')
    assert 'big-msg-info' not in response
    assert get_table(response) == [
        [u'Inner SubCategory', u'subé3', u'subé1'],
        ['number of rows', '1', '15'],
    ]
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'date__month')
    response = form.submit('visualize')
    assert 'big-msg-info' not in response
    assert get_table(response) == [
        ['mois (Date)', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt'],
        ['number of rows', '10', '1', '1', '1', '1', '1', '1', '1'],
    ]


def test_truncated_previous_year_range(schema1, app, admin, freezer):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'date__month')
    form.set('drilldown_y', 'date__year')
    form.set('filter__date_2', 'since_1jan_last_year')
    freezer.move_to('2019-01-01 01:00:00')
    response = form.submit('visualize')
    assert get_table(response) == [
        ['', 'Total'],
        ['Total', '0']
    ]
    freezer.move_to('2018-01-01 01:00:00')
    response = form.submit('visualize')
    assert get_table(response) == [
        ['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
        ['2017', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
        ['Total', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
    ]


def test_boolean_dimension(schema1, app, admin):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'boolean')
    response = form.submit('visualize')
    assert get_table(response) == [['Boolean', 'Non', 'Oui'], ['number of rows', '9', '8']]
    form.set('filter__boolean', [o[0] for o in form.fields['filter__boolean'][0].options if o[2] == 'Oui'][0])
    response = form.submit('visualize')
    assert get_table(response) == [['Boolean', 'Oui'], ['number of rows', '8']]


def test_string_dimension(schema1, app, admin):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'string')
    response = form.submit('visualize')
    assert get_table(response) == [['String', 'a', 'b', 'c', 'Aucun(e)'], ['number of rows', '11', '2', '3', '1']]
    form.set('filter__string', ['a', 'b', '__none__'])
    response = form.submit('visualize')
    assert get_table(response) == [['String', 'a', 'b', 'Aucun(e)'], ['number of rows', '11', '2', '1']]


def test_yearmonth_drilldown(schema1, app, admin):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'date__yearmonth')
    response = form.submit('visualize')
    assert get_table(response) == [
        [u'ann\xe9e et mois (Date)', '01/2017', '02/2017', '03/2017',
         '04/2017', '05/2017', '06/2017', '07/2017', '08/2017'],
        ['number of rows', '10', '1', '1', '1', '1', '1', '1', '1']
    ]


def test_ods(schema1, app, admin):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'innersubcategory')
    response = form.submit('visualize')
    assert 'big-msg-info' not in response
    response = response.click(href='save')
    response.form['name'] = 'test'
    response = response.form.submit().follow()
    ods_response = response.click(href='ods')
    # skip first line of ODS table as it's a header not present in the HTML display
    assert get_table(response) == get_ods_table(ods_response)[1:]
    root = get_ods_document(ods_response)
    nodes = root.findall('.//{%s}table-cell' % TABLE_NS)
    assert len([node for node in nodes if node.attrib['{%s}value-type' % OFFICE_NS] == 'float']) == 2


def test_truncated_previous_year_range_on_datetime(schema1, app, admin, freezer):
    login(app, admin)
    response = app.get('/').follow()
    response = response.click('Facts 1')
    form = response.form
    form.set('representation', 'table')
    form.set('measure', 'simple_count')
    form.set('drilldown_x', 'date__month')
    form.set('drilldown_y', 'date__year')
    form.set('filter__datetime_2', 'since_1jan_last_year')
    freezer.move_to('2019-01-01 01:00:00')
    response = form.submit('visualize')
    assert get_table(response) == [
        ['', 'Total'],
        ['Total', '0']
    ]
    freezer.move_to('2018-01-01 01:00:00')
    response = form.submit('visualize')
    assert get_table(response) == [
        ['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
        ['2017', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
        ['Total', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
    ]
