#
# Copyright (c) Daniel Sheffield 2023
#
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from pytest import mark, fixture
from itertools import chain
from app.activities.grouped_widget_util import (
    to_named_value,
    to_numbered_field,
    to_unnumbered_field,
    in_same_row,
)

@mark.parametrize("idx", chain(range(2), [None, 'any']))
@mark.parametrize("key, value", [
    ('label', 'value'),
    ('label2', 3),
    ('checkbox', True),
    ('checkbox', 'mixed'),
])
def test_to_unnumbered_field(key, idx, value):
    assert (key, value) == to_unnumbered_field((key, idx, value))

@mark.parametrize("key, value, expected", [
    ('label#1', 'value', ('label', 1, 'value')),
    ('label2#0', 3, ('label2', 0, 3)),
    ('checkbox', True, ('checkbox', 0, True)),
    ('checkbox', 'mixed', ('checkbox', 0, 'mixed')),
])
def test_to_numbered_field(key, value, expected):
    assert expected == to_numbered_field((key, value))

@mark.parametrize("key, values, expected", [
    ('label#1', [
        ('label', 1, 'value'),
        ('label2', 0, 3),
        ('checkbox', 0, True),
        ('checkbox', 0, 'mixed'),
        ('label2', 1, 0),
    ], [
        ('label', 1, 'value'),
        ('label2', 1, 0),
    ]),
    ('label', [
        ('label', 1, 'value'),
        ('label2', 0, 3),
        ('checkbox', 0, True),
        ('checkbox', 0, 'mixed'),
        ('label2', 1, 0),
    ], [
        ('label2', 0, 3),
        ('checkbox', 0, True),
        ('checkbox', 0, 'mixed'),
    ]),
])
def test_in_same_row(key, values, expected):
    assert expected == list(filter(in_same_row(key), values))

@mark.parametrize("key, values, expected", [
    ('label2', [
        (1, 'value'),
        (0, 3),
        (0, True),
        (0, 'mixed'),
        (1, 0),
    ], [
        ('label2#1', 'value'),
        ('label2#0', 3),
        ('label2#0', True),
        ('label2#0', 'mixed'),
        ('label2#1', 0)
    ]),
])
def test_to_named_value(key, values, expected):
    assert expected == list(map(to_named_value(key), values))