/*
 * Copyright (C) 2000 - 2024 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception. You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "https://www.silverpeas.org/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

package org.silverpeas.components.infoletter.model;

import org.silverpeas.components.infoletter.InfoLetterContentManager;
import org.silverpeas.core.admin.user.model.User;
import org.silverpeas.core.contribution.model.ContributionIdentifier;
import org.silverpeas.core.contribution.model.SilverpeasContent;
import org.silverpeas.core.contribution.model.WithURL;
import org.silverpeas.core.util.DateUtil;
import org.silverpeas.core.util.ServiceProvider;
import org.silverpeas.kernel.SilverpeasRuntimeException;

import java.text.ParseException;
import java.util.Date;
import java.util.Optional;

/**
 * @author lbertin
 */
public class InfoLetterPublicationPdC extends InfoLetterPublication
    implements SilverpeasContent, WithURL {
  private static final long serialVersionUID = -2174573301215680444L;
  public static final String TYPE = "publication";

  private String silverObjectId;
  private String positions;

  /**
   * Default constructor
   */
  public InfoLetterPublicationPdC() {
    super();
  }

  /**
   * Constructor from InfoLetterPublication
   * @param ilp InfoLetterPublication
   */
  public InfoLetterPublicationPdC(InfoLetterPublication ilp) {
    super(ilp.getPK().toResourceReference(), ilp.getTitle(), ilp.getDescription(),
        ilp.getParutionDate(), ilp.getPublicationState(), ilp.getLetterId());
  }

  /**
   * @return the positions
   */
  public String getPositions() {
    return positions;
  }

  /**
   * @param positions the positions to set
   */
  public void setPositions(String positions) {
    this.positions = positions;
  }

  @Override
  public String getName() {
    return getTitle();
  }

  @Override
  public String getURL() {
    return "searchResult?Type=Publication&Id=" + getId();
  }

  @Override
  public String getId() {
    return getPK().getId();
  }

  @Override
  public Date getCreationDate() {
    try {
      return DateUtil.parse(getParutionDate());
    } catch (ParseException e) {
      throw new SilverpeasRuntimeException(e);
    }
  }

  @Override
  public String getComponentInstanceId() {
    return getInstanceId();
  }

  @Override
  public Date getLastUpdateDate() {
    return getCreationDate();
  }

  @Override
  public User getCreator() {
    return null;
  }

  @Override
  public User getLastUpdater() {
    return null;
  }

  @Override
  public String getSilverpeasContentId() {
    if (this.silverObjectId == null) {
      InfoLetterContentManager contentManager =
          ServiceProvider.getService(InfoLetterContentManager.class);
      int objectId = contentManager.getSilverContentId(getId(), getInstanceId());
      if (objectId >= 0) {
        this.silverObjectId = String.valueOf(objectId);
      }
    }
    return this.silverObjectId;
  }

  @Override
  public ContributionIdentifier getIdentifier() {
    return ContributionIdentifier.from(getInstanceId(), getId(), getContributionType());
  }

  @Override
  public String getContributionType() {
    return TYPE;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Optional<InfoLetterPath> getResourcePath() {
    return Optional.of(this).map(InfoLetterPath::getPath);
  }

  @Override
  public boolean equals(final Object o) {
    return super.equals(o);
  }

  @Override
  public int hashCode() {
    return super.hashCode();
  }
}
