package net.ranides.assira.time2;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;

import java.time.LocalDate;
import java.time.ZonedDateTime;

import static org.junit.Assert.*;

public class ZonedDateTest {

    @Test
    public void testWithZone() {
        ZonedDate date = ZonedDate.of(DateTimeUtils.ZONE_PL, LocalDate.of(2020, 11, 28));

        assertEquals("2020-11-28 [Poland]", date.toString());
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testWithZoneError() {
        ZonedDate date = ZonedDate.of(DateTimeUtils.ZONE_PL, LocalDate.of(2020, 11, 28));

        System.out.println(date.withZone(DateTimeUtils.ZONE_UTC));
    }

    @Test
    public void testSerialization() throws JsonProcessingException {
        // https://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring

        MyItem item = MyItem.builder()
            .id(1170)
            .code("RTTI")
            .date1(LocalDate.now())
            .date2(ZonedDate.now())
            .date3(ZonedDate.now())
            .date4(ZonedDate.now())
            .build();

        ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());

        System.out.println(mapper.writeValueAsString(item));
    }

    @Test
    public void testEpoch() {
        ZonedDateTime datetime1 = DateTimeLiteral.datetimeUtc(2020, 11, 20, 20, 45, 0);
        ZonedDateTime datetime2 = DateTimeLiteral.datetimeUtc(2020, 11, 20, 20, 45, 0);
        ZonedDateTime datetime3 = DateTimeLiteral.datetimeUtc(2020, 11, 20, 23, 45, 0);

        ZonedDate date1 = ZonedDate.of(datetime1).withZoneUtc();
        ZonedDate date2 = ZonedDate.of(datetime2).withZonePolish();
        ZonedDate date3 = ZonedDate.of(datetime3).withZonePolish();

        System.out.println(date1);
        System.out.println(date2);
        System.out.println(date3);

        long seconds1 = date1.withMin().toEpochSecond();
        long seconds2 = date2.withMin().toEpochSecond();
        long seconds3 = date3.withMin().toEpochSecond();

        System.out.println(date1.withMin());
        System.out.println(date2.withMin());
        System.out.println(date3.withMin());

        System.out.println(seconds1);
        System.out.println(seconds2);
        System.out.println(seconds3);

        ZonedDate out1 = ZonedDate.ofEpochSecond(DateTimeUtils.ZONE_UTC, seconds1);
        ZonedDate out2 = ZonedDate.ofEpochSecond(DateTimeUtils.ZONE_PL, seconds2);
        ZonedDate out3 = ZonedDate.ofEpochSecond(DateTimeUtils.ZONE_PL, seconds3);

        System.out.println(out1);
        System.out.println(out2);
        System.out.println(out3);

        System.out.println(out1.withMin());
        System.out.println(out2.withMin());
        System.out.println(out3.withMin());

        System.out.println(out1.withMin().toEpochSecond());
        System.out.println(out2.withMin().toEpochSecond());
        System.out.println(out3.withMin().toEpochSecond());
    }

    @Data
    @Builder
    public static class MyItem {
        private final int id;

        private String code;

        private LocalDate date1;

        @JsonFormat(timezone = "Etc/GMT0")
        private ZonedDate date2;

        @JsonFormat(pattern = "yyyy-MM-dd' ['VV']'")
        private ZonedDate date3;

        @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
        private ZonedDate date4;
    }
}