package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.*;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
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 de.assecutor.votianlt.security.SecurityService;
import de.assecutor.votianlt.pages.base.ui.view.MainLayout;
import jakarta.annotation.security.RolesAllowed;

@Route(value = "dashboard", layout = MainLayout.class)
@PageTitle("VotianLT - Dashboard")
@RolesAllowed("USER")
public class AuthenticatedStartView extends VerticalLayout {

    private final SecurityService securityService;

    public AuthenticatedStartView(SecurityService securityService) {
        this.securityService = securityService;
        setSizeFull();
        setPadding(false);
        setSpacing(false);
        setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.STRETCH);

        // Hero Section for authenticated users
        add(createAuthenticatedHeroSection());

        // System Section
        add(createSystemSection());

        // App Section
        add(createAppSection());

        // Footer
        add(createFooter());
    }

    private Component createAuthenticatedHeroSection() {
        VerticalLayout heroSection = new VerticalLayout();
        heroSection.setWidthFull();
        heroSection.setPadding(true);
        heroSection.setSpacing(true);
        heroSection.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        heroSection.getStyle().set("background",
                "linear-gradient(135deg, var(--lumo-primary-color-10pct), var(--lumo-primary-color-50pct))");
        heroSection.getStyle().set("min-height", "300px");
        heroSection.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);

        // Welcome message for authenticated user
        String currentUser = securityService.getCurrentUsername();
        H1 welcomeTitle = new H1("Willkommen zurück, " + currentUser + "!");
        welcomeTitle.getStyle().set("text-align", "center");
        welcomeTitle.getStyle().set("color", "var(--lumo-primary-text-color)");
        welcomeTitle.getStyle().set("margin-bottom", "var(--lumo-space-l)");

        Paragraph welcomeDescription = new Paragraph(
                "Nutzen Sie die Navigation links, um neue Aufträge zu erstellen oder Ihre Verwaltung zu bearbeiten.");
        welcomeDescription.getStyle().set("text-align", "center");
        welcomeDescription.getStyle().set("max-width", "600px");
        welcomeDescription.getStyle().set("font-size", "var(--lumo-font-size-l)");

        heroSection.add(welcomeTitle, welcomeDescription);
        return heroSection;
    }

    private Component createSystemSection() {
        VerticalLayout systemSection = new VerticalLayout();
        systemSection.setWidthFull();
        systemSection.setPadding(true);
        systemSection.setSpacing(true);
        systemSection.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        systemSection.getStyle().set("background-color", "var(--lumo-base-color)");

        // Section Header
        H2 systemTitle = new H2("Das System");
        systemTitle.getStyle().set("color", "var(--lumo-primary-color)");
        systemTitle.getStyle().set("text-align", "center");

        Paragraph systemIntro = new Paragraph(
                "Für Solo-Selbstständige und Kleinunternehmer im Transportgewerbe ist von entscheidender Bedeutung, "
                        + "dass sie sich in erster Linie auf ihr eigentliches Geschäft konzentrieren können: Kunden gewinnen und Waren von A nach B liefern.");
        systemIntro.getStyle().set("text-align", "center");
        systemIntro.getStyle().set("max-width", "800px");
        systemIntro.getStyle().set("margin-bottom", "var(--lumo-space-xl)");

        // Features Grid
        HorizontalLayout featuresGrid = new HorizontalLayout();
        featuresGrid.setWidthFull();
        featuresGrid.setSpacing(true);
        featuresGrid.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.START);

        // Feature Cards
        featuresGrid.add(createFeatureCard(VaadinIcon.COG, "Einrichtungsassistent",
                "Mithilfe des Einrichtungsassistenten haben Sie die Möglichkeit, Ihr Nutzerprofil zu vervollständigen."),
                createFeatureCard(VaadinIcon.USERS, "Kunden- und Auftragsverwaltung",
                        "Mit der Kunden- und Auftragsverwaltung haben Sie alle Kontaktdaten und Auftragsdetails stets im Blick."),
                createFeatureCard(VaadinIcon.CLIPBOARD_TEXT, "Auftragserstellung",
                        "Stellen Sie mit wenigen Mausklicks Aufträge ins System ein und legen Sie fest, welcher Mitarbeiter welchen Transportauftrag abarbeiten soll."));

        systemSection.add(systemTitle, systemIntro, featuresGrid);
        return systemSection;
    }

    private Component createFeatureCard(VaadinIcon iconType, String title, String description) {
        VerticalLayout card = new VerticalLayout();
        card.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        card.setPadding(true);
        card.setSpacing(true);
        card.getStyle().set("background-color", "var(--lumo-base-color)");
        card.getStyle().set("border", "1px solid var(--lumo-contrast-20pct)");
        card.getStyle().set("border-radius", "var(--lumo-border-radius-m)");
        card.getStyle().set("box-shadow", "var(--lumo-box-shadow-s)");
        card.setWidth("300px");

        Icon icon = iconType.create();
        icon.setSize("48px");
        icon.getStyle().set("color", "var(--lumo-primary-color)");
        icon.getStyle().set("margin-bottom", "var(--lumo-space-m)");

        H3 cardTitle = new H3(title);
        cardTitle.getStyle().set("text-align", "center");
        cardTitle.getStyle().set("margin", "0");
        cardTitle.getStyle().set("color", "var(--lumo-primary-text-color)");

        Paragraph cardDescription = new Paragraph(description);
        cardDescription.getStyle().set("text-align", "center");
        cardDescription.getStyle().set("font-size", "var(--lumo-font-size-s)");
        cardDescription.getStyle().set("color", "var(--lumo-secondary-text-color)");
        cardDescription.getStyle().set("margin", "0");

        card.add(icon, cardTitle, cardDescription);
        return card;
    }

    private Component createAppSection() {
        VerticalLayout appSection = new VerticalLayout();
        appSection.setWidthFull();
        appSection.setPadding(true);
        appSection.setSpacing(true);
        appSection.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        appSection.getStyle().set("background-color", "var(--lumo-contrast-5pct)");

        H2 appTitle = new H2("Die App");
        appTitle.getStyle().set("color", "var(--lumo-primary-color)");
        appTitle.getStyle().set("text-align", "center");

        Paragraph appDescription = new Paragraph(
                "Mit unserer mobilen App bleiben Sie auch unterwegs immer über Ihre Aufträge informiert "
                        + "und können wichtige Aufgaben direkt vom Smartphone aus erledigen.");
        appDescription.getStyle().set("text-align", "center");
        appDescription.getStyle().set("max-width", "600px");

        appSection.add(appTitle, appDescription);
        return appSection;
    }

    private Component createFooter() {
        VerticalLayout footer = new VerticalLayout();
        footer.setWidthFull();
        footer.setPadding(true);
        footer.setSpacing(true);
        footer.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        footer.getStyle().set("background-color", "var(--lumo-contrast-10pct)");
        footer.getStyle().set("border-top", "1px solid var(--lumo-contrast-20pct)");

        HorizontalLayout footerContent = new HorizontalLayout();
        footerContent.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.CENTER);
        footerContent.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);

        Paragraph copyright = new Paragraph("© 2024 VotianLT. Alle Rechte vorbehalten.");
        copyright.getStyle().set("color", "var(--lumo-secondary-text-color)");
        copyright.getStyle().set("font-size", "var(--lumo-font-size-s)");
        copyright.getStyle().set("margin", "0");

        footerContent.add(copyright);
        footer.add(footerContent);

        return footer;
    }
}