import pandas as pd

from ydata_profiling.report.presentation.core.duplicate import Duplicate
from ydata_profiling.report.presentation.flavours.html import templates
from ydata_profiling.i18n import _


def to_html(df: pd.DataFrame) -> str:

    # html = df.to_html(
    #     classes="duplicate table table-striped",
    #     justify="center",
    # )

    html = (
        df
        .style
        .set_table_attributes('class="duplicate table table-striped"')
        .set_table_styles([
            {'selector': 'th', 'props': [('text-align', 'center')]},  # 所有 th 居左
            {'selector': 'td', 'props': [('text-align', 'center')]}
        ])
        .to_html()
    )

    if df.empty:
        html = html.replace(
            "<tbody>",
            f"<tbody><tr><td colspan={len(df.columns) + 1}>Dataset does not contain duplicate rows.</td></tr>",
        )
    return html


class HTMLDuplicate(Duplicate):
    def render(self) -> str:
        duplicate_html = to_html(self.content["duplicate"]).replace("# duplicates",_("report.duplicates_key"))
        return templates.template("duplicate.html").render(
            **self.content, duplicate_html=duplicate_html
        )
