package thyyxxk.webserver.utils;

import lombok.Getter;
import thyyxxk.webserver.constants.sidicts.Insutype;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;

import java.util.*;

public class MedInsTypeUtil {

    @Getter
    static class Judge {
        List<String> admdvs;
        Insutype insutype;
        List<String> medtype;

        public Judge(List<String> admdvs, Insutype insutype, List<String> medtype) {
            this.admdvs = admdvs;
            this.insutype = insutype;
            this.medtype = medtype;
        }
    }

    private static final Map<String, Judge> map = new HashMap<>();

    static {
        List<String> cssbj = Arrays.asList("430102", "430103", "430104", "430105", "430111", "430112", "430140", "430199");
        List<String> cssdbz = Arrays.asList("430102", "430103", "430104", "430105", "430111", "430112", "430140", "430199", "430121", "430181", "430182");
        List<String> csxdq = Collections.singletonList("430121");
        List<String> lysdq = Collections.singletonList("430181");
        List<String> nxsdq = Collections.singletonList("430182");
        List<String> hnsbj = Collections.singletonList("439900");
        List<String> ptzyMedtypes = Arrays.asList("2101", "22");
        List<String> matnMedtypes = Arrays.asList("52", "2106", "2107");
        List<String> dbzMedtypes = Collections.singletonList("2102");

        map.put("长沙市城职普通住院", new Judge(cssbj, Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES, ptzyMedtypes));
        map.put("长沙市城居普通住院", new Judge(cssbj, Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS, ptzyMedtypes));
        map.put("长沙市离休", new Judge(cssbj, Insutype.MEDICAL_SECURITY_FOR_RETIRED_PERSONNEL, ptzyMedtypes));
        map.put("长沙市生育", new Judge(cssbj, null, matnMedtypes));
        map.put("长沙县城职普通住院", new Judge(csxdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES, ptzyMedtypes));
        map.put("长沙县城居普通住院", new Judge(csxdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS, ptzyMedtypes));
        map.put("长沙县离休", new Judge(csxdq, Insutype.MEDICAL_SECURITY_FOR_RETIRED_PERSONNEL, ptzyMedtypes));
        map.put("长沙县生育", new Judge(csxdq, null, matnMedtypes));
        map.put("浏阳市城职普通住院", new Judge(lysdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES, ptzyMedtypes));
        map.put("浏阳市城居普通住院", new Judge(lysdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS, ptzyMedtypes));
        map.put("浏阳市离休", new Judge(lysdq, Insutype.MEDICAL_SECURITY_FOR_RETIRED_PERSONNEL, ptzyMedtypes));
        map.put("浏阳市生育", new Judge(lysdq, null, matnMedtypes));
        map.put("宁乡市城职普通住院", new Judge(nxsdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES, ptzyMedtypes));
        map.put("宁乡市城居普通住院", new Judge(nxsdq, Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS, ptzyMedtypes));
        map.put("宁乡市离休", new Judge(nxsdq, Insutype.MEDICAL_SECURITY_FOR_RETIRED_PERSONNEL, ptzyMedtypes));
        map.put("宁乡市生育", new Judge(nxsdq, null, matnMedtypes));
        map.put("湖南省城职普通住院", new Judge(hnsbj, Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES, ptzyMedtypes));
        map.put("湖南省城居普通住院", new Judge(hnsbj, Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS, ptzyMedtypes));
        map.put("湖南省离休", new Judge(hnsbj, Insutype.MEDICAL_SECURITY_FOR_RETIRED_PERSONNEL, ptzyMedtypes));
        map.put("湖南省生育", new Judge(hnsbj, null, matnMedtypes));
        map.put("省城职单病种", new Judge(hnsbj, null, dbzMedtypes));
        map.put("长沙市单病种", new Judge(cssdbz, null, dbzMedtypes));
    }

    public static String getMedInsType(SiSetlinfo setlinfo) {
        String admdvs = setlinfo.getInsuplcAdmdvs();
        Insutype insutype = Insutype.get(setlinfo.getInsutype());
        String medtype = setlinfo.getMedType();
        if (!admdvs.startsWith("43")) {
            return  "省外异地";
        }
        if (!admdvs.equals("439900") && admdvs.startsWith("43") && !admdvs.startsWith("4301")) {
            return medtype.equals("2102") ? "省内异地单病种" : "省内异地";
        }
        for (Map.Entry<String, Judge> entry : map.entrySet()) {
            Judge judge = entry.getValue();
            if (null == judge.getInsutype()) {
                if (judge.getAdmdvs().contains(admdvs) && judge.getMedtype().contains(medtype)) {
                    return entry.getKey();
                }
            }
            if (judge.getAdmdvs().contains(admdvs) && Objects.equals(judge.getInsutype(), insutype)
                    && judge.getMedtype().contains(medtype)) {
                return entry.getKey();
            }
        }
        return null;
    }
}
