# piker: trading gear for hackers
# Copyright (C) Tyler Goodlet (in stewardship for piker0)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""
Feed status and controls widget(s) for embedding in a UI-pane.

"""
from __future__ import annotations
from typing import (
    Any,
    TYPE_CHECKING,
)

# from PyQt5.QtCore import Qt

from ._style import _font, _font_small
# from ..calc import humanize
from ._label import FormatLabel

if TYPE_CHECKING:
    from ._chart import ChartPlotWidget
    from ..data.feed import Feed
    from ._forms import FieldsForm


def mk_feed_label(
    form: FieldsForm,
    feed: Feed,
    chart: ChartPlotWidget,

) -> FormatLabel:
    '''
    Generate a label from feed meta-data to be displayed
    in a UI sidepane.

    TODO: eventually buttons for changing settings over
    a feed control protocol.

    '''
    status: dict[str, Any] = feed.status
    assert status

    # SO tips on ws/nls,
    # https://stackoverflow.com/a/15721400
    ws: str = '&nbsp;'
    # nl: str = '<br>'  # dun work?
    actor_info_repr: str = (
        f')> **{status["actor_short_id"]}**\n'
        '\n'  # bc md?
    )

    # fields to select *IN* for display
    # (see `.data.feed.open_feed()` status
    #  update -> TAG_feed_status_update)
    for key in [
        'ipc',
        'hist_shm',
        'rt_shm',
        'throttle_hz',
    ]:
        # NOTE, the 2nd key is filled via `.format()` updates.
        actor_info_repr += (
            f'\n'  # bc md?
            f'{ws}|_{key}: **{{{key}}}**\n'
        )
        # ^TODO? formatting and content..
        # -[ ] showing which fqme is "forward" on the
        #    chart/fsp/order-mode?
        #  '|_ flows: **{symbols}**\n'
        #
        # -[x] why isn't the indent working?
        #  => markdown, now solved..

    feed_label = FormatLabel(
        fmt_str=actor_info_repr,
        font=_font.font,
        font_size=_font_small.px_size,
        font_color='default_lightest',
    )

    # ?TODO, remove this?
    # form.vbox.setAlignment(feed_label, Qt.AlignBottom)
    # form.vbox.setAlignment(Qt.AlignBottom)
    # _ = chart.height() - (
    #     form.height() +
    #     form.fill_bar.height()
    #     # feed_label.height()
    # )

    feed_label.format(**feed.status)
    return feed_label
