/*
 *  Copyright 2017 Alexey Zhokhov
 *
 *  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.zhokhov.graphql.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import static com.zhokhov.graphql.datetime.DateTimeHelper.DATE_FORMATTERS;

/**
 * @author <a href='mailto:alexey@zhokhov.com'>Alexey Zhokhov</a>
 */
class LocalDateTimeConverter {

    private final boolean zoneConversionEnabled;

    LocalDateTimeConverter(boolean zoneConversionEnabled) {
        this.zoneConversionEnabled = zoneConversionEnabled;
    }

    // ISO_8601
    String toISOString(LocalDateTime dateTime) {
        Objects.requireNonNull(dateTime, "dateTime");

        return DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.of(toUTC(dateTime), ZoneOffset.UTC));
    }

    LocalDateTime parseDate(String date) {
        Objects.requireNonNull(date, "date");
        int index = -1;
        for (DateTimeFormatter formatter : DATE_FORMATTERS) {
            index++;
            try {
                // equals ISO_LOCAL_DATE or custom date format
                if (index > 1) {
                    LocalDate localDate = LocalDate.parse(date, formatter);

                    return localDate.atStartOfDay();
                } else {
                    LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
                    return fromUTC(dateTime);
                }
            } catch (java.time.format.DateTimeParseException ignored) {
            }
        }

        return null;
    }

    private LocalDateTime convert(LocalDateTime dateTime, ZoneId from, ZoneId to) {
        if (zoneConversionEnabled) {
            return dateTime.atZone(from).withZoneSameInstant(to).toLocalDateTime();
        }
        return dateTime;
    }

    private LocalDateTime fromUTC(LocalDateTime dateTime) {
        return convert(dateTime, ZoneOffset.UTC, ZoneId.systemDefault());
    }

    private LocalDateTime toUTC(LocalDateTime dateTime) {
        return convert(dateTime, ZoneId.systemDefault(), ZoneOffset.UTC);
    }

}

@@ -1539,32 +1539,8 @@
 %22);%0A
-        int index = -1;%0A
     
@@ -1613,16 +1613,35 @@
     
-index++;
+// try to parse as dateTime
 %0A   
@@ -1675,54 +1675,70 @@
     
-// equals ISO_LOCAL_DATE or custom 
+LocalDateTime dateTime = LocalDateTime.parse(
 date
+,
  format
+ter);
 %0A   
@@ -1754,184 +1754,225 @@
     
-if (index %3E 1) %7B%0A                    LocalDate localDate = LocalDate.parse(date, formatter);%0A%0A                    return localDate.atStartOfDay();%0A                %7D else %7B%0A    
+return fromUTC(dateTime);%0A            %7D catch (java.time.format.DateTimeParseException ignored) %7B%7D%0A%0A            // try to parse as date%0A            try %7B%0A                // equals ISO_LOCAL_DATE or custom date format%0A
     
@@ -1988,36 +1988,33 @@
    LocalDate
-Time dateTim
+ localDat
 e = LocalDat
@@ -2006,36 +2006,32 @@
 Date = LocalDate
-Time
 .parse(date, for
@@ -2059,55 +2059,40 @@
     
-   
- 
 return 
-fromUTC(dateTime);%0A                %7D
+localDate.atStartOfDay();
 %0A   
@@ -2159,29 +2159,16 @@
 nored) %7B
-%0A            
 %7D%0A      

Remove index based parsing