package ru.vyatsu.qr_access_admin.unit.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll;
import ru.vyatsu.qr_access_admin.common.MainLayout;
import ru.vyatsu.qr_access_admin.partner.entity.PartnerRepository;
import ru.vyatsu.qr_access_admin.unit.component.UnitEditor;
import ru.vyatsu.qr_access_admin.unit.entity.UnitEntity;
import ru.vyatsu.qr_access_admin.unit.entity.UnitRepository;
import ru.vyatsu.qr_access_admin.unit.mapper.UnitEntityUnitModelMapper;
import ru.vyatsu.qr_access_admin.unit.model.UnitModel;

import java.util.List;

@Route(value = "units", layout = MainLayout.class)
@PageTitle("Устройства")
@PermitAll
public class UnitView extends VerticalLayout {

    private final UnitRepository unitRepository;
    private final Grid<UnitModel> grid;
    private final UnitEditor editor;
    private final UnitEntityUnitModelMapper entityModelMapper;

    public UnitView(UnitRepository unitRepository, UnitEntityUnitModelMapper entityModelMapper, PartnerRepository partnerRepository) {
        this.unitRepository = unitRepository;
        this.entityModelMapper = entityModelMapper;

        var addButton = new Button("Добавить устройство", VaadinIcon.PLUS.create());
        grid = new Grid<>(UnitModel.class);
        grid.setColumns("clientId", "clientName", "clientSecretExpiresAt");
        editor = new UnitEditor(partnerRepository);

        var actionsLayout = new HorizontalLayout(addButton);
        add(actionsLayout, grid, editor);

        this.configureEditor();

        addButton.addClickListener(e -> editUnit(new UnitModel()));

        grid.setHeight("200px");
        grid.asSingleSelect().addValueChangeListener(e -> editUnit(e.getValue()));

        refreshUnitsGrid();
    }

    private void refreshUnitsGrid() {
        List<UnitEntity> entities = unitRepository.findAllByAdminEditableIsTrue();

        grid.setItems(entityModelMapper.mapEntityToModel(entities));
    }

    private void editUnit(UnitModel unit) {
        if (unit == null) {
            editor.setVisible(false);
        } else {
            editor.setVisible(true);
            editor.setCurrentUnit(unit);
        }
    }

    private void configureEditor() {
        editor.setVisible(false);

        editor.setSaveListener(unit -> {
            UnitEntity entity = entityModelMapper.mapModelToEntity(unit);
            unitRepository.save(entity);
            refreshUnitsGrid();
            editUnit(null);
        });

        editor.setDeleteListener(unit -> {
            if (unit.isAdminEditable()) {
                unitRepository.deleteById(unit.getId());
                refreshUnitsGrid();
                editUnit(null);
            }
        });

        editor.setCancelListener(() -> editUnit(null));
    }
}
