package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.HasDynamicTitle;
import com.vaadin.flow.router.Route;
import de.assecutor.votianlt.model.PriceTable;
import de.assecutor.votianlt.pages.base.ui.component.ViewToolbar;
import de.assecutor.votianlt.pages.base.ui.view.AdminLayout;
import de.assecutor.votianlt.repository.PriceTableRepository;
import jakarta.annotation.security.RolesAllowed;

@Route(value = "admin-price-table", layout = AdminLayout.class)
@RolesAllowed("ADMIN")
public class AdminPricetableView extends VerticalLayout implements HasDynamicTitle {

    private final PriceTableRepository priceTableRepository;
    private final TextField monthlyBasePackage;
    private final TextField appUserFee;

    public AdminPricetableView(PriceTableRepository priceTableRepository) {
        this.priceTableRepository = priceTableRepository;

        setSpacing(false);
        setPadding(false);
        setWidthFull();
        addClassName("admin-form-view");
        add(new ViewToolbar(getTranslation("adminpricetable.title")));

        VerticalLayout fieldsLayout = new VerticalLayout();
        fieldsLayout.setSpacing(true);
        fieldsLayout.setPadding(false);
        fieldsLayout.addClassNames("form-shell", "form-card");

        monthlyBasePackage = new TextField();
        monthlyBasePackage.setLabel(getTranslation("adminpricetable.field.monthly"));
        monthlyBasePackage.setWidth("40%");
        monthlyBasePackage.setMaxWidth("40%");

        appUserFee = new TextField();
        appUserFee.setLabel(getTranslation("adminpricetable.field.appuserfee"));
        appUserFee.setHelperText(getTranslation("adminpricetable.field.appuserfee.helper"));
        appUserFee.setWidth("40%");
        appUserFee.setMaxWidth("40%");

        Button saveButton = new Button(getTranslation("button.savechanges"));
        saveButton.getStyle().set("margin-top", "20px");
        saveButton.addClickListener(e -> savePriceTable());
        saveButton.addThemeVariants(com.vaadin.flow.component.button.ButtonVariant.LUMO_PRIMARY);

        fieldsLayout.add(monthlyBasePackage, appUserFee, saveButton);

        add(fieldsLayout);

        // Load existing data
        loadPriceTable();
    }

    private void savePriceTable() {
        try {
            // Get first entry or create new one
            PriceTable priceTable = priceTableRepository.findAll().stream().findFirst().orElse(new PriceTable());

            priceTable.setMonthlyBasePackage(monthlyBasePackage.getValue());
            priceTable.setAppUserFee(appUserFee.getValue());

            priceTableRepository.save(priceTable);
            Notification.show(getTranslation("adminpricetable.notification.saved"), 3000,
                    Notification.Position.BOTTOM_CENTER);
        } catch (Exception ex) {
            Notification.show(getTranslation("adminpricetable.notification.save.error", ex.getMessage()), 5000,
                    Notification.Position.BOTTOM_CENTER);
        }
    }

    private void loadPriceTable() {
        try {
            PriceTable priceTable = priceTableRepository.findAll().stream().findFirst().orElse(null);

            if (priceTable != null) {
                monthlyBasePackage
                        .setValue(priceTable.getMonthlyBasePackage() != null ? priceTable.getMonthlyBasePackage() : "");
                appUserFee.setValue(priceTable.getAppUserFee() != null ? priceTable.getAppUserFee() : "");
            }
        } catch (Exception ex) {
            Notification.show(getTranslation("adminpricetable.notification.load.error", ex.getMessage()), 5000,
                    Notification.Position.BOTTOM_CENTER);
        }
    }

    @Override
    public String getPageTitle() {
        return getTranslation("page.title.pricetable");
    }
}
