package quant.rich.emoney.pojo.dto;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonView;

import lombok.Data;
import lombok.experimental.Accessors;
import quant.rich.emoney.util.HtmlSanitizer;

@Data
public class NonParamsIndexDetail implements IndexDetail {
    
    @JsonView(IndexDetail.class)
    private String id;
    @JsonView(IndexDetail.class)
    private String name;
    @JsonView(IndexDetail.class)
    private String nameCode;
    @JsonView(IndexDetail.class)
    private List<NonParamsIndexDetailData> data = new ArrayList<>();
    @JsonView(IndexDetail.class)
    private String original;
    
    @Data
    @Accessors(chain=true)
    public static class NonParamsIndexDetailData {
        @JsonView(IndexDetail.class)
        private String title;
        @JsonView(IndexDetail.class)
        private List<String> items;
        @JsonView(IndexDetail.class)
        private String image;
    }

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

    @Override
    public String getIndexCode() {
        return nameCode;
    }

    @Override
    public List<Detail> getDetails() {
        List<Detail> list = new ArrayList<>();
        data.forEach(d -> {
            if (StringUtils.isNotBlank(d.getTitle())) {
                list.add(new Detail().setContent(d.getTitle()).setType(DetailType.TITLE));
            }
            if (!CollectionUtils.isEmpty(d.getItems())) {
                d.getItems().forEach(i -> {
                    list.add(new Detail().setContent(i).setType(DetailType.TEXT));
                });
            }
            String image = d.getImage();
            if (StringUtils.isNotBlank(image)) {
                // 判断是否是合法 base64 图片，如果不是，生成警告
                if (!image.startsWith("data:image/") || !image.contains("base64")) {
                    list.add(new Detail()
                        .setContent(
                            new StringBuilder()
                            .append("配图 url: ")
                            .append(image)
                            .append(" 无法获取其地址，请检查或重试")
                            .toString()
                        )
                        .setType(DetailType.TEXT));
                }
                else {
                    list.add(new Detail().setContent(image).setType(DetailType.IMAGE));
                }
            }
        });
        return list;
    }
    
    @Override
    public void sanitize() {
        for (NonParamsIndexDetailData data : getData()) {
            String title = data.getTitle();
            if (StringUtils.isNotBlank(title)) {
                data.setTitle(HtmlSanitizer.sanitize(title));
            }
            if (data.getItems() != null) {
                List<String> items = new ArrayList<>();
                for (String item : data.getItems()) {
                    items.add(HtmlSanitizer.sanitize(item));
                }
                data.setItems(items);
            }
            
        }
    }

}
