package com.fs.hisStore.util;

import com.fs.hisStore.dto.ForbiddenDrugItemDTO;

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

public class ForbiddenDrugListManager {
    private List<ForbiddenDrugItemDTO> forbiddenDrugList = new ArrayList<>();
    private Long nextId = 1L;

    /**
     * 添加禁止药品（类似节目单添加节目）
     * @param name 禁止品种名称
     * @param type 类型1-4
     * @param exceptionKey 关键字
     * @param remark 备注
     */
    public void addItem(String name, String type, String exceptionKey, String remark) {
        ForbiddenDrugItemDTO item = new ForbiddenDrugItemDTO();
        item.setId(nextId++);
        item.setName(name);
        item.setType(type);
        item.setExceptionKey(exceptionKey);
        item.setRemark(remark);
        int maxSort = forbiddenDrugList.stream().mapToInt(ForbiddenDrugItemDTO::getSortNum).max().orElse(0);
        item.setSortNum(maxSort + 1);
        forbiddenDrugList.add(item);
        sortList();
    }

    /**
     * 删除禁止药品
     * @param id 禁止项ID
     */
    public boolean removeItem(Long id) {
        return forbiddenDrugList.removeIf(item -> id.equals(item.getId()));
    }

    /**
     * 调整排序
     * @param id 禁止项ID
     * @param direction 方向
     */
    public void adjustSort(Long id, String direction) {
        ForbiddenDrugItemDTO target = forbiddenDrugList.stream()
                .filter(item -> id.equals(item.getId()))
                .findFirst()
                .orElse(null);
        if (target == null) return;

        int targetIndex = forbiddenDrugList.indexOf(target);
        int swapIndex = "up".equals(direction) ? targetIndex - 1 : targetIndex + 1;
        if (swapIndex < 0 || swapIndex >= forbiddenDrugList.size()) return;
        ForbiddenDrugItemDTO swapItem = forbiddenDrugList.get(swapIndex);
        int tempSort = target.getSortNum();
        target.setSortNum(swapItem.getSortNum());
        swapItem.setSortNum(tempSort);
        sortList();
    }

    /**
     * 按排序号升序排列清单
     */
    private void sortList() {
        forbiddenDrugList = forbiddenDrugList.stream()
                .sorted(Comparator.comparingInt(ForbiddenDrugItemDTO::getSortNum))
                .collect(Collectors.toList());
    }

    /**
     * 获取按顺序排列的禁止清单
     */
    public List<ForbiddenDrugItemDTO> getSortedForbiddenList() {
        return new ArrayList<>(forbiddenDrugList);
    }

    /**
     * 初始化默认禁止清单
     */
    public void initDefaultList() {
        //政策禁止类
        addItem("疫苗", "1", "", "政策明确禁止");
        addItem("血液制品", "1", "", "政策明确禁止");
        addItem("麻醉药品", "1", "", "政策明确禁止");
        addItem("精神药品", "1", "", "政策明确禁止");
        addItem("医疗用毒性药品", "1", "", "政策明确禁止");
        addItem("放射性药品", "1", "", "政策明确禁止");
        addItem("药品类易制毒化学品", "1", "", "政策明确禁止");
        addItem("医疗机构制剂", "1", "", "政策明确禁止");
        addItem("中药配方颗粒", "1", "", "政策明确禁止");

        //剂型禁止类
        addItem("注射剂", "3", "降糖", "降糖类注射剂例外");
        addItem("含麻黄碱类复方制剂", "3", "麻黄", "含麻黄中成药例外");
        addItem("含麻醉药品口服复方制剂", "3", "", "无例外");
        addItem("含曲马多口服复方制剂", "3", "", "无例外");
        addItem("右美沙芬口服单方制剂", "3", "", "无例外");

        //兴奋剂禁止类
        addItem("蛋白同化制剂", "4", "胰岛素", "胰岛素例外");
        addItem("肽类激素", "4", "胰岛素", "胰岛素例外");

        //外用剂型
        String[] singleDrugs = {"地高辛", "丙吡胺", "奎尼丁", "哌唑嗪", "普鲁卡因胺", "普罗帕酮", "胺碘酮", "奎宁"};
        for (String drug : singleDrugs) {
            addItem(drug, "2", "外用", "外用剂型例外");
        }
    }
}
