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 = OutputSplitSpecialtyEnum.PROJECT_LEAD.getCode();
    public static final String SPECIALTY_ARCH = OutputSplitSpecialtyEnum.ARCH.getCode();
    public static final String SPECIALTY_DECOR = OutputSplitSpecialtyEnum.DECOR.getCode();
    public static final String SPECIALTY_STRUCT = OutputSplitSpecialtyEnum.STRUCT.getCode();
    public static final String SPECIALTY_WATER = OutputSplitSpecialtyEnum.WATER.getCode();
    public static final String SPECIALTY_ELEC = OutputSplitSpecialtyEnum.ELEC.getCode();
    public static final String SPECIALTY_HVAC = OutputSplitSpecialtyEnum.HVAC.getCode();
    public static final String SPECIALTY_DIGITAL = OutputSplitSpecialtyEnum.DIGITAL.getCode();

    public static final String ROLE_DIRECTOR = OutputSplitRoleEnum.DIRECTOR.getCode();
    public static final String ROLE_CHECK = OutputSplitRoleEnum.CHECK.getCode();
    public static final String ROLE_REVIEW = OutputSplitRoleEnum.REVIEW.getCode();
    public static final String ROLE_APPROVE = OutputSplitRoleEnum.APPROVE.getCode();
    public static final String ROLE_DESIGN = OutputSplitRoleEnum.DESIGN.getCode();
    public static final String ROLE_PROJECT_MANAGER = OutputSplitRoleEnum.PROJECT_MANAGER.getCode();
    public static final String ROLE_ENGINEERING_PRINCIPAL = OutputSplitRoleEnum.ENGINEERING_PRINCIPAL.getCode();

    public static final List<SpecialtyItem> ASSIGNMENT_SPECIALTY_ITEMS = Arrays.asList(
            new SpecialtyItem(SPECIALTY_PROJECT_LEAD, OutputSplitSpecialtyEnum.PROJECT_LEAD.getLabel(), 0),
            new SpecialtyItem(SPECIALTY_ARCH, OutputSplitSpecialtyEnum.ARCH.getLabel(), 1),
            new SpecialtyItem(SPECIALTY_DECOR, OutputSplitSpecialtyEnum.DECOR.getLabel(), 2),
            new SpecialtyItem(SPECIALTY_STRUCT, OutputSplitSpecialtyEnum.STRUCT.getLabel(), 3),
            new SpecialtyItem(SPECIALTY_WATER, OutputSplitSpecialtyEnum.WATER.getLabel(), 4),
            new SpecialtyItem(SPECIALTY_ELEC, OutputSplitSpecialtyEnum.ELEC.getLabel(), 5),
            new SpecialtyItem(SPECIALTY_HVAC, OutputSplitSpecialtyEnum.HVAC.getLabel(), 6),
            new SpecialtyItem(SPECIALTY_DIGITAL, OutputSplitSpecialtyEnum.DIGITAL.getLabel(), 7)
    );

    public static final List<SpecialtyItem> SPECIALTY_ITEMS = Arrays.asList(
            new SpecialtyItem(SPECIALTY_ARCH, OutputSplitSpecialtyEnum.ARCH.getLabel(), 1),
            new SpecialtyItem(SPECIALTY_DECOR, OutputSplitSpecialtyEnum.DECOR.getLabel(), 2),
            new SpecialtyItem(SPECIALTY_STRUCT, OutputSplitSpecialtyEnum.STRUCT.getLabel(), 3),
            new SpecialtyItem(SPECIALTY_WATER, OutputSplitSpecialtyEnum.WATER.getLabel(), 4),
            new SpecialtyItem(SPECIALTY_ELEC, OutputSplitSpecialtyEnum.ELEC.getLabel(), 5),
            new SpecialtyItem(SPECIALTY_HVAC, OutputSplitSpecialtyEnum.HVAC.getLabel(), 6),
            new SpecialtyItem(SPECIALTY_DIGITAL, OutputSplitSpecialtyEnum.DIGITAL.getLabel(), 7)
    );

    public static final List<RoleItem> SPECIALTY_ROLE_ITEMS = Arrays.asList(
            new RoleItem(ROLE_DIRECTOR, OutputSplitRoleEnum.DIRECTOR.getLabel(), 1),
            new RoleItem(ROLE_CHECK, OutputSplitRoleEnum.CHECK.getLabel(), 2),
            new RoleItem(ROLE_REVIEW, OutputSplitRoleEnum.REVIEW.getLabel(), 3),
            new RoleItem(ROLE_APPROVE, OutputSplitRoleEnum.APPROVE.getLabel(), 4),
            new RoleItem(ROLE_DESIGN, OutputSplitRoleEnum.DESIGN.getLabel(), 5)
    );

    public static final List<RoleItem> PROJECT_LEAD_ROLE_ITEMS = Arrays.asList(
            new RoleItem(ROLE_PROJECT_MANAGER, OutputSplitRoleEnum.PROJECT_MANAGER.getLabel(), 1),
            new RoleItem(ROLE_ENGINEERING_PRINCIPAL, OutputSplitRoleEnum.ENGINEERING_PRINCIPAL.getLabel(), 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;
    }

}
