package de.kopis.timeclicker.pages;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import de.kopis.timeclicker.exceptions.NotAuthenticatedException;
import de.kopis.timeclicker.model.TimeEntry;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

public class TimeEntryPage extends TemplatePage {

    public TimeEntryPage(PageParameters parameters) {
        super("Edit entry", parameters);

        if (getCurrentUser() == null) {
            error("Not authenticated!");
        }

        try {
            // load TimeEntry by key
            TimeEntry entry;
            if (parameters.get("key") != null && !parameters.get("key").isEmpty()) {
                final String key = parameters.get("key").toString();
                entry = getApi().show(key, getCurrentUser());
            } else {
                entry = new TimeEntry();
            }
            //TODO extract Form?
            final Form<TimeEntry> form = new Form<>("entryForm");
            form.setDefaultModel(new CompoundPropertyModel(entry));
            form.add(new HiddenField("key"));
            //TODO add timezone to DateTimeField?
            form.add(new DateTimeField("start"));
            form.add(new DateTimeField("stop"));
            form.add(new TextField("tags"));
            form.add(new Button("update") {
                @Override
                public void onSubmit() {
                    //TODO implement LoadableDetachableModel with TimeEntry!
                    TimeEntry updateEntry = (TimeEntry) getForm().getModel().getObject();
                    try {
                        UserService userService = UserServiceFactory.getUserService();
                        User user = userService.getCurrentUser();
                        getApi().update(updateEntry.getKey(), updateEntry.getStart(), updateEntry.getStop(), updateEntry.getTags(), user);
                        success("Entry saved.");
                    } catch (NotAuthenticatedException e) {
                        error("Can not save entry. Try again.");
                    }
                }
            });
            add(form);
        } catch (NotAuthenticatedException e) {
            error("Can not load entry, you're not authenticated");
        }
    }
}

@@ -41,154 +41,54 @@
 ort 
-com.google.appengine.api.users.User;%0Aimport com.google.appengine.api.users.UserService;%0Aimport com.google.appengine.api.users.UserServiceFactory
+java.util.Locale;%0Aimport java.util.TimeZone
 ;%0A
+%0A
 impo
@@ -194,16 +194,100 @@
 eEntry;%0A
+import org.apache.wicket.Session;%0Aimport org.apache.wicket.core.request.ClientInfo;%0A
 import o
@@ -602,16 +602,78 @@
 yModel;%0A
+import org.apache.wicket.protocol.http.request.WebClientInfo;%0A
 import o
@@ -731,16 +731,170 @@
 eters;%0A%0A
+import com.google.appengine.api.users.User;%0Aimport com.google.appengine.api.users.UserService;%0Aimport com.google.appengine.api.users.UserServiceFactory;%0A%0A
 public c
@@ -1478,24 +1478,24 @@
 imeEntry();%0A
-
             
@@ -1488,32 +1488,452 @@
 ;%0A            %7D%0A
+%0A            //TODO get locale from usersettings%0A            final Locale locale = Locale.getDefault();%0A            Session.get().setLocale(locale);%0A%0A            final ClientInfo info = Session.get().getClientInfo();%0A            if (info instanceof WebClientInfo) %7B%0A                final TimeZone tz = getTimeZone(getCurrentUser());%0A                ((WebClientInfo) info).getProperties().setTimeZone(tz);%0A            %7D%0A%0A
             //TO

Set user timezone when editing a TimeEntry