package com.bug.widgets;

import com.bug.lib.DateUtil;
import com.smartgwt.client.data.*;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.*;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

import java.lang.String;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class tempDialog extends VLayout {
    protected static String ButtonsIcons = "[SKINIMG]actions/";

    public static String ID = "@id";
    public static String TITLE = "title";
    public static String NAME = "name";
    public static String TYPE = "type";
    public static String DESCRIPTION = "description";
    public static String COUNT = "count";
    public static String BEGINNING = "beginning";
    public static String EXPIRE = "expire";
    public static String CUSTOMER = "customer";

    private Timeline licensesLine;
    private DataSource licensesData;
    private Lane[] licensesLanes;
    private Date currentDate;


    public tempDialog() {
    };

    public void init() {

        currentDate = new Date(118, 01, 01);
//        currentDate = new Date();
        createLicensesData();
    }

    public void createUI() {
        setWidth("100%");
        setHeight("100%");
        setLayoutMargin(5);
        setMembersMargin(5);

        createLicensesLine();
        addMembers(licensesLine);
    }

    private void createLicensesData() {
    	DataSourceTextField idField = new DataSourceTextField(ID);
        idField.setPrimaryKey(true);
        DataSourceTextField customerField = new DataSourceTextField(CUSTOMER);
        DataSourceTextField titleField = new DataSourceTextField(TITLE);
        DataSourceTextField typeField = new DataSourceTextField(TYPE);
        DataSourceTextField descField = new DataSourceTextField(DESCRIPTION);
        DataSourceDateField beginningDateField = new DataSourceDateField(BEGINNING);
        beginningDateField.setFieldValueExtractor(new FieldValueExtractor() {
            public Object execute(final Object record, final Object value, final DataSourceField field, final String fieldName) {
                return DateUtil.parseServerDate((String) value);
            }
        });
        DataSourceDateField expireDateField = new DataSourceDateField(EXPIRE);
        expireDateField.setFieldValueExtractor(new FieldValueExtractor() {
            public Object execute(final Object record, final Object value, final DataSourceField field, final String fieldName) {
                return DateUtil.parseServerDate((String) value);
            }
        });

        licensesData = new DataSource() {
            @Override
            protected void transformResponse(DSResponse response, DSRequest request, Object data) {
                Record[] records = response.getData();
                Map<String, Boolean> types = new HashMap<>();
                for(Record record:records) {
                    types.put(record.getAttributeAsString(TYPE), true);
                }
                licensesLanes = new Lane[]{};
                int i = 0;
                for(String type:types.keySet()) {
                    Lane lane = new Lane();
                    lane.setName(type);
                    Array.set(licensesLanes, i++, lane);
                }
                super.transformResponse(response, request, data);
            }
        };
        licensesData.setID("licensesData");
        licensesData.setFields(idField, customerField, titleField, typeField, descField, beginningDateField, expireDateField);
        licensesData.setClientOnly(true);
        licensesData.setDataFormat(DSDataFormat.JSON);
        licensesData.setUseTestDataFetch(true);

        String url = "api/licenses.json";

        licensesData.setDataURL(url);
        licensesData.fetchData(null, new DSCallback() {
            public void execute(DSResponse response, Object rawData, DSRequest request) {
            	createUI();
                
            }
        });
    }

    private void createLicensesLine() {
        ListGridField typeField = new ListGridField(TYPE, TYPE);
        typeField.setWidth(200);

        HeaderLevel yearLevel = new HeaderLevel(TimeUnit.YEAR);
        HeaderLevel monthLevel = new HeaderLevel(TimeUnit.MONTH);
        HeaderLevel[] headerLevels = new HeaderLevel[]{
                yearLevel,
                monthLevel
        };
        licensesLine = new Timeline();
        licensesLine.setWidth("100%");
        licensesLine.setHeight("100%");
        licensesLine.setHeaderLevels(headerLevels);
        licensesLine.setShowComplexFields(false);
        licensesLine.setShowQuickEventDialog(false);
        licensesLine.setStartDate(currentDate);
        licensesLine.setCanEditLane(true);
        licensesLine.setCanRemoveEvents(false);
        licensesLine.setShowEventDescriptions(false);
        licensesLine.setLaneEventPadding(2);
        licensesLine.setLaneFields(typeField);
        licensesLine.setNameField(TITLE);
        licensesLine.setLaneNameField(TYPE);
        licensesLine.setStartDateField(BEGINNING);
        licensesLine.setEndDateField(EXPIRE);
        licensesLine.setHideUnusedLanes(false);
        licensesLine.setResolution(headerLevels, TimeUnit.YEAR, 2, null);
        licensesLine.setLanes(licensesLanes);
        licensesLine.setData(licensesData.getTestData());

        licensesLine.setDisableDateCustomizer(new DisableDateCustomizer() {
            @Override
            public boolean shouldDisableDate(Date date, CalendarView calendarView) {
                return false;
            }
        });
    }
}
