package de.scai.client.ticket1449.defect;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;
import de.scai.client.Demo;
import de.scai.client.SmartGwtBugReport;
import de.scai.client.ticket1449.defect.model.BugReportDataSource;

import java.util.ArrayList;

public class DemoTicket1449 implements Demo {

    private class UserDataSource extends BugReportDataSource {
        public static final String FIELDNAME_ID = "FIELD_id";
        public static final String FIELDNAME_USERNAME = "FIELD_userName";

        protected DataSourceIntegerField id = new DataSourceIntegerField(FIELDNAME_ID, "Id");
        protected DataSourceTextField userName = new DataSourceTextField(FIELDNAME_USERNAME, "Username");

        private UserDataSource() {
            super();
            id.setPrimaryKey(true);
            addField(id);
            addField(userName);

            initData();
        }

        @Override
        protected void initData() {
            data = new ArrayList<ListGridRecord>();
        }
    }

    private class UserDynamicForm extends DynamicForm {
        protected TextItem userName = new TextItem(UserDataSource.FIELDNAME_USERNAME, "Username");

        protected void init() {

            this.setFields(
                    userName
            );
        }
    }

    private UserDataSource userDataSource;
    private ValuesManager valuesManager;
    private UserDynamicForm userDynamicForm;
    private Button save;

    @Override
    public void run() {

        userDataSource = new UserDataSource();

        valuesManager = new ValuesManager();
        valuesManager.setDataSource(userDataSource);

        initForm();

        Layout layout = new VLayout();
        layout.addMember(userDynamicForm);
        SmartGwtBugReport.layoutRoot.addMember(layout);

        save = new Button("Save");
        save.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                if (valuesManager.validate()) {
                    GWT.log(valuesManager.getChangedValues().toString());
                    if (valuesManager.getChangedValues().size() == 0) {
                        SC.warn("No changed values");
                    }
                    valuesManager.saveData(new DSCallback() {
                        @Override
                        public void execute(DSResponse dsResponse, Object o, DSRequest dsRequest) {
                            GWT.log("dsResponse.result is: " + Integer.toString(dsResponse.getStatus()));
                        }
                    });
                }
            }
        });
        layout.addMember(save);


        userDynamicForm.addItemChangedHandler(new ItemChangedHandler() {
            @Override
            public void onItemChanged(ItemChangedEvent event) {
                GWT.log(valuesManager.getChangedValues().toString());
            }
        });

        valuesManager.addMember(userDynamicForm);
        valuesManager.editNewRecord();
    }

    private void initForm() {
        userDynamicForm = new UserDynamicForm();
        userDynamicForm.init();
        userDynamicForm.setDataSource(userDataSource);
    }
}
