package com.utilinc.planner.gwt.web.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
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 com.smartgwt.client.widgets.layout.HLayout;

/* CSS
 .topBorder{
        border-top: 1px solid black;
    }
    
    .doubleBottomBorder{
        border-bottom: 4px double black;
    }
 */

public class TestModule implements EntryPoint
{
    private static String[][] cellValues = new String[][] { { "Engels, Taylor (7523483661-7523686071-RSIL) ", "" },
            { "", "" }, { "Total Customer Charge", "9.92711467999999" }, { "Total Energy Charge", "30.78482638608" },
            { "Total Demand Charge", "" }, { "Total Bill", "40.71194106608" }, { "if", "" }, { "Customer Count", "1" },
            { "end if", "" }, { "On Peak kW Qty", "" }, { "", "" }, { "Read Date", "20160406" },
            { "Current Month", "4" }, { "Weather Factor", "0.0098" }, { "", "" }, { "", "" }, { "CHIE", "6913" },
            { "PGE", "31900" }, { "Current Jurisdiction", "6913" }, { "if", "" }, { "Total Customers", "426005" },
            { "elseif", "" }, { "Total Customers", "" }, { "end if", "" }, { "Customer To Divide Rate By", "426005" },
            { "Weather Factor (For Posting Only)", "2.30044248306945E-08" }, { "", "" }, { "", "" }, { "", "" },
            { "", "" }, { "if", "" }, { "Active Demand?", "" }, { "end if", "" }, { "IS Rate Compare?", "" },
            { "if", "" }, { "Current RC", "" }, { "Rate Code of Sequence", "" }, { "if", "" }, { "kW Minimun", "" },
            { "Current Period", "" }, { "11 Periods Ago", "" }, { "", "" }, { "Total Demand In Past 12 Months?", "" },
            { "Total Active Bills In Past 12 Months?", "" }, { "", "" }, { "Avg Demand", "" }, { "", "" }, { "if", "" },
            { "Process Rate Compare", "" }, { "end if", "" }, { "else", "" }, { "Home Rate Code", "" },
            { "end if", "" }, { "else", "" }, { "Post", "1" }, { "end if", "" }, { "", "" }, { "if", "" },
            { "kW Price", "" }, { "Penalty For Not Meeting Minimum", "" }, { "if", "" },
            { "Amount To Add To Bill", "" }, { "end if", "" }, { "end if", "" }, { "Customer Group", "" }, { "", "" },
            { "if", "" }, { "Total Customer Charge", "9.92711467999999" }, { "Total Energy Charge", "30.78482638608" },
            { "Total Demand Charge", "" }, { "Facility Related Demand Charge", "" }, { "Total Bill", "40.71194106608" },
            { "CIS Total Bill", "40.71194106608" }, { "", "" }, { "Bill Components", "" },
            { "Customer Charge", "9.92711467999999" }, { "Energy Charge", "30.78482638608" }, { "Demand Charge", "" },
            { "Facility Related Demand Charge", "" }, { "Fuel Charge", "" },
            { "Total Bill w/ Riders & Fuel", "40.71194106608" }, { "Quantities For RD", "" },
            { "Block 1 or Base kWh Qty", "551.304" }, { "Block 2 kWh Qty", "" }, { "Block 3 kWh Qty", "" },
            { "On Peak kWh Qty", "220.5216" }, { "Mid Peak kWh Qty", "" }, { "Off Peak kWh Qty", "330.7824" },
            { "All kWh Qty", "551.304" }, { "On Peak kW Qty", "" }, { "Mid Peak kW Qty", "" },
            { "Off Peak kW Qty", "" }, { "All kW Qty", "" }, { "Block 1 or Base kWh Charge", "30.78482638608" },
            { "Block 2 kWh Charge", "" }, { "Block 3 kWh Charge", "" }, { "On Peak kWh Charge", "" },
            { "Off Peak kWh Charge", "" }, { "On Peak kW Charge", "" }, { "Total kWh Amount", "1102.608" },
            { "Weather Adjustment", "10.8055584" }, { "Bill Start Date", "20160306" }, { "Bill End Date", "20160406" },
            { "Billing Days", "31" } };

    public void onModuleLoad()
    {
        Canvas p = getMainPanel();
        p.draw();
    }

    public Canvas getMainPanel()
    {
        final ListGrid grid = new ListGrid()
        {

            protected String getCellStyle(ListGridRecord record, int rowNum, int colNum)
            {
                String sup = super.getCellStyle(record, rowNum, colNum);
                if (rowNum % 5 == 0)
                {

                    sup += " topBorder";
                    if (colNum > 0)
                    {
                        sup += " doubleBottomBorder";
                    }
                }
                return sup;
            }
        };

        grid.setCanEdit(true);
        grid.setVirtualScrolling(false);
        grid.setWrapCells(true);
        grid.setFixedRecordHeights(false);
        grid.setCanFreezeFields(true);

        ArrayList<ListGridField> fields = new ArrayList<ListGridField>();
        ListGridField frozenField = new ListGridField("frozen1");
        frozenField.setCanEdit(false);
        frozenField.setFrozen(true);
        frozenField.setWidth(100);
        fields.add(frozenField);

        for (int i = 0; i < 20; i++)
        {
            ListGridField f = new ListGridField("unfrozen" + i);
            f.setWidth(100);
            f.setCanEdit(true);
            fields.add(f);
        }

        grid.setFields(fields.toArray(new ListGridField[0]));

        ArrayList<TestRecord> records = new ArrayList<TestRecord>();
        for (String[] row : cellValues)
        {
            TestRecord record = new TestRecord(row);
            records.add(record);
        }
        grid.setRecords(records.toArray(new ListGridRecord[0]));
        grid.setHeight(500);
        grid.setWidth(800);

        HLayout panel = new HLayout();
        panel.setHeight(500);
        panel.setWidth(800);
        panel.addMember(grid);

        return panel;
    }

    private class TestRecord extends ListGridRecord
    {
        public TestRecord(String[] values)
        {
            this.setAttribute("frozen1", values[0]);
            for (int i = 0; i < 20; i++)
                this.setAttribute("unfrozen" + i, values[1]);
        }
    }

}
