/*
 * Copyright (C) 2000 - 2025 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 <http://www.gnu.org/licenses/>.
 */

package org.silverpeas.mobile.shared.dto.news;

import jsinterop.base.JsPropertyMap;

import java.io.Serializable;

public class NewsDTO implements Serializable {

    private static final long serialVersionUID = 2921606984249560882L;

    private String id;
    private String idNews;
    private String title;
    private String description;
    private String updateDate;
    private String vignette;
    private String instanceId;
    private String startDate;
    private String endDate;

    private String content;

    private boolean important;
    private boolean draft;
    private boolean visible;

    private boolean managable = false;

    public NewsDTO() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj) {
        return ((NewsDTO) obj).getId().equals(getId());
    }

    public String getIdNews() {
        return idNews;
    }

    public String getVignette() {
        return vignette;
    }

    public void setVignette(final String vignette) {
        this.vignette = vignette;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(final String title) {
        this.title = title;
    }

    public String getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(final String updateDate) {
        this.updateDate = updateDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    public void setInstanceId(final String instanceId) {
        this.instanceId = instanceId;
    }

    public String getInstanceId() {
        return instanceId;
    }

    public boolean getManagable() {
        return managable;
    }

    public void setManagable(final boolean managable) {
        this.managable = managable;
    }


    public boolean getVisible() {
        return visible;
    }

    public void setVisible(final boolean visible) {
        this.visible = visible;
    }

    public boolean getDraft() {
        return draft;
    }

    public void setDraft(final boolean draft) {
        this.draft = draft;
    }

    public void setIdNews(final String idNews) {
        this.idNews = idNews;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean getImportant() {
        return important;
    }

    public void setImportant(boolean important) {
        this.important = important;
    }

    public void setStartDate(String startDate) {
        if (startDate != null) {
            this.startDate = startDate.replaceAll("T", " ");
        } else {
            this.startDate = startDate;
        }
    }

    public String getStartDate() {
        return startDate;
    }

    public void setEndDate(String endDate) {
        if (endDate != null) {
            this.endDate = endDate.replaceAll("T", " ");
        } else {
            this.endDate = endDate;
        }
    }

    public String getEndDate() {
        return endDate;
    }

    public static NewsDTO fromJSON(JsPropertyMap<Object> json) {
        NewsDTO dto = new NewsDTO();

        dto.setId((String) json.get("id"));
        dto.setIdNews((String) json.get("idNews"));
        dto.setTitle((String) json.get("title"));
        dto.setDescription((String) json.get("description"));
        dto.setUpdateDate((String) json.get("updateDate"));
        dto.setVignette((String) json.get("vignette"));
        dto.setInstanceId((String) json.get("instanceId"));
        dto.setStartDate((String) json.get("startDate"));
        dto.setEndDate((String) json.get("endDate"));
        dto.setContent((String) json.get("content"));

        Object important = json.get("important");
        if (important != null) {
            dto.setImportant((Boolean) important);
        }

        Object draft = json.get("draft");
        if (draft != null) {
            dto.setDraft((Boolean) draft);
        }

        Object visible = json.get("visible");
        if (visible != null) {
            dto.setVisible((Boolean) visible);
        }

        Object managable = json.get("managable");
        if (managable != null) {
            dto.setManagable((Boolean) managable);
        }


        return dto;

    }

    public JsPropertyMap<Object> toJSON() {
        JsPropertyMap<Object> json = JsPropertyMap.of();

        json.set("id", getId());
        json.set("idNews", getIdNews());
        json.set("title", getTitle());
        json.set("description", getDescription());
        json.set("updateDate", getUpdateDate());
        json.set("vignette", getVignette());
        json.set("instanceId", getInstanceId());
        json.set("startDate", getStartDate());
        json.set("endDate", getEndDate());
        json.set("content", getContent());

        json.set("important", getImportant());
        json.set("draft", getDraft());
        json.set("visible", getVisible());
        json.set("managable", getManagable());

        return json;
    }
}
