package com.gwtbug.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * Minimal ListGrid modeled on TITAN {@code OpeningsGrid}: same text columns and
 * similar sizing/scroll settings, with dummy records created in the browser.
 */
public class OpeningsSampleGrid extends ListGrid {

    private static final int DUMMY_ROW_COUNT = 80;

    private static final Object[][] COLUMNS = {
            {"needsAttention", "Needs Attention", 100},
            {"errorLevel", "Error Level", 90},
            {"needBy", "Need By", 100},
            {"mark", "Mark", 100},
            {"assignedHeading", "Assigned Heading", 120},
            {"unitAbbr", "Unit Abbr", 100},
            {"unitOpeningDescription", "Unit Opening Description", 160},
            {"bankName", "Bank Name", 110},
            {"qty", "Qty", 60},
            {"exterior", "Exterior", 90},
            {"openingGroupId", "Group", 80},
            {"uom", "Style", 80},
            {"hand", "Hand", 80},
            {"width", "Width", 80},
            {"height", "Height", 80},
            {"jambDepth", "Jamb Depth", 90},
            {"degrees", "Degrees", 100},
            {"undercut", "Undercut", 100},
            {"frameListCost", "Frame List Cost", 120},
            {"frameSpecialNet", "Special Net", 100},
            {"frameSpecialNetCost", "Special Net Cost", 120},
            {"frameCost", "Frame Cost", 100},
            {"frameWeldListCost", "Frame Weld List Cost", 140},
            {"frameWeldCost", "Frame Weld Cost", 120},
            {"frameShopWeldCost", "Frame Shop Weld Cost", 150},
            {"frameTotalCost", "Frame Total Cost", 130},
            {"openingDoorListCosts", "Doors List Price", 120},
            {"openingDoorCosts", "Doors Cost", 100},
            {"openingListCost", "Material List Price", 130},
            {"openingCost", "Material Cost", 110},
            {"openingFrameMaterialCode", "Frame Material", 120},
            {"frameId", "Frame Name", 150},
            {"doorMaterialCode", "Door Materials", 120},
            {"openingDoors", "Door Names", 150},
            {"ansiFunctionId", "ANSI Function", 120},
            {"hardwareSetId", "Hardware Set", 120},
            {"doorQty", "Door Qty", 80},
            {"preHung", "Pre-Hung", 90},
            {"existingFrame", "Existing Frame", 110},
            {"frameByOthers", "Frame By Others", 120},
            {"frameByOthersSupplier", "Frame By Others Supplier", 160},
            {"existingDoor", "Existing Door", 110},
            {"doorByOthers", "Door By Others", 120},
            {"doorByOthersSupplier", "Door By Others Supplier", 160},
            {"locations", "Manufacturer Locations", 150},
            {"anchorQtyValue", "Anchor Qty", 90},
            {"addOnQtyValue", "Add-On Qty", 90},
            {"prepQtyValue", "Prep Qty", 80},
            {"extendedFrameCost", "Extended Frame Cost", 140},
            {"extendedDoorCost", "Extended Door Cost", 140},
            {"building", "Building", 100},
            {"level", "Level", 80},
            {"location1", "Location 1", 110},
            {"fromTo", "From / To", 100},
            {"location2", "Location 2", 110},
            {"frameType", "Frame Type", 120},
            {"doorType", "Door Type", 120},
            {"architectsMark", "Architects Mark", 120},
            {"architectsLabel", "Architects Label", 120},
            {"architectsJamb", "Architects Jamb", 120},
            {"architectsHead", "Architects Head", 120},
            {"architectsSill", "Architects Sill", 120},
            {"fireRating", "Fire Rating", 90},
            {"temperatureRiseRating", "Temperature Rating", 130},
            {"smokeDraft", "Smoke Draft", 100},
            {"stcRating", "STC Rating", 90},
            {"windRating", "Wind Rating", 90},
            {"blastPeakPsi", "Blast Peak PSI", 110},
            {"blastRating", "Blast Rating", 100},
            {"bulletResistanceRating", "Bullet Resistance", 130},
            {"labelId", "Label", 90},
            {"wallTypeInfo", "Wall Type Info", 120},
            {"wallId", "Wall Type", 100},
            {"userDefined1", "User Defined 1", 130},
            {"userDefined2", "User Defined 2", 130},
            {"userDefined3", "User Defined 3", 130},
            {"userDefined4", "User Defined 4", 130},
            {"userDefined5", "User Defined 5", 130},
            {"wallThickness", "Wall Thickness", 110},
            {"description", "Description", 320},
            {"notes", "Notes", 320},
            {"changeDescription", "Change Description", 320}
    };

