package com.huangzj.protoplugin.memo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MemoItem {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public String id = "";
    public String title = "";
    public String content = "";
    public long updated = 0L;
    public boolean pinned = false;
    public List<String> tags = new ArrayList<>();

    public MemoItem() {}

    public MemoItem(String id, String title, String content, long updated) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.updated = updated;
    }

    public String displayTitle() {
        if (title != null && !title.trim().isEmpty()) {
            return title.trim();
        }
        return "(无标题)";
    }

    public String displayDate() {
        if (updated <= 0) {
            return "";
        }
        return DATE_FORMAT.format(new Date(updated));
    }

    public String contentPreview() {
        if (content == null || content.trim().isEmpty()) {
            return "";
        }
        String trimmed = content.trim();
        int newlineIdx = trimmed.indexOf('\n');
        String firstLine = newlineIdx >= 0 ? trimmed.substring(0, newlineIdx) : trimmed;
        if (firstLine.length() > 50) {
            return firstLine.substring(0, 50) + "...";
        }
        return firstLine;
    }

    public String tagsDisplay() {
        if (tags == null || tags.isEmpty()) {
            return "";
        }
        return String.join(", ", tags);
    }
}
