package it.topcs.controlroom.entity;

import org.postgresql.util.PGobject;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Converter(autoApply = false)
public class TextArrayConverter implements AttributeConverter<String[], PGobject> {
  @Override
  public PGobject convertToDatabaseColumn(String[] objectValue) {


    try {
      PGobject out = new PGobject();
      out.setType("text[]");
      out.setValue(String.join(",",objectValue));
      return out;
    } catch (Exception e) {
      throw new IllegalArgumentException("Unable to serialize to tex[] field ", e);
    }

  }

  @Override
  public String[] convertToEntityAttribute(PGobject dataValue) {
    if (dataValue == null) return null;

    return  dataValue.getValue().split(",");
  }
}
