package it.integry.integrywmsnative.core.data_store.db.converter;

import androidx.room.TypeConverter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;

import it.integry.integrywmsnative.core.utility.UtilityDate;

public class AppDatabaseConverters {
    @TypeConverter
    public static LocalDateTime longToLocalDateTime(Long value) {
        return value == null ? null : UtilityDate.timeToLocalDateTime(value, null);
    }

    @TypeConverter
    public static Long localDateTimeToTimestamp(LocalDateTime date) {
        return date == null ? null : UtilityDate.localDateTimeToTime(date, null);
    }

    @TypeConverter
    public static LocalDate longToLocalDate(Long value) {
        return value == null ? null : UtilityDate.timeToLocalDate(value, null);
    }

    @TypeConverter
    public static Long localDateToTimestamp(LocalDate date) {
        return date == null ? null : UtilityDate.localDateToTime(date);
    }

    @TypeConverter
    public static ArrayList<String> fromString(String value) {
        Type listType = new TypeToken<ArrayList<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromArrayList(ArrayList<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }

}