    public OpeningsSampleGrid() {
        setID("openingsSampleGrid");
        setWidth100();
        setHeight100();
        setMinWidth(120);
        setMinHeight(92);
        setCellHeight(20);
        setHeaderHeight(45);
        setCellPadding(5);
        setAlign(Alignment.LEFT);

        setShowFilterEditor(true);
        setFilterEditorHeight(24);
        setFilterOnKeypress(true);
        setCanEdit(true);
        setEditEvent(ListGridEditEvent.CLICK);
        setListEndEditAction(RowEndEditAction.NEXT);
        setSaveByCell(false);
        setAutoSaveEdits(true);
        setConfirmDiscardEdits(false);
        setCanSelectAll(true);
        setCanAutoFitFields(false);
        setAutoFitFieldWidths(false);
        setAutoFitWidthApproach(AutoFitWidthApproach.TITLE);
        setFixedRecordHeights(true);
        setShowAllRecords(false);
        setVirtualScrolling(false);
        setLeaveScrollbarGap(false);
        setEnforceVClipping(true);
        setRedrawOnResize(false);
        setShowRecordComponents(false);
        setShowRollOver(false);
        setFastCellUpdates(true);

        setFields(createFields());
        setData(createDummyRecords());
    }

    private ListGridField[] createFields() {
        final ListGridField[] fields = new ListGridField[COLUMNS.length];
        for (int i = 0; i < COLUMNS.length; i++) {
            final String name = (String) COLUMNS[i][0];
            final String title = (String) COLUMNS[i][1];
            final int width = (Integer) COLUMNS[i][2];
            final ListGridField field = new ListGridField(name, title, width);
            field.setType(ListGridFieldType.TEXT);
            field.setCanEdit(true);
            field.setCanFilter(true);
            field.setWrap(false);
            field.setEditorType(new TextItem());
            fields[i] = field;
        }
        return fields;
    }

    private ListGridRecord[] createDummyRecords() {
        final ListGridRecord[] records = new ListGridRecord[DUMMY_ROW_COUNT];
        for (int i = 0; i < DUMMY_ROW_COUNT; i++) {
            final int n = i + 1;
            final ListGridRecord record = new ListGridRecord();
            record.setAttribute("needsAttention", n % 7 == 0 ? "Yes" : "No");
            record.setAttribute("errorLevel", n % 11 == 0 ? "WARNING" : n % 17 == 0 ? "CRITICAL" : "");
            record.setAttribute("needBy", "2026-09-" + twoDigits((n % 28) + 1));
            record.setAttribute("mark", "OP-" + n);
            record.setAttribute("assignedHeading", "HDG-" + (n % 12 + 1));
            record.setAttribute("unitAbbr", "U" + (n % 5 + 1));
            record.setAttribute("unitOpeningDescription", "Unit opening " + n);
            record.setAttribute("bankName", n % 4 == 0 ? "Bank A" : "");
            record.setAttribute("qty", String.valueOf((n % 3) + 1));
            record.setAttribute("exterior", n % 2 == 0 ? "Yes" : "No");
            record.setAttribute("openingGroupId", "GRP-" + (n % 8 + 1));
            record.setAttribute("uom", n % 3 == 0 ? "SGL" : n % 3 == 1 ? "PR" : "Cased");
            record.setAttribute("hand", n % 2 == 0 ? "RH" : "LH");
            record.setAttribute("width", "3'-0\"");
            record.setAttribute("height", "7'-0\"");
            record.setAttribute("jambDepth", "5-3/4\"");
            record.setAttribute("degrees", "90");
            record.setAttribute("undercut", "3/4\"");
            record.setAttribute("frameListCost", money(420 + n));
            record.setAttribute("frameSpecialNet", n % 9 == 0 ? "Yes" : "No");
            record.setAttribute("frameSpecialNetCost", money(15 + n));
            record.setAttribute("frameCost", money(380 + n));
            record.setAttribute("frameWeldListCost", money(55 + n));
            record.setAttribute("frameWeldCost", money(48 + n));
            record.setAttribute("frameShopWeldCost", money(62 + n));
            record.setAttribute("frameTotalCost", money(500 + n));
            record.setAttribute("openingDoorListCosts", money(610 + n));
            record.setAttribute("openingDoorCosts", money(540 + n));
            record.setAttribute("openingListCost", money(1100 + n));
            record.setAttribute("openingCost", money(980 + n));
            record.setAttribute("openingFrameMaterialCode", "HM");
            record.setAttribute("frameId", "F-16GA-" + (n % 20 + 1));
            record.setAttribute("doorMaterialCode", "WD");
            record.setAttribute("openingDoors", "Door " + n);
            record.setAttribute("ansiFunctionId", "F" + (n % 10 + 1));
            record.setAttribute("hardwareSetId", "HS-" + (n % 15 + 1));
            record.setAttribute("doorQty", n % 3 == 1 ? "2" : "1");
            record.setAttribute("preHung", n % 6 == 0 ? "Yes" : "No");
            record.setAttribute("existingFrame", n % 8 == 0 ? "Yes" : "No");
            record.setAttribute("frameByOthers", "No");
            record.setAttribute("frameByOthersSupplier", n % 10 == 0 ? "Acme Frames" : "");
            record.setAttribute("existingDoor", "No");
            record.setAttribute("doorByOthers", "No");
            record.setAttribute("doorByOthersSupplier", n % 10 == 0 ? "Acme Doors" : "");
            record.setAttribute("locations", "Plant " + (n % 4 + 1));
            record.setAttribute("anchorQtyValue", String.valueOf(n % 4));
            record.setAttribute("addOnQtyValue", String.valueOf(n % 3));
            record.setAttribute("prepQtyValue", String.valueOf(n % 5));
            record.setAttribute("extendedFrameCost", money(500 * ((n % 3) + 1)));
            record.setAttribute("extendedDoorCost", money(540 * ((n % 3) + 1)));
            record.setAttribute("building", "Building " + (char) ('A' + n % 4));
            record.setAttribute("level", "L" + (n % 6 + 1));
            record.setAttribute("location1", "Corridor " + n);
            record.setAttribute("fromTo", n % 2 == 0 ? "To" : "From");
            record.setAttribute("location2", "Room " + (100 + n));
            record.setAttribute("frameType", "Masonry");
            record.setAttribute("doorType", "Flush");
            record.setAttribute("architectsMark", "A-" + n);
            record.setAttribute("architectsLabel", "Label " + n);
            record.setAttribute("architectsJamb", "Jamb " + n);
            record.setAttribute("architectsHead", "Head " + n);
            record.setAttribute("architectsSill", "Sill " + n);
            record.setAttribute("fireRating", n % 5 == 0 ? "90 min" : "20 min");
            record.setAttribute("temperatureRiseRating", "250 F");
            record.setAttribute("smokeDraft", n % 4 == 0 ? "Yes" : "No");
            record.setAttribute("stcRating", "STC-35");
            record.setAttribute("windRating", "DP-50");
            record.setAttribute("blastPeakPsi", "8.0");
            record.setAttribute("blastRating", "GSA");
            record.setAttribute("bulletResistanceRating", "Level 3");
            record.setAttribute("labelId", "UL");
            record.setAttribute("wallTypeInfo", "Gyp");
            record.setAttribute("wallId", "WT-" + (n % 6 + 1));
            record.setAttribute("userDefined1", "UD1-" + n);
            record.setAttribute("userDefined2", "UD2-" + n);
            record.setAttribute("userDefined3", "UD3-" + n);
            record.setAttribute("userDefined4", "UD4-" + n);
            record.setAttribute("userDefined5", "UD5-" + n);
            record.setAttribute("wallThickness", "4-7/8\"");
            record.setAttribute("description", "Sample opening " + n + " for scrollbar reproduction");
            record.setAttribute("notes", "Dummy note for opening OP-" + n);
            record.setAttribute("changeDescription", n % 5 == 0 ? "Added opening in CO-" + n : "");
            records[i] = record;
        }
        return records;
    }

    private static String twoDigits(final int value) {
        return value < 10 ? "0" + value : String.valueOf(value);
    }

    private static String money(final int value) {
        return value + ".00";
    }
}
