package org.diagbot.bigdata.common;


import org.diagbot.bigdata.dao.model.ResultMappingDiag;
import org.diagbot.bigdata.dao.model.ResultMappingFilter;
import org.diagbot.bigdata.dao.model.ResultMappingVital;
import org.diagbot.bigdata.service.ResultMappingDiagService;
import org.diagbot.bigdata.service.ResultMappingFilterService;
import org.diagbot.bigdata.service.ResultMappingVitalService;
import org.diagbot.bigdata.util.BigDataConstants;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.*;

@WebListener
public class InitListener implements ServletContextListener {
    @Autowired
    ResultMappingVitalService resultMappingVitalService;
    @Autowired
    ResultMappingDiagService resultMappingDiagService;
    @Autowired
    ResultMappingFilterService resultMappingFilterService;

    public void contextDestroyed(ServletContextEvent arg0) {

    }

    /**
     * 开始初始化数据
     *
     * @return
     */
    public void contextInitialized(ServletContextEvent event) {
        contextStandardLibraryInitialized(event);
        contextFeatureMappingInitialized(event);
        contextResultMappingDiagInitialized(event);
        contextResultMappingFilterInitialized(event);
    }

    public void contextStandardLibraryInitialized(ServletContextEvent event) {
        ApplicationCacheUtil applicationCacheUtil = new ApplicationCacheUtil();
        applicationCacheUtil.putStandardInfoContext(event.getServletContext());
    }

    public void contextFeatureMappingInitialized(ServletContextEvent event) {
        List<ResultMappingVital> resultMappingVitals = resultMappingVitalService.selectList(new HashMap<>());
        Map<String, String> mapping = new HashMap<>();
        for (ResultMappingVital resultMappingVital : resultMappingVitals) {
            mapping.put(resultMappingVital.getName(), resultMappingVital.getNameMapping());
        }
        event.getServletContext().setAttribute(BigDataConstants.result_mapping_vital, mapping);
    }

    public void contextResultMappingDiagInitialized(ServletContextEvent event) {
        List<ResultMappingDiag> resultMappingDiags = resultMappingDiagService.selectList(new HashMap<>());

        Map<String, String> mapping = new HashMap<>();
        for (ResultMappingDiag resultMappingDiag : resultMappingDiags) {
            mapping.put(resultMappingDiag.getDiagName(), resultMappingDiag.getDeptName());
        }
        event.getServletContext().setAttribute(BigDataConstants.result_mapping_diag, mapping);
    }

    public void contextResultMappingFilterInitialized(ServletContextEvent event) {
        List<ResultMappingFilter> resultMappingFilters = resultMappingFilterService.selectList(new HashMap<>());

        Map<String, Map<String, ResultMappingFilter>> mapping = new HashMap<>();
        Map<String, ResultMappingFilter> filterMap = null;
        for (ResultMappingFilter resultMappingFilter : resultMappingFilters) {
            filterMap = mapping.get(resultMappingFilter.getFeatureType());
            if (filterMap == null) {
                filterMap = new HashMap<>();
            }
            filterMap.put(resultMappingFilter.getFeatureName(), resultMappingFilter);
            mapping.put(resultMappingFilter.getFeatureType(), filterMap);
        }
        event.getServletContext().setAttribute(BigDataConstants.result_mapping_filter, mapping);
    }

    private void put(Map<String, List<String>> map, String key, List<String> value, String ele) {
        if (value == null) {
            value = new ArrayList<>(Arrays.asList(ele));
            map.put(key, value);
        } else {
            if (!value.contains(ele)) {
                value.add(ele);
                map.put(key, value);
            }
        }
    }
}
