package org.kumoricon.site.attendee;

import com.vaadin.event.ShortcutAction;
import com.vaadin.navigator.View;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;
import org.kumoricon.BaseGridView;
import org.kumoricon.model.attendee.Attendee;
import org.kumoricon.site.attendee.search.AttendeeSearchPresenter;

import javax.annotation.PostConstruct;
import java.time.format.DateTimeFormatter;

public abstract class CheckInView extends BaseGridView implements View {
    public static final String VIEW_NAME = "order";
    public static final String REQUIRED_RIGHT = "pre_reg_check_in";

    private TextArea attendeeInfo = new TextArea("Attendee");

    private Button btnCheckIn = new Button("Continue");
    private Button btnCancel = new Button("Cancel");
    private CheckBox informationVerified = new CheckBox("Information Verified");
    private CheckBox parentalConsentFormReceived = new CheckBox("Parental Consent Form Received");

    protected Attendee attendee;
    protected Integer orderId;
    protected AttendeeSearchPresenter handler;

    public CheckInView(AttendeeSearchPresenter handler) {
        this.handler = handler;
    }

    @PostConstruct
    public void init() {
        setRows(3);
        setColumns(5);
        setRowExpandRatio(2, 10.0f);
        setColumnExpandRatio(0, 10.0f);
        setColumnExpandRatio(3, 2.0f);
        setColumnExpandRatio(4, 10.0f);

        attendeeInfo.setEnabled(false);
        attendeeInfo.setWidth("500px");
        attendeeInfo.setHeight("300px");

        addComponent(attendeeInfo, 1, 0, 1, 2);
        addComponent(informationVerified, 2, 0);
        addComponent(parentalConsentFormReceived, 2, 1);

        btnCheckIn.addClickListener((Button.ClickListener) clickEvent -> btnCheckInClicked());
        btnCancel.addClickListener(c -> close());

        btnCheckIn.setClickShortcut(ShortcutAction.KeyCode.ENTER);
        btnCheckIn.addStyleName(ValoTheme.BUTTON_PRIMARY);

        btnCheckIn.setEnabled(false);

        addComponent(btnCheckIn, 3, 0);
        addComponent(btnCancel, 3, 1);

        informationVerified.addValueChangeListener(e -> enableButtons());
        parentalConsentFormReceived.addValueChangeListener(e -> enableButtons());
    }

    private void enableButtons() {
        if (informationVerified.getValue()) {
            if (attendee.isMinor()) {
                if (parentalConsentFormReceived.getValue()) {
                    btnCheckIn.setEnabled(true);
                }
            } else {
                btnCheckIn.setEnabled(true);
            }
        } else {
            btnCheckIn.setEnabled(false);
        }
    }

    protected void btnCheckInClicked() {
        handler.checkInAttendee(this, attendee);
        close();
    }

    @Override
    public String getRequiredRight() {
        return REQUIRED_RIGHT;
    }

    public void showAttendee(Attendee attendee) {
        if (!attendee.getCheckedIn()) {
            this.attendee = attendee;
            attendeeInfo.setValue(dumpAttendeeToString(attendee));

            parentalConsentFormReceived.setEnabled(attendee.isMinor());
        } else {
            notify("Error: Attendee already checked in");
            close();
        }
    }


    protected String dumpAttendeeToString(Attendee attendee) {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Name: %s %s\n", attendee.getFirstName(), attendee.getLastName()));
        if (!attendee.getNameIsLegalName()) {
            sb.append(String.format("Legal Name: %s %s\n\n", attendee.getLegalFirstName(), attendee.getLegalLastName()));
        }
        sb.append(String.format("Birthdate: %s (%s years old)\n", attendee.getBirthDate().format(DateTimeFormatter.ofPattern("M/d/Y")), attendee.getAge()));
        sb.append(String.format("Emergency Contact: \n\t%s \n\t%s\n", attendee.getEmergencyContactFullName(), attendee.getEmergencyContactPhone()));
        if (attendee.isMinor()) {
            sb.append(String.format("Parent Contact: \n\t%s \n\t%s\n", attendee.getParentFullName(), attendee.getParentPhone()));
        }

        return sb.toString();
    }

    public boolean parentalConsentFormReceived() {
        return parentalConsentFormReceived.getValue();
    }

    public boolean informationVerified() {
        return informationVerified.getValue();
    }
}

@@ -2454,32 +2454,107 @@
 tEnabled(true);%0A
+                %7D else %7B%0A                    btnCheckIn.setEnabled(false);%0A
                 

Disable checkin button if consent form checkbox is unchecked