package com.lesingle.modules.device.vo;

import com.lesingle.modules.ugc.entity.UgcWork;
import com.lesingle.modules.ugc.entity.UgcWorkPage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 终端设备作品详情
 */
@Data
@Schema(description = "终端设备作品详情")
public class DeviceWorkDetailVo implements Serializable {

    @Schema(description = "作品ID")
    private Long id;

    @Schema(description = "作品标题")
    private String title;

    @Schema(description = "作者名称")
    private String authorName;

    @Schema(description = "封面图URL")
    private String coverUrl;

    @Schema(description = "作品描述")
    private String description;

    @Schema(description = "发布状态")
    private String status;

    @Schema(description = "可见范围")
    private String visibility;

    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    @Schema(description = "修改时间")
    private LocalDateTime modifyTime;

    @Schema(description = "作品页面列表")
    private List<PageItem> pages;

    /**
     * 作品页面项
     */
    @Data
    @Schema(description = "作品页面项")
    public static class PageItem implements Serializable {

        @Schema(description = "页码")
        private Integer pageNo;

        @Schema(description = "图片URL")
        private String imageUrl;

        @Schema(description = "文本内容")
        private String text;

        @Schema(description = "音频URL")
        private String audioUrl;
    }

    /**
     * 从 PublicUserWorkService.findDetail() 返回的 Map 转换
     */
    @SuppressWarnings("unchecked")
    public static DeviceWorkDetailVo fromDetailMap(Map<String, Object> detailMap) {
        DeviceWorkDetailVo vo = new DeviceWorkDetailVo();

        UgcWork work = (UgcWork) detailMap.get("work");
        vo.setId(work.getId());
        vo.setTitle(work.getTitle());
        vo.setAuthorName(work.getAuthorName());
        vo.setCoverUrl(work.getCoverUrl());
        vo.setDescription(work.getDescription());
        vo.setStatus(work.getStatus());
        vo.setVisibility(work.getVisibility());
        vo.setCreateTime(work.getCreateTime());
        vo.setModifyTime(work.getModifyTime());

        List<UgcWorkPage> pages = (List<UgcWorkPage>) detailMap.get("pages");
        if (pages != null) {
            vo.setPages(new ArrayList<>());
            for (UgcWorkPage p : pages) {
                PageItem item = new PageItem();
                item.setPageNo(p.getPageNo());
                item.setImageUrl(p.getImageUrl());
                item.setText(p.getText());
                item.setAudioUrl(p.getAudioUrl());
                vo.getPages().add(item);
            }
        }
        return vo;
    }
}
