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.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

import java.util.HashMap;
import java.util.Map;

/**
 * A ListGrid implementation for displaying and managing Actor records.
 * Provides a user-friendly interface for viewing and interacting with actor data.
 */
public class ActorListGrid 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_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_LAST_UPDATE = "Last Update";

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

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

        // Configure grid fields
        configureFields();

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

    }

    @Override
    protected Canvas getExpansionComponent(final ListGridRecord listGridRecord) {
        final CustomerListGrid odsg = new CustomerListGrid();
        return odsg;
    }

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

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

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


        //IMAGE FIELD
        ListGridField imageField = new ListGridField("image", "Image");
        imageField.setType(ListGridFieldType.IMAGE);
        imageField.setImageHeight(50);
        imageField.setImageWidth(50);
        imageField.setCanEdit(false);
        final Map<String, String> valueIcons = new HashMap<>();
        valueIcons.put("OpeningConstants.QUESTION", "ComponentIcons.GRID_QUESTION_ICON");
        valueIcons.put("OpeningConstants.QUES_ANS", "ComponentIcons.GRID_QUESTION_AND_ANSWER_ICON");
        valueIcons.put("OpeningConstants.ANSWER", "ComponentIcons.GRID_ANSWER_ICON");
        imageField.setValueIcons(valueIcons);
        imageField.setShowValueIconOnly(true);

        // Set the fields on the grid
        setFields(firstNameField, lastNameField, lastUpdateField, imageField);
    }

    /**
     * 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);

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

        // 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, lastUpdateDSField);
    }

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

    /**
     * Gets the value of a field from the selected actor 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 getSelectedActorValue(String fieldName) {
        ListGridRecord record = getSelectedActor();
        return record != null ? record.getAttribute(fieldName) : null;
    }
}
