package dev.bytevibe.hyperpoint;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.Serializable;
import java.util.UUID;

/**
 * 表示一个幻灯片，包含多个页面
 */
public class Slide implements Serializable {
    private String id;
    private String name;
    private ObservableList<SlidePage> pages;
    private int currentPageIndex;

    public Slide() {
        this.id = UUID.randomUUID().toString();
        this.name = "新幻灯片";
        this.pages = FXCollections.observableArrayList();
        this.currentPageIndex = 0;
        // 创建一个初始页面
        this.pages.add(new SlidePage("页面 1"));
    }

    public Slide(String name) {
        this.id = UUID.randomUUID().toString();
        this.name = name;
        this.pages = FXCollections.observableArrayList();
        this.currentPageIndex = 0;
        // 创建一个初始页面
        this.pages.add(new SlidePage("页面 1"));
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ObservableList<SlidePage> getPages() {
        return pages;
    }

    public int getCurrentPageIndex() {
        return currentPageIndex;
    }

    public void setCurrentPageIndex(int index) {
        if (index >= 0 && index < pages.size()) {
            this.currentPageIndex = index;
        }
    }

    public SlidePage getCurrentPage() {
        if (currentPageIndex >= 0 && currentPageIndex < pages.size()) {
            return pages.get(currentPageIndex);
        }
        return null;
    }

    /**
     * 添加新页面
     */
    public void addPage() {
        int pageNumber = pages.size() + 1;
        pages.add(new SlidePage("页面 " + pageNumber));
    }

    /**
     * 添加指定的页面
     */
    public void addPage(SlidePage page) {
        if (page != null) {
            pages.add(page);
        }
    }

    /**
     * 删除指定索引的页面（如果页面数大于1）
     */
    public boolean removePage(int index) {
        if (pages.size() > 1 && index >= 0 && index < pages.size()) {
            pages.remove(index);
            if (currentPageIndex >= pages.size()) {
                currentPageIndex = pages.size() - 1;
            }
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return name;
    }
}

