package nato.ivct.gui.client;

import java.util.Locale;

import org.eclipse.scout.rt.client.dto.FormData;
import org.eclipse.scout.rt.client.ui.ClientUIPreferences;
import org.eclipse.scout.rt.client.ui.form.AbstractForm;
import org.eclipse.scout.rt.client.ui.form.fields.button.AbstractCancelButton;
import org.eclipse.scout.rt.client.ui.form.fields.button.AbstractOkButton;
import org.eclipse.scout.rt.client.ui.form.fields.groupbox.AbstractGroupBox;
import org.eclipse.scout.rt.client.ui.form.fields.smartfield.AbstractSmartField;
import org.eclipse.scout.rt.client.ui.messagebox.MessageBoxes;
import org.eclipse.scout.rt.platform.Order;
import org.eclipse.scout.rt.platform.nls.LocaleUtility;
import org.eclipse.scout.rt.platform.text.TEXTS;
import org.eclipse.scout.rt.shared.services.lookup.ILookupCall;

import nato.ivct.gui.client.OptionsForm.MainBox.CancelButton;
import nato.ivct.gui.client.OptionsForm.MainBox.LocaleField;
import nato.ivct.gui.client.OptionsForm.MainBox.LogLevelField;
import nato.ivct.gui.client.OptionsForm.MainBox.OkButton;
import nato.ivct.gui.shared.AvailableLocaleLookupCall;
import nato.ivct.gui.shared.LogLevelLookupCall;
import nato.ivct.gui.shared.OptionsFormData;


@FormData(value = OptionsFormData.class, sdkCommand = FormData.SdkCommand.CREATE)
public class OptionsForm extends AbstractForm {

    @Override
    protected String getConfiguredTitle() {
        return TEXTS.get("Options");
    }


    @Override
    protected void execInitForm() {
        // set the language
        final String localeString = ClientUIPreferences.getClientPreferences(ClientSession.get()).get(ClientSession.PREF_USER_LOCALE, "");
        getLocaleField().setValue(LocaleUtility.parse(localeString));

        // set the log level
        final String logLevelString = ClientUIPreferences.getClientPreferences(ClientSession.get()).get(ClientSession.CUR_LOG_LEVEL, "");
        getLogLevelField().setValue(logLevelString);
    }


    public CancelButton getCancelButton() {
        return getFieldByClass(CancelButton.class);
    }


    public MainBox getMainBox() {
        return getFieldByClass(MainBox.class);
    }


    public LocaleField getLocaleField() {
        return getFieldByClass(LocaleField.class);
    }


    public LogLevelField getLogLevelField() {
        return getFieldByClass(LogLevelField.class);
    }


    public OkButton getOkButton() {
        return getFieldByClass(OkButton.class);
    }

    @Order(1000)
    public class MainBox extends AbstractGroupBox {

        @Override
        protected int getConfiguredGridColumnCount() {
            return 1;
        }

        @Order(1000)
        public class LocaleField extends AbstractSmartField<Locale> {

            @Override
            protected int getConfiguredGridW() {
                return 1;
            }


            @Override
            protected String getConfiguredLabel() {
                return TEXTS.get("Language");
            }


            @Override
            protected boolean getConfiguredStatusVisible() {
                return false;
            }


            @Override
            protected Class<? extends ILookupCall<Locale>> getConfiguredLookupCall() {
                return AvailableLocaleLookupCall.class;
            }
        }

        @Order(1100)
        public class LogLevelField extends AbstractSmartField<String> {

            @Override
            protected int getConfiguredGridW() {
                return 1;
            }


            @Override
            protected String getConfiguredLabel() {
                return TEXTS.get("LogLevel");
            }


            @Override
            protected boolean getConfiguredStatusVisible() {
                return false;
            }


            @Override
            protected Class<? extends ILookupCall<String>> getConfiguredLookupCall() {
                return LogLevelLookupCall.class;
            }
        }

        @Order(100000)
        public class OkButton extends AbstractOkButton {
            @Override
            protected void execClickAction() {
                // publish log level
                final String level = getLogLevelField().getValue();
                final Locale language = getLocaleField().getValue();

                if (level != null) {
                    storeLogLevel(level);
                }

                if (language != null) {
                    storeLanguageOptions();
                }
            }
        }

        @Order(101000)
        public class CancelButton extends AbstractCancelButton {}
    }

    protected void storeLogLevel(String logLevel) {
        final boolean logLevelChanged = ClientUIPreferences.getClientPreferences(ClientSession.get()).put(ClientSession.CUR_LOG_LEVEL, getLogLevelField().getValue().toString());
        if (logLevelChanged) {
            ClientUIPreferences.getClientPreferences(ClientSession.get()).flush();
        }
    }


    protected void storeLanguageOptions() {
        // not inside form handler, because the form is used in a FormToolButton without a handler
        final boolean localeChanged = ClientUIPreferences.getClientPreferences(ClientSession.get()).put(ClientSession.PREF_USER_LOCALE, getLocaleField().getValue().toString());
        if (localeChanged) {
            ClientUIPreferences.getClientPreferences(ClientSession.get()).flush();
            MessageBoxes.createOk().withBody(TEXTS.get("ChangeOfLanguageApplicationOnNextLogin")).show();
        }
    }

    // Not used by this form
    //	public class ModifyHandler extends AbstractFormHandler {
    //
    //		@Override
    //		protected void execLoad() {
    //			IOptionsService service = BEANS.get(IOptionsService.class);
    //			OptionsFormData formData = new OptionsFormData();
    //			exportFormData(formData);
    //			formData = service.load(formData);
    //			importFormData(formData);
    //
    //			setEnabledPermission(new UpdateOptionsPermission());
    //		}
    //
    //		@Override
    //		protected void execStore() {
    //			IOptionsService service = BEANS.get(IOptionsService.class);
    //			OptionsFormData formData = new OptionsFormData();
    //			exportFormData(formData);
    //			service.store(formData);
    //		}
    //	}
    //
    //	public class NewHandler extends AbstractFormHandler {
    //
    //		@Override
    //		protected void execLoad() {
    //			IOptionsService service = BEANS.get(IOptionsService.class);
    //			OptionsFormData formData = new OptionsFormData();
    //			exportFormData(formData);
    //			formData = service.prepareCreate(formData);
    //			importFormData(formData);
    //
    //			setEnabledPermission(new CreateOptionsPermission());
    //		}
    //
    //		@Override
    //		protected void execStore() {
    //			IOptionsService service = BEANS.get(IOptionsService.class);
    //			OptionsFormData formData = new OptionsFormData();
    //			exportFormData(formData);
    //			service.create(formData);
    //		}
    //	}
}

@@ -586,24 +586,68 @@
 ssageBoxes;%0A
+import org.eclipse.scout.rt.platform.BEANS;%0A
 import org.e
@@ -1139,32 +1139,77 @@
 caleLookupCall;%0A
+import nato.ivct.gui.shared.IOptionsService;%0A
 import nato.ivct
@@ -4930,32 +4930,34 @@
 ) %7B%0A            
+//
 ClientUIPreferen
@@ -5003,32 +5003,100 @@
 get()).flush();%0A
+            BEANS.get(IOptionsService.class).setLogLevel(logLevel);%0A
         %7D%0A    %7D%0A

Set the log level also during TC execution