package FieldTypeOperators.client;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import FieldTypeOperators.shared.FieldTypeOperatorsDTO;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.layout.VLayout;

public class FieldTypeOperators implements EntryPoint {

    @Override
    public void onModuleLoad() {

        // fields
        final Record fieldAsTEXT = new Record();
        fieldAsTEXT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_NAME, FieldTypeOperatorsDTO.VALUE_TEXT);
        fieldAsTEXT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TITLE, FieldTypeOperatorsDTO.VALUE_TEXT);
        fieldAsTEXT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TYPE, FieldType.TEXT.getValue());
        fieldAsTEXT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_LENGTH, FieldTypeOperatorsDTO.DEFAULT_FIELD_LENGTH);
        final Record fieldAsDATE = new Record();
        fieldAsDATE.setAttribute(FieldTypeOperatorsDTO.PROPERTY_NAME, FieldTypeOperatorsDTO.VALUE_DATE);
        fieldAsDATE.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TITLE, FieldTypeOperatorsDTO.VALUE_DATE);
        fieldAsDATE.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TYPE, FieldType.DATE.getValue());
        fieldAsDATE.setAttribute(FieldTypeOperatorsDTO.PROPERTY_LENGTH, FieldTypeOperatorsDTO.DEFAULT_FIELD_LENGTH);
        final Record fieldAsINTEGER = new Record();
        fieldAsINTEGER.setAttribute(FieldTypeOperatorsDTO.PROPERTY_NAME, FieldTypeOperatorsDTO.VALUE_INTEGER);
        fieldAsINTEGER.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TITLE, FieldTypeOperatorsDTO.VALUE_INTEGER);
        fieldAsINTEGER.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TYPE, FieldType.INTEGER.getValue());
        fieldAsINTEGER.setAttribute(FieldTypeOperatorsDTO.PROPERTY_LENGTH, FieldTypeOperatorsDTO.DEFAULT_FIELD_LENGTH);
        final Record fieldAsFLOAT = new Record();
        fieldAsFLOAT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_NAME, FieldTypeOperatorsDTO.VALUE_FLOAT);
        fieldAsFLOAT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TITLE, FieldTypeOperatorsDTO.VALUE_FLOAT);
        fieldAsFLOAT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TYPE, FieldType.FLOAT.getValue());
        fieldAsFLOAT.setAttribute(FieldTypeOperatorsDTO.PROPERTY_LENGTH, FieldTypeOperatorsDTO.DEFAULT_FIELD_LENGTH);
        final Record fieldAsBOOLEAN = new Record();
        fieldAsBOOLEAN.setAttribute(FieldTypeOperatorsDTO.PROPERTY_NAME, FieldTypeOperatorsDTO.VALUE_BOOLEAN);
        fieldAsBOOLEAN.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TITLE, FieldTypeOperatorsDTO.VALUE_BOOLEAN);
        fieldAsBOOLEAN.setAttribute(FieldTypeOperatorsDTO.PROPERTY_TYPE, FieldType.BOOLEAN.getValue());
        fieldAsBOOLEAN.setAttribute(FieldTypeOperatorsDTO.PROPERTY_LENGTH, FieldTypeOperatorsDTO.DEFAULT_FIELD_LENGTH);

        final Record[] fieldList =
            new Record[] { fieldAsTEXT, fieldAsDATE, fieldAsINTEGER, fieldAsFLOAT, fieldAsBOOLEAN };

        // operators
        final List<OperatorId> operatorsOfTEXT = //
            Arrays.asList(OperatorId.IS_NULL, //
                          OperatorId.NOT_NULL, //
                          OperatorId.EQUALS, //
                          OperatorId.CONTAINS, //
                          OperatorId.STARTS_WITH, //
                          OperatorId.ENDS_WITH, //
                          OperatorId.NOT_EQUAL, //
                          OperatorId.NOT_CONTAINS, //
                          OperatorId.NOT_STARTS_WITH, //
                          OperatorId.NOT_ENDS_WITH, //
                          OperatorId.IEQUALS, //
                          OperatorId.ICONTAINS, //
                          OperatorId.ISTARTS_WITH, //
                          OperatorId.IENDS_WITH, //
                          OperatorId.INOT_EQUAL, //
                          OperatorId.INOT_CONTAINS, //
                          OperatorId.INOT_STARTS_WITH, //
                          OperatorId.INOT_ENDS_WITH, //
                          OperatorId.IBETWEEN, //
                          OperatorId.IBETWEEN_INCLUSIVE, //
                          OperatorId.MATCHES_PATTERN, //
                          OperatorId.STARTS_WITH_PATTERN, //
                          OperatorId.ENDS_WITH_PATTERN, //
                          OperatorId.CONTAINS_PATTERN, //
                          OperatorId.IMATCHES_PATTERN, //
                          OperatorId.ISTARTS_WITH_PATTERN, //
                          OperatorId.IENDS_WITH_PATTERN, //
                          OperatorId.ICONTAINS_PATTERN, //
                          OperatorId.REGEXP, //
                          OperatorId.IREGEXP, //
                          OperatorId.IN_SET, //
                          OperatorId.NOT_IN_SET //
            );
        final OperatorId[] operatorIdsOfTEXT = operatorsOfTEXT.toArray(new OperatorId[operatorsOfTEXT.size()]);

        final List<OperatorId> operatorsOfDATE = //
            Arrays.asList( //
                          OperatorId.IS_NULL, //
                          OperatorId.NOT_NULL, //
                          OperatorId.GREATER_THAN, //
                          OperatorId.LESS_THAN, //
                          OperatorId.GREATER_OR_EQUAL, //
                          OperatorId.LESS_OR_EQUAL, //
                          OperatorId.EQUALS, //
                          OperatorId.NOT_EQUAL, //
                          OperatorId.BETWEEN, //
                          OperatorId.BETWEEN_INCLUSIVE, //
                          OperatorId.IN_SET, //
                          OperatorId.NOT_IN_SET //
            );
        final OperatorId[] operatorIdsOfDATE = operatorsOfDATE.toArray(new OperatorId[operatorsOfDATE.size()]);

        final List<OperatorId> operatorsOfINTEGER = //
            Arrays.asList( //
                          OperatorId.IS_NULL, //
                          OperatorId.NOT_NULL, //
                          OperatorId.GREATER_THAN, //
                          OperatorId.LESS_THAN, //
                          OperatorId.GREATER_OR_EQUAL, //
                          OperatorId.LESS_OR_EQUAL, //
                          OperatorId.EQUALS, //
                          OperatorId.BETWEEN, //
                          OperatorId.BETWEEN_INCLUSIVE, //
                          OperatorId.IN_SET, //
                          OperatorId.NOT_IN_SET //
            );
        final OperatorId[] operatorIdsOfINTEGER = operatorsOfINTEGER.toArray(new OperatorId[operatorsOfINTEGER.size()]);

        final List<OperatorId> operatorsOfFLOAT = //
            Arrays.asList( //
                          OperatorId.IS_NULL, //
                          OperatorId.NOT_NULL, //
                          OperatorId.GREATER_THAN, //
                          OperatorId.LESS_THAN, //
                          OperatorId.GREATER_OR_EQUAL, //
                          OperatorId.LESS_OR_EQUAL, //
                          OperatorId.EQUALS, //
                          OperatorId.BETWEEN, //
                          OperatorId.BETWEEN_INCLUSIVE, //
                          OperatorId.IN_SET, //
                          OperatorId.NOT_IN_SET //
            );
        final OperatorId[] operatorIdsOfFLOAT = operatorsOfFLOAT.toArray(new OperatorId[operatorsOfFLOAT.size()]);

        final List<OperatorId> operatorsOfBOOLEAN = //
            Arrays.asList( //
                          OperatorId.IS_NULL, //
                          OperatorId.NOT_NULL, //
                          OperatorId.EQUALS //
            );
        final OperatorId[] operatorIdsOfBOOLEAN = operatorsOfBOOLEAN.toArray(new OperatorId[operatorsOfBOOLEAN.size()]);

        // data source

        // final DataSource defaultDataSource =
        // DataSource.get("fieldTypeOperatorsDefaultDS");
        final DataSource defaultDataSource = new DataSource();
        defaultDataSource.setClientOnly(true);
        defaultDataSource.setCacheData(fieldList);

        // final DataSource problemDataSource =
        // DataSource.get("fieldTypeOperatorsProblemDS");
        final DataSource problemDataSource = new DataSource();
        problemDataSource.setClientOnly(true);
        problemDataSource.setCacheData(fieldList);
        problemDataSource.setTypeOperators(FieldType.TEXT, operatorIdsOfTEXT);
        problemDataSource.setTypeOperators(FieldType.DATE, operatorIdsOfDATE);
        problemDataSource.setTypeOperators(FieldType.INTEGER, operatorIdsOfINTEGER);
        problemDataSource.setTypeOperators(FieldType.FLOAT, operatorIdsOfFLOAT);
        problemDataSource.setTypeOperators(FieldType.BOOLEAN, operatorIdsOfBOOLEAN);

        // default case

        final FilterBuilder defaultFilterBuilder = new FilterBuilder();
        defaultFilterBuilder.setHeight("100");
        defaultFilterBuilder.setWidth("100");
        defaultFilterBuilder.setFieldDataSource(defaultDataSource);
        final Label defaultLabel = new Label("Default");

        // problem case

        final FilterBuilder problemFilterBuilder = new FilterBuilder();
        problemFilterBuilder.setHeight("100");
        problemFilterBuilder.setWidth("100");
        problemFilterBuilder.setFieldDataSource(problemDataSource);

        final Label problemLabel = new Label("Problem");

        // layout

        final VLayout layout = new VLayout();
        layout.setWidth("100%");
        layout.setHeight("100%");
        layout.addMember(defaultLabel);
        layout.addMember(defaultFilterBuilder);
        layout.addMember(problemLabel);
        layout.addMember(problemFilterBuilder);

        // render

        layout.draw();
    }
}
