"""
Mobile vs. non-mobile.
"""
from dataclasses import dataclass
from datetime import datetime
from typing import ClassVar

from rich.padding import PaddingDimensions
from rich.text import Text

from epstein_files.util.constant.strings import SUBHEADER_STYLE, TIMESTAMP_STYLE
from epstein_files.util.external_link import link_markup
from epstein_files.util.helpers.string_helper import capitalize_first, iso_timestamp, starred_header, timestamp_human

ALL_OTHER_FILES_MULTIPLIER = 1.5  # applied when --all-other-files is in use
DEFAULT_WIDTH = 150
NOT_ALL_FILES_MSG = 'Not All Epstein Files Are Here!'

RICH_LINK_MARKUP = link_markup('https://pypi.org/project/rich/', 'rich')
MOBILE_WARNING = f"This page was generated by {RICH_LINK_MARKUP}. It is very poorly optimized for mobile and may take a while to render on a phone." \
                  "\nIf your browser won't render part of the page try reloading."
MOBILE_WARNING_TXT = Text.from_markup(MOBILE_WARNING)
MOBILE_WARNING_TXT.stylize('dim')


@dataclass(kw_only=True)
class IndentCfg:
    """
    show_with (int): Indent for `show_with_name` files
    """
    email_attachment: int
    email_body: int
    info: int
    other_files_table: int
    show_with: int
    supressed_msg: int


# TODO: another thing that changes based on mobile is id_only for link texts
@dataclass
class MobileConfig:
    abbreviations_width: ClassVar[int | None] = None
    character_bio_padding: ClassVar[tuple[int, int, int, int]] = (0, 0, 1, 2)
    contact_list_padding: ClassVar[PaddingDimensions] = (0, 0, 0, 1)
    max_alt_links: ClassVar[int | None] = 1
    not_all_files_warning: ClassVar[str] = starred_header(NOT_ALL_FILES_MSG, num_spaces=1, num_stars=1)
    num_color_key_cols: ClassVar[int] = 2
    other_files_preview_chars: ClassVar[int] = 300
    section_header_padding: ClassVar[PaddingDimensions] = (2, 1, 1, 1)
    show_emailer_tables: ClassVar[bool] = False
    site_glossary_horizontal_padding: ClassVar[int] = 2
    social_link_separator: ClassVar[str] = ' '
    subtitle_margins: ClassVar[PaddingDimensions] = (2, 1, 1, 1)
    subtitle_padding: ClassVar[PaddingDimensions] = (0, 1)
    subtitle_width: ClassVar[int | None] = None
    width: ClassVar[int] = 52

    indents: ClassVar[IndentCfg] = IndentCfg(
        email_attachment=6,
        email_body=0,
        info=1,
        other_files_table=0,
        show_with=0,
        supressed_msg=0,
    )

    @classmethod
    def format_text_msg_time(cls, dt: datetime) -> str:
        return dt.strftime(r"%-d/%-m/%-y %-H:%M")

    @classmethod
    def info_padding(cls) -> PaddingDimensions:
        return (0, 0, 0, cls.indents.info)

    @classmethod
    def email_subheader(cls, email_type: str, author: Text, recipients: Text, timestamp: datetime | Text) -> Text:
        prefix = f"{capitalize_first(email_type)} from " if email_type != 'email' else ''
        txt = Text(prefix, SUBHEADER_STYLE).append(author)

        if isinstance(timestamp, Text):
            timestamp = timestamp
        else:
            timestamp = Text(' at ').append(timestamp_human(timestamp), TIMESTAMP_STYLE)

        txt.append(' to ').append(recipients).append(timestamp)
        return txt

    @classmethod
    def suppressed_file_padding(cls) -> tuple[int, int, int, int]:
        return (0, 0, 0, cls.indents.supressed_msg)


class SiteConfig(MobileConfig):
    abbreviations_width: ClassVar[int | None] = 62
    character_bio_padding: ClassVar[tuple[int, int, int, int]] = (0, 1, 0, 0)
    contact_list_padding: ClassVar[PaddingDimensions] = (0, 0, 0, 5)
    max_alt_links: ClassVar[int | None] = None
    not_all_files_warning: ClassVar[str] = starred_header(NOT_ALL_FILES_MSG, num_spaces=6, num_stars=14)
    num_color_key_cols: ClassVar[int] = 5
    other_files_preview_chars: ClassVar[int] = 900
    section_header_padding: ClassVar[PaddingDimensions] = (2, 25, 1, 25)
    show_emailer_tables: ClassVar[bool] = True
    site_glossary_horizontal_padding: ClassVar[int] = 5
    social_link_separator: ClassVar[str] = '  /  '
    subtitle_margins: ClassVar[PaddingDimensions] = (2, 0, 2, 0)
    subtitle_padding: ClassVar[PaddingDimensions] = (0, 3)
    subtitle_width: ClassVar[int | None] = 70
    width: ClassVar[int] = DEFAULT_WIDTH

    indents: ClassVar[IndentCfg] = IndentCfg(
        email_attachment=12,
        email_body=1,
        info=1,
        other_files_table=2,
        show_with=30,
        supressed_msg=1,
    )

    @classmethod
    def format_text_msg_time(cls, dt: datetime) -> str:
        return iso_timestamp(dt)

    @classmethod
    def email_subheader(cls, email_type: str, author: Text, recipients: Text, timestamp: datetime) -> Text:
        return super().email_subheader(
            f"OCR text of {email_type}",
            author,
            recipients,
            Text(f" probably sent at ").append(timestamp_human(timestamp), style=TIMESTAMP_STYLE)
        )
