import os

import ipywidgets as widgets
from IPython.display import display
from ipywidgets import VBox


def select_data_folders(path):
    attributes = []
    for name in os.listdir(path):
        if "_checkpoints" in name or name.startswith('.') or not os.path.isdir(os.path.join(path, name)):
            continue

        attributes.append(os.path.join(path, name))
    return attributes


class PathSelectorWidget(VBox):
    def __init__(self, data_path):
        style = {"description_width": "initial"}

        attr_list = select_data_folders(data_path)
        dataset_list = []
        for item in attr_list:
            dataset_list.append(item.split("/")[-1])  # TODO: use pathlib
        dataset_list = sorted(dataset_list)
        self.select_data_widget = widgets.Select(
            options=dataset_list,
            description="Choose dataset:",
            disabled=False,
            style=style,
        )

        self.info_widget_save = widgets.Text(
            value=self.select_data_widget.value,
            placeholder="",
            description="Save path is:",
            disabled=False,
            style=style,
        )

        self.select_data_widget.observe(self.on_select_change)

        display(self.info_widget_save, self.select_data_widget)

    def on_select_change(self, change):
        try:
            self.info_widget_save.value = change.owner.options[change.new["index"]]
        except:
            pass
