/*
 * Copyright 2013 Urs Wolfer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.urswolfer.intellij.plugin.gerrit.rest.gson;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * A Gson deserializer which uses UTC as base for dates.
 *
 * I would prefer to use the default Gson parser, but I have found no way to tell Gson that dates are in UTC.
 *
 * @author Urs Wolfer
 */
public class DateDeserializer implements JsonDeserializer<Date> {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    static {
        DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    @Override
    public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        String date = jsonElement.getAsString();
        try {
            return DATE_FORMAT.parse(date);
        } catch (ParseException e) {
            throw new JsonParseException(e);
        }
    }
}

@@ -1231,16 +1231,86 @@
 %3CDate%3E %7B
+%0A    private static final String DATE_PATTERN = %22yyyy-MM-dd hh:mm:ss%22;
 %0A%0A    pr
@@ -1328,16 +1328,28 @@
 c final 
+ThreadLocal%3C
 SimpleDa
@@ -1356,16 +1356,17 @@
 teFormat
+%3E
  DATE_FO
@@ -1376,16 +1376,28 @@
 T = new 
+ThreadLocal%3C
 SimpleDa
@@ -1408,113 +1408,274 @@
 rmat
-(%22yyyy-MM-dd hh:mm:ss%22);%0A    static %7B%0A        DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A    %7D
+%3E() %7B%0A        @Override%0A        protected SimpleDateFormat initialValue() %7B%0A            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);%0A            dateFormat.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A            return dateFormat;%0A        %7D%0A    %7D;
 %0A%0A  
@@ -1927,16 +1927,22 @@
 _FORMAT.
+get().
 parse(da

fix DateDeserializer