# Adapted from
# https://cprohm.de/blog/polars-seaborn/
# 2023-02-21

# Copyright (c) 2023 Christopher Prohm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import functools as ft
from dataclasses import dataclass
from typing import Union

import polars as pl
import seaborn as sns


@pl.api.register_dataframe_namespace("sns")
@pl.api.register_lazyframe_namespace("sns")
@dataclass
class SeabornPlotting:
    df: Union[pl.DataFrame, pl.LazyFrame]

    def pipe(self, func, /, **kwargs):
        def maybe_collect(df):
            return df.collect() if isinstance(df, pl.LazyFrame) else df

        exprs = {}
        for key in "x", "y", "hue", "col", "row":
            val = kwargs.get(key)
            if val is None:
                continue

            expr = pl.col(val) if isinstance(val, str) else val

            exprs[expr.meta.output_name()] = expr
            kwargs[key] = expr.meta.output_name()

        return (
            self.df.select(list(exprs.values()))
            .pipe(maybe_collect)
            .to_pandas()
            .pipe(func, **kwargs)
        )

    barplot = ft.partialmethod(pipe, sns.barplot)
    boxenplot = ft.partialmethod(pipe, sns.boxenplot)
    boxplot = ft.partialmethod(pipe, sns.boxplot)
    catplot = ft.partialmethod(pipe, sns.catplot)
    countplot = ft.partialmethod(pipe, sns.countplot)
    displot = ft.partialmethod(pipe, sns.displot)
    distplot = ft.partialmethod(pipe, sns.distplot)
    ecdfplot = ft.partialmethod(pipe, sns.ecdfplot)
    histplot = ft.partialmethod(pipe, sns.histplot)
    kdeplot = ft.partialmethod(pipe, sns.kdeplot)
    lineplot = ft.partialmethod(pipe, sns.lineplot)
    lmplot = ft.partialmethod(pipe, sns.lmplot)
    pairplot = ft.partialmethod(pipe, sns.pairplot)
    pointplot = ft.partialmethod(pipe, sns.pointplot)
    regplot = ft.partialmethod(pipe, sns.regplot)
    relplot = ft.partialmethod(pipe, sns.relplot)
    residplot = ft.partialmethod(pipe, sns.residplot)
    rugplot = ft.partialmethod(pipe, sns.rugplot)
    scatterplot = ft.partialmethod(pipe, sns.scatterplot)
    stripplot = ft.partialmethod(pipe, sns.stripplot)
    swarmplot = ft.partialmethod(pipe, sns.swarmplot)
    violinplot = ft.partialmethod(pipe, sns.violinplot)
