package com.liquidnet.service.sweet.constant;

import com.liquidnet.service.sweet.vo.SweetManualSortOptionVo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 草莓音乐节手册 Tab 显示模块
 */
public enum SweetManualPosition {

    ARTIST("artist", "艺人"),
    SIGNING_TIME("signingTime", "签售时间"),
    SITE_MAP("siteMap", "现场地图"),
    HOW_TO_REACH("howToReach", "交通出行"),
    OFFICIAL_SUPPORT("officialSupport", "官方支持"),
    AUDIENCE_NOTICE("audienceNotice", "观众须知"),
    PREVENTION_GUIDELINES("preventionGuidelines", "防疫指南"),
    NOTICE("notice", "通知公告"),
    STRATEGY("strategy", "观演攻略"),
    CUSTOMER_SERVICE("customerService", "客服"),
    FOOD_GUIDE("foodGuide", "餐饮攻略"),
    ALBUM("album", "相册"),
    LOST_FOUND("lostFound", "失物招领"),
    MAP_GEOJSON("mapGeojson", "互动地图");

    private final String key;
    private final String label;

    SweetManualPosition(String key, String label) {
        this.key = key;
        this.label = label;
    }

    public String getKey() {
        return key;
    }

    public String getLabel() {
        return label;
    }

    public static boolean isValid(String key) {
        if (key == null) {
            return false;
        }
        for (SweetManualPosition position : values()) {
            if (position.key.equals(key)) {
                return true;
            }
        }
        return false;
    }

    public static String getLabel(String key) {
        for (SweetManualPosition position : values()) {
            if (position.key.equals(key)) {
                return position.label;
            }
        }
        return key;
    }

    public static List<SweetManualSortOptionVo> allOptions() {
        return Arrays.stream(values())
                .map(item -> new SweetManualSortOptionVo(item.key, item.label))
                .collect(Collectors.toList());
    }

    public static List<String> parsePositions(String showPosition) {
        if (showPosition == null || showPosition.trim().isEmpty()) {
            return new ArrayList<>();
        }
        return Arrays.stream(showPosition.split(","))
                .map(String::trim)
                .filter(item -> !item.isEmpty())
                .collect(Collectors.toList());
    }

    public static String normalizeContent(String content) {
        List<String> invalidKeys = new ArrayList<>();
        List<String> positions = parsePositions(content).stream()
                .filter(key -> {
                    if (isValid(key)) {
                        return true;
                    }
                    invalidKeys.add(key);
                    return false;
                })
                .collect(Collectors.toList());
        if (!invalidKeys.isEmpty()) {
            throw new IllegalArgumentException("无效的模块标识: " + String.join(",", invalidKeys));
        }
        return String.join(",", positions);
    }
}
