#
# Copyright (c) Daniel Sheffield 2023
#
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from typing import Dict, List, Tuple
from pytest import fixture, mark
from app.parse_recipe import parse_recipe
from app.data.QueryManager import QueryManager
from app.rest import ALL_UNITS

from app.widgets import (
    SuggestionPopup,
    FlowBarGraphWithVScale,
    NoTabCheckBox,
)

@fixture
def signals():
    return []

@fixture
def signal(signals):
    return lambda x, *a: signals.append(x)

@fixture
def tracker():
    return dict()

@fixture
def options():
    return ['a', 'b', 'c']

@fixture
def suggestion_popup(tracker, options, signal):
    popup = SuggestionPopup('identifier', options, lambda x, y: tracker.update({x: y}))
    popup._emit = signal
    return popup

@mark.parametrize('pos', [0, 1, 2])
def test_suggestion_popup_choice(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], pos: int):
    while pos > 0:
        pos -= 1
        suggestion_popup.keypress((0,0), 'tab')
    assert len(tracker.keys()) == 0
    suggestion_popup.keypress((0,0), 'enter')
    assert tracker['identifier'] == options[pos]
    
def test_suggestion_popup_banish(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], signals):
    suggestion_popup.keypress((0,0), 'esc')
    assert len(tracker.keys()) == 0
    assert 'close' in signals

@fixture
def bargraph():
    return FlowBarGraphWithVScale(
        50, 14,
        ['bg','popup_focus', 'badge_neutral' ],
        hatt=['dark red', 'dark red', 'dark red']
    )

@mark.parametrize('vscale', [
    list(map(float, (1, 2, 3))),
    None,
])
def test_flow_bar_graph_with_vscale(bargraph: FlowBarGraphWithVScale, vscale: Tuple[float]):
    bargraph.set_data([[1],[2],[3]], 3, vscale=vscale)
    bargraph.set_caption('my data')
    assert bargraph.total_width == 50
    assert bargraph.canvas_width == 50 - bargraph._vscale_width
    assert bargraph.height == 14
    bargraph.set_bar_width(2)
    bargraph.render((100,))

@fixture
def notabcheckbox():
    return NoTabCheckBox('label')

def test_no_tab_checkbox(notabcheckbox: NoTabCheckBox):
    state = notabcheckbox.get_state()
    notabcheckbox.keypress((0,0), 'tab')
    assert notabcheckbox.get_state() == state
    notabcheckbox.keypress((0,0), 'enter')
    assert notabcheckbox.get_state() != state