package com.yihu.hos.crawler.model.adapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.crawler.model.patient.PatientIdentity;
import com.yihu.hos.standard.model.adapter.AdapterDatasetModel;
import com.yihu.hos.standard.model.adapter.AdapterDictEntryModel;
import com.yihu.hos.standard.model.adapter.AdapterMetadataModel;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import com.yihu.hos.standard.service.bo.AdapterVersion;
import com.yihu.hos.web.framework.util.springutil.SpringBeanUtil;

import java.util.*;


/**
 * @author Air
 * @version 1.0
 * @created 2015.08.11 11:29
 */
public class AdapterDataSet {

    private List<AdapterMetaData> adapterMetaDataList;
    private AdapterDatasetModel adapterDatasetModel;
    private Map<String, AdapterMetaData> adapterMetaDataMap;
    private String eventNoCode;
    private AdapterVersion adapterVersion;

    public AdapterDataSet(AdapterDatasetModel adapterDatasetModel, AdapterVersion adapterVersion, Map<Integer, List<AdapterDictEntryModel>> entryMap) {
        this.adapterDatasetModel = adapterDatasetModel;
        this.adapterVersion = adapterVersion;
        prepareData(entryMap);
    }

    public AdapterDatasetModel getAdapterDataSetT() {
        return adapterDatasetModel;
    }

    public void prepareData(Map<Integer, List<AdapterDictEntryModel>> entryMap) {
        try {
            adapterMetaDataList = new ArrayList<>();
            adapterMetaDataMap = new HashMap<>();
            Map<String, Object> condition = new HashMap<>();
            condition.put("column", "adapter_metadata_code");

            ObjectMapper objectMapper = new ObjectMapper();
            AdapterMetadataService metadataService = SpringBeanUtil.getService(AdapterMetadataService.BEAN_ID);
            List<AdapterMetadataModel> adapterMetaDataModelList = metadataService.getAdapterMetadataNotNullList(adapterVersion, adapterDatasetModel.getStdDatasetId(),objectMapper.writeValueAsString(condition));
            List<Integer> stdMetaDataIdList = new ArrayList<>();
            if (!CollectionUtil.isEmpty(adapterMetaDataModelList)) {
                for (AdapterMetadataModel adapterMetadataModel : adapterMetaDataModelList) {
                    stdMetaDataIdList.add(adapterMetadataModel.getStdMetadataId());
                }
                for (AdapterMetadataModel adapterMetadataModel : adapterMetaDataModelList) {
                    AdapterMetaData adapterMetaData = new AdapterMetaData(adapterMetadataModel, adapterVersion, entryMap);
                    adapterMetaDataList.add(adapterMetaData);
                    adapterMetaDataMap.put(adapterMetadataModel.getStdMetadataCode().toUpperCase(), adapterMetaData);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map<String, AdapterMetaData> getAdapterMetaDataMap() {
        return adapterMetaDataMap;
    }

    public List<AdapterMetaData> getAdapterMetaDataList() {
        return adapterMetaDataList;
    }

    public boolean isHavePatientID() {
        return findPatientIdentity(PatientIdentity.getPatientIDCode());
    }

    public boolean isHaveEventNo() {
        Set<String> eventNoSet = PatientIdentity.getEventNoSet();
        for (String eventNo : eventNoSet) {
            if (findPatientIdentity(eventNo)) {
                eventNoCode = eventNo;
                return true;
            }
        }

        return false;
    }

    public boolean isHaveLocalCardNo() {
        return findPatientIdentity(PatientIdentity.getLocalCardNoCode());
    }

    public boolean isHaveIdCard() {
        return findPatientIdentity(PatientIdentity.getIdCardCode());
    }

    public boolean findPatientIdentity(String code) {
        if (getAdapterMetaDataMap().get(code) == null) {
            return false;
        }

        return true;
    }

    public String getEventNoCode() {
        return eventNoCode;
    }
}
