package cn.iocoder.lyzsys.module.tjt.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面 4 / 页面 5 业务常量
 *
 * @author Codex
 */
public final class OutputSplitBizConstants {

    public static final String SPECIALTY_PROJECT_LEAD = "project_lead";
    public static final String SPECIALTY_ARCH = "arch";
    public static final String SPECIALTY_DECOR = "decor";
    public static final String SPECIALTY_STRUCT = "struct";
    public static final String SPECIALTY_WATER = "water";
    public static final String SPECIALTY_ELEC = "elec";
    public static final String SPECIALTY_HVAC = "hvac";
    public static final String SPECIALTY_DIGITAL = "digital";

    public static final String ROLE_DIRECTOR = "director";
    public static final String ROLE_CHECK = "check";
    public static final String ROLE_REVIEW = "review";
    public static final String ROLE_APPROVE = "approve";
    public static final String ROLE_DESIGN = "design";
    public static final String ROLE_PROJECT_MANAGER = "project_manager";
    public static final String ROLE_ENGINEERING_PRINCIPAL = "engineering_principal";

    public static final List<SpecialtyItem> ASSIGNMENT_SPECIALTY_ITEMS = Arrays.asList(
            new SpecialtyItem(SPECIALTY_PROJECT_LEAD, "项目经理/工程负责人", 0),
            new SpecialtyItem(SPECIALTY_ARCH, "建筑", 1),
            new SpecialtyItem(SPECIALTY_DECOR, "装饰", 2),
            new SpecialtyItem(SPECIALTY_STRUCT, "结构", 3),
            new SpecialtyItem(SPECIALTY_WATER, "给排水", 4),
            new SpecialtyItem(SPECIALTY_ELEC, "电气", 5),
            new SpecialtyItem(SPECIALTY_HVAC, "暖通", 6),
            new SpecialtyItem(SPECIALTY_DIGITAL, "数字化设计", 7)
    );

    public static final List<SpecialtyItem> SPECIALTY_ITEMS = Arrays.asList(
            new SpecialtyItem(SPECIALTY_ARCH, "建筑", 1),
            new SpecialtyItem(SPECIALTY_DECOR, "装饰", 2),
            new SpecialtyItem(SPECIALTY_STRUCT, "结构", 3),
            new SpecialtyItem(SPECIALTY_WATER, "给排水", 4),
            new SpecialtyItem(SPECIALTY_ELEC, "电气", 5),
            new SpecialtyItem(SPECIALTY_HVAC, "暖通", 6),
            new SpecialtyItem(SPECIALTY_DIGITAL, "数字化设计", 7)
    );

    public static final List<RoleItem> SPECIALTY_ROLE_ITEMS = Arrays.asList(
            new RoleItem(ROLE_DIRECTOR, "专业负责人", 1),
            new RoleItem(ROLE_CHECK, "校对", 2),
            new RoleItem(ROLE_REVIEW, "审核", 3),
            new RoleItem(ROLE_APPROVE, "审定", 4),
            new RoleItem(ROLE_DESIGN, "设计", 5)
    );

    public static final List<RoleItem> PROJECT_LEAD_ROLE_ITEMS = Arrays.asList(
            new RoleItem(ROLE_PROJECT_MANAGER, "项目经理", 1),
            new RoleItem(ROLE_ENGINEERING_PRINCIPAL, "工程负责人", 2)
    );

    private OutputSplitBizConstants() {
    }

    public static boolean isValidSpecialtyCode(String code) {
        return ASSIGNMENT_SPECIALTY_ITEMS.stream().anyMatch(item -> item.getCode().equals(code));
    }

    public static boolean isValidRoleCode(String code) {
        return SPECIALTY_ROLE_ITEMS.stream().anyMatch(item -> item.getCode().equals(code))
                || PROJECT_LEAD_ROLE_ITEMS.stream().anyMatch(item -> item.getCode().equals(code));
    }

    public static boolean isValidRoleCode(String specialtyCode, String roleCode) {
        return getRoleItems(specialtyCode).stream().anyMatch(item -> item.getCode().equals(roleCode));
    }

    public static String getSpecialtyName(String code) {
        return ASSIGNMENT_SPECIALTY_ITEMS.stream()
                .filter(item -> item.getCode().equals(code))
                .findFirst()
                .map(SpecialtyItem::getName)
                .orElse(code);
    }

    public static String getRoleName(String code) {
        return getAllRoleItems().stream()
                .filter(item -> item.getCode().equals(code))
                .findFirst()
                .map(RoleItem::getName)
                .orElse(code);
    }

    public static int getRoleSortNo(String code) {
        return getAllRoleItems().stream()
                .filter(item -> item.getCode().equals(code))
                .findFirst()
                .map(RoleItem::getSortNo)
                .orElse(0);
    }

    public static int getSpecialtySortNo(String code) {
        return ASSIGNMENT_SPECIALTY_ITEMS.stream()
                .filter(item -> item.getCode().equals(code))
                .findFirst()
                .map(SpecialtyItem::getSortNo)
                .orElse(0);
    }

    public static Map<String, String> buildSpecialtyMap() {
        Map<String, String> map = new LinkedHashMap<>();
        for (SpecialtyItem item : SPECIALTY_ITEMS) {
            map.put(item.getCode(), item.getName());
        }
        return map;
    }

    public static List<RoleItem> getRoleItems(String specialtyCode) {
        if (SPECIALTY_PROJECT_LEAD.equals(specialtyCode)) {
            return PROJECT_LEAD_ROLE_ITEMS;
        }
        return SPECIALTY_ROLE_ITEMS;
    }

    private static List<RoleItem> getAllRoleItems() {
        return Arrays.asList(
                PROJECT_LEAD_ROLE_ITEMS.get(0),
                PROJECT_LEAD_ROLE_ITEMS.get(1),
                SPECIALTY_ROLE_ITEMS.get(0),
                SPECIALTY_ROLE_ITEMS.get(1),
                SPECIALTY_ROLE_ITEMS.get(2),
                SPECIALTY_ROLE_ITEMS.get(3),
                SPECIALTY_ROLE_ITEMS.get(4)
        );
    }

    @Getter
    @AllArgsConstructor
    public static class SpecialtyItem {

        private final String code;
        private final String name;
        private final Integer sortNo;
    }

    @Getter
    @AllArgsConstructor
    public static class RoleItem {

        private final String code;
        private final String name;
        private final Integer sortNo;
    }

}
