package de.scai.client.ticket1445.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.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.ticket1445.defect.model.BugReportDataSource;

import java.util.ArrayList;

public class DemoTicket1445Defect implements Demo {

    private class UserDataSource extends BugReportDataSource {
        public static final String FIELDNAME_ID = "FIELD_id";
        public static final String FIELDNAME_USERNAME = "FIELD_userName";
        public static final String FIELDNAME_GROUPID = "FIELD_groupId";
        public static final String FIELDNAME_ROLEID = "FIELD_roleId";
        public static final String FIELDNAME_CREATEDONID = "FIELD_createdOn";
        public static final String FIELDNAME_COMMENT = "FIELD_comment";
        public static final String FIELDNAME_AGE = "FIELD_age";
        public static final String FIELDNAME_WAGE = "FIELD_wage";

        protected DataSourceIntegerField id = new DataSourceIntegerField(FIELDNAME_ID, "Id");
        protected DataSourceTextField userName = new DataSourceTextField(FIELDNAME_USERNAME, "Username");
        protected DataSourceIntegerField groupId = new DataSourceIntegerField(FIELDNAME_GROUPID, "Group");
        protected DataSourceIntegerField roleId = new DataSourceIntegerField(FIELDNAME_ROLEID, "Role");
        protected DataSourceDateField createdOn = new DataSourceDateField(FIELDNAME_CREATEDONID, "Created On");
        protected DataSourceTextField comment = new DataSourceTextField(FIELDNAME_COMMENT, "Comment");
        protected DataSourceIntegerField age = new DataSourceIntegerField(FIELDNAME_AGE, "Age");
        protected DataSourceIntegerField wage = new DataSourceIntegerField(FIELDNAME_WAGE, "Wage");

        private UserDataSource() {
            super();
            id.setPrimaryKey(true);
            addField(id);
            addField(userName);
            addField(groupId);
            addField(roleId);
            addField(createdOn);
            addField(comment);

            initData();
        }

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

    private class GroupDataSource extends BugReportDataSource {
        public static final String FIELDNAME_ID = "FIELD_id";
        public static final String FIELDNAME_GROUPNAME = "FIELD_groupName";

        protected DataSourceIntegerField id = new DataSourceIntegerField(FIELDNAME_ID, "Id");
        protected DataSourceTextField groupName = new DataSourceTextField(FIELDNAME_GROUPNAME, "Groupname");

        private GroupDataSource() {
            super();
            addField(id);
            addField(groupName);

            initData();
        }

        @Override
        protected void initData() {
            data = new ArrayList<ListGridRecord>();
            for (int i = 1; i <= 5; i++) {
                ListGridRecord listGridRecord = new ListGridRecord();
                listGridRecord.setAttribute(FIELDNAME_ID, data.size());
                listGridRecord.setAttribute(FIELDNAME_GROUPNAME, "dummyGroup"+ Integer.toString(i));
                data.add(listGridRecord);
            }
        }
    }

    private class RoleDataSource extends BugReportDataSource {
        public static final String FIELDNAME_ID = "FIELD_id";
        public static final String FIELDNAME_ROLENAME = "FIELD_roleName";

        protected DataSourceIntegerField id = new DataSourceIntegerField(FIELDNAME_ID, "Id");
        protected DataSourceTextField roleName = new DataSourceTextField(FIELDNAME_ROLENAME, "Rolename");

        private RoleDataSource() {
            super();
            addField(id);
            addField(roleName);

            initData();
        }

        @Override
        protected void initData() {
            data = new ArrayList<ListGridRecord>();
            for (int i = 1; i <= 5; i++) {
                ListGridRecord listGridRecord = new ListGridRecord();
                listGridRecord.setAttribute(FIELDNAME_ID, data.size());
                listGridRecord.setAttribute(FIELDNAME_ROLENAME, "dummyRole"+ Integer.toString(i));
                data.add(listGridRecord);
            }
        }
    }

    private class UserDynamicForm extends DynamicForm {
        protected TextItem userName = new TextItem(UserDataSource.FIELDNAME_USERNAME, "Username");
        protected ComboBoxItem groupName = new ComboBoxItem(UserDataSource.FIELDNAME_GROUPID, "Groupname");
        protected ComboBoxItem roleName = new ComboBoxItem(UserDataSource.FIELDNAME_ROLEID, "Rolename");
        protected DateItem createdOn = new DateItem(UserDataSource.FIELDNAME_CREATEDONID, "Created On");
        protected RichTextItem comment = new RichTextItem(UserDataSource.FIELDNAME_COMMENT, "Comment");
        protected IntegerItem age = new IntegerItem(UserDataSource.FIELDNAME_AGE, "Age");
        protected IntegerItem wage = new IntegerItem(UserDataSource.FIELDNAME_WAGE, "Wage");

        protected void setComboBox(ComboBoxItem comboBoxItem, DataSource dataSource, String valueField, String displayField) {
            dataSource.setShowPrompt(false);
            comboBoxItem.setOptionDataSource(dataSource);

            comboBoxItem.setValueField(valueField);
            comboBoxItem.setDisplayField(displayField);
        }

        protected void init() {
            setComboBox(groupName, new GroupDataSource(), GroupDataSource.FIELDNAME_ID, GroupDataSource.FIELDNAME_GROUPNAME);
            setComboBox(roleName, new RoleDataSource(), RoleDataSource.FIELDNAME_ID, RoleDataSource.FIELDNAME_ROLENAME);

            this.setFields(
                    // userName
                    groupName
                    // , roleName
                    // , createdOn
                    // , comment
                    // , age
                    , wage
            );
        }
    }

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

    @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);

        Button 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);

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

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