# Path: fuzzinator/ui/tui/button.py
# class FormattedButton(WidgetWrap):
#     signals = ['click']
#     _selectable = True
# 
#     def __init__(self, label, on_press=None, user_data=None, style='button'):
#         self.text_len = len(label)
#         self.btn = ShortButton(label, on_press, user_data)
#         super().__init__(AttrMap(self.btn, style))
#         connect_signal(self.btn, 'click', lambda btn: self._emit('click'))
# 
#     def sizing(self):
#         return frozenset(['fixed'])
# 
#     def pack(self, size=None, focus=None):
#         return self.text_len + 6, 1
# 
# Path: fuzzinator/ui/tui/dialogs.py
# class BugEditor(Edit):
# 
#     def keypress(self, size, key):
#         if key == 'ctrl k':
#             lines = self._edit_text.splitlines(keepends=True)
#             line_cnt = self._edit_text[:self.edit_pos].count('\n')
#             before = ''.join(lines[:line_cnt])
#             after = ''.join(lines[line_cnt + 1:]) if line_cnt + 1 < len(lines) else ''
#             self.set_edit_text(before + after)
#             self.set_edit_pos(len(before))
#             return None
#         return super().keypress(size, key)


import logging
from urwid import *
from ...config import config_get_object
from ...formatter import JsonFormatter
from ...tracker import TrackerError
from .button import FormattedButton
from .decor_widgets import PatternBox
from .dialogs import BugEditor
from .graphics import fz_box_pattern


logger = logging.getLogger(__name__)


class ReportDialog(PopUpTarget):
    """
    Base class of dialogs that allow preparing an issue report on the
    Urwid_/text-based UI.

    .. _Urwid: https://github.com/urwid/urwid
    """

    signals = ['close']

    def __init__(self, *, issue, config, db):
        """
        Notes:

          - A new dialog is instantiated for every report.
          - Instead of ``__init__``, the :meth:`init` method must be overridden
            in sub-classes.
        """
        self.issue = issue
        self.db = db

        self.tracker = config_get_object(config, 'sut.' + issue['sut'], 'tracker')
        self.settings = self.tracker.settings()

        formatter = config_get_object(config, 'sut.' + issue['sut'], 'formatter') or JsonFormatter()

# Path: fuzzinator/ui/tui/button.py
# class FormattedButton(WidgetWrap):
#     signals = ['click']
#     _selectable = True
# 
#     def __init__(self, label, on_press=None, user_data=None, style='button'):
#         self.text_len = len(label)
#         self.btn = ShortButton(label, on_press, user_data)
#         super().__init__(AttrMap(self.btn, style))
#         connect_signal(self.btn, 'click', lambda btn: self._emit('click'))
# 
#     def sizing(self):
#         return frozenset(['fixed'])
# 
#     def pack(self, size=None, focus=None):
#         return self.text_len + 6, 1
# 
# Path: fuzzinator/ui/tui/dialogs.py
# class BugEditor(Edit):
# 
#     def keypress(self, size, key):
#         if key == 'ctrl k':
#             lines = self._edit_text.splitlines(keepends=True)
#             line_cnt = self._edit_text[:self.edit_pos].count('\n')
#             before = ''.join(lines[:line_cnt])
#             after = ''.join(lines[line_cnt + 1:]) if line_cnt + 1 < len(lines) else ''
#             self.set_edit_text(before + after)
#             self.set_edit_pos(len(before))
#             return None
#         return super().keypress(size, key)


# Path: fuzzinator/ui/tui/reporter_dialogs.py
import logging
from urwid import *
from ...config import config_get_object
from ...formatter import JsonFormatter
from ...tracker import TrackerError
from .button import FormattedButton
from .decor_widgets import PatternBox
from .dialogs import BugEditor
from .graphics import fz_box_pattern



logger = logging.getLogger(__name__)


class ReportDialog(PopUpTarget):
    """
    Base class of dialogs that allow preparing an issue report on the
    Urwid_/text-based UI.

    .. _Urwid: https://github.com/urwid/urwid
    """

    signals = ['close']

    def __init__(self, *, issue, config, db):
        """
        Notes:

          - A new dialog is instantiated for every report.
          - Instead of ``__init__``, the :meth:`init` method must be overridden
            in sub-classes.
        """
        self.issue = issue
        self.db = db

        self.tracker = config_get_object(config, 'sut.' + issue['sut'], 'tracker')
        self.settings = self.tracker.settings()

        formatter = config_get_object(config, 'sut.' + issue['sut'], 'formatter') or JsonFormatter()

        self.issue_title = BugEditor(edit_text=formatter.summary(issue=issue))