package com.assaabloy.demoapp.client.view.grid;


import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * A ListGrid implementation for displaying and managing Customer records.
 * Provides a user-friendly interface for viewing and interacting with customer data.
 */
public class CustomerListGrid extends BaseListGrid {

    // Field name constants
    public static final String FIELD_FIRST_NAME = "first_name";
    public static final String FIELD_LAST_NAME = "last_name";
    public static final String FIELD_EMAIL = "email";
    public static final String FIELD_PHONE = "phone";
    public static final String FIELD_ADDRESS = "address";
    public static final String FIELD_LAST_UPDATE = "last_update";

    // Field title constants
    private static final String TITLE_FIRST_NAME = "First Name";
    private static final String TITLE_LAST_NAME = "Last Name";
    private static final String TITLE_EMAIL = "Email";
    private static final String TITLE_PHONE = "Phone";
    private static final String TITLE_ADDRESS = "Address";
    private static final String TITLE_LAST_UPDATE = "Last Update";

    // Column width constants
    private static final int WIDTH_FIRST_NAME = 120;
    private static final int WIDTH_LAST_NAME = 120;
    private static final int WIDTH_EMAIL = 200;
    private static final int WIDTH_PHONE = 120;
    private static final int WIDTH_ADDRESS = 250;
    private static final int WIDTH_LAST_UPDATE = 150;

    /**
     * Creates a new CustomerListGrid with default configuration.
     */
    public CustomerListGrid() {
        super();
        configureGrid();
    }

    /**
     * Configures the grid properties and sets up event handlers.
     */
    private void configureGrid() {
        // Basic grid configuration
        setEmptyCellValue("-");
        setShowEmptyMessage(true);
        setEmptyMessage("No customer records found");
        setCanEdit(true);
        // Configure grid fields
        configureFields();

        // Set up data change handler
        addDataArrivedHandler(event -> markForRedraw());
    }

    /**
     * Configures the grid fields with appropriate properties.
     */
    private void configureFields() {
        // Configure First Name field
        ListGridField firstNameField = createTextField(FIELD_FIRST_NAME, TITLE_FIRST_NAME, WIDTH_FIRST_NAME, true);

        // Configure Last Name field
        ListGridField lastNameField = createTextField(FIELD_LAST_NAME, TITLE_LAST_NAME, WIDTH_LAST_NAME, true);

        // Configure Email field
        ListGridField emailField = createTextField(FIELD_EMAIL, TITLE_EMAIL, WIDTH_EMAIL, false);
        emailField.setCanFilter(true);

        // Configure Phone field
        ListGridField phoneField = createTextField(FIELD_PHONE, TITLE_PHONE, WIDTH_PHONE, false);

        // Configure Address field
        ListGridField addressField = createTextField(FIELD_ADDRESS, TITLE_ADDRESS, WIDTH_ADDRESS, false);

        // Configure Last Update field
        ListGridField lastUpdateField = new ListGridField(FIELD_LAST_UPDATE, TITLE_LAST_UPDATE);
        lastUpdateField.setType(ListGridFieldType.DATE);
        lastUpdateField.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATETIME);
        lastUpdateField.setWidth(WIDTH_LAST_UPDATE);
        lastUpdateField.setAlign(Alignment.CENTER);
        lastUpdateField.setCanEdit(false);
        lastUpdateField.setCanFilter(false);
        lastUpdateField.setShowDefaultContextMenu(false);

        // Set the fields on the grid
        setFields(firstNameField, lastNameField, emailField, phoneField, addressField, lastUpdateField);
    }

    /**
     * Creates a standard text field configuration.
     */
    private ListGridField createTextField(String name, String title, int width, boolean required) {
        ListGridField field = new ListGridField(name, title);
        field.setWidth(width);
        field.setRequired(required);
        field.setCanFilter(true);
        field.setShowDefaultContextMenu(true);
        field.setCanSort(true);
        return field;
    }

    @Override
    public ListGrid setDataSource(DataSource dataSource) {
        if (dataSource != null && (dataSource.getFields() == null || dataSource.getFields().length == 0)) {
            configureDataSourceFields(dataSource);
        }
        return super.setDataSource(dataSource);
    }

    /**
     * Configures the data source fields if they haven't been configured yet.
     */
    private void configureDataSourceFields(DataSource dataSource) {
        // First Name field
        DataSourceTextField firstNameDSField = new DataSourceTextField(FIELD_FIRST_NAME, TITLE_FIRST_NAME);
        firstNameDSField.setPrimaryKey(false);
        firstNameDSField.setRequired(true);
        firstNameDSField.setLength(45);

        // Last Name field
        DataSourceTextField lastNameDSField = new DataSourceTextField(FIELD_LAST_NAME, TITLE_LAST_NAME);
        lastNameDSField.setRequired(true);
        lastNameDSField.setLength(45);

        // Email field
        DataSourceTextField emailDSField = new DataSourceTextField(FIELD_EMAIL, TITLE_EMAIL);
        emailDSField.setLength(50);

        // Phone field
        DataSourceTextField phoneDSField = new DataSourceTextField(FIELD_PHONE, TITLE_PHONE);
        phoneDSField.setLength(20);

        // Address field
        DataSourceTextField addressDSField = new DataSourceTextField(FIELD_ADDRESS, TITLE_ADDRESS);
        addressDSField.setLength(200);

        // Last Update field
        DataSourceDateField lastUpdateDSField = new DataSourceDateField(FIELD_LAST_UPDATE, TITLE_LAST_UPDATE);
        lastUpdateDSField.setRequired(false);

        // Set the fields on the data source
        dataSource.setFields(firstNameDSField, lastNameDSField, emailDSField, phoneDSField, addressDSField, lastUpdateDSField);
    }

    /**
     * Gets the currently selected customer record.
     *
     * @return The selected customer record, or null if no record is selected
     */
    public ListGridRecord getSelectedCustomer() {
        return getSelectedRecord();
    }

    /**
     * Gets the value of a field from the selected customer record.
     *
     * @param fieldName The name of the field to get
     * @return The field value, or null if no record is selected or the field doesn't exist
     */
    public Object getSelectedCustomerValue(String fieldName) {
        ListGridRecord record = getSelectedCustomer();
        return record != null ? record.getAttribute(fieldName) : null;
    }
}
