package com.yihu.hos.standard.match.dictionary;

import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import com.yihu.hos.standard.dao.IStdDictionaryModelDao;
import com.yihu.hos.standard.dao.StdDictionaryEntryModelDao;
import com.yihu.hos.standard.match.matchModel.DictItemMatchVO;
import com.yihu.hos.standard.model.adapter.AdapterDictEntryModel;
import com.yihu.hos.standard.service.adapter.AdapterDictEntryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/24.
 * 字典项匹配器
 */
public class DictItemStrategyExecute implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(DictItemStrategyExecute.class);
    public static Map<String, List<Integer>> size = new HashMap<String, List<Integer>>();
    private DictItemMatchVO matchVO;
    private DictitemStandardExistStrategy standardExistStrategy;
    private AdapterDictEntryModel adapterDictEntryModel;
    private String std_version;
    private String adapter_std_version;
    private String version;
    private IStdDictionaryModelDao stdDictionaryDao;
    private StdDictionaryEntryModelDao stdDictionaryEntryDao;
    private AdapterDictEntryService adapterDictEntryService;
    private String schemaName;



    private DictitemStandardNameStrategy dictitemStandardNameStrategy;

    public DictItemStrategyExecute(
            String schemaName,
            AdapterDictEntryModel adapterDictEntryModel,
            DictItemMatchVO matchVO,
            String std_version,
            String adapter_std_version,
            String version,
            IStdDictionaryModelDao stdDictionaryDao,
                    StdDictionaryEntryModelDao stdDictionaryEntryDao,
                    AdapterDictEntryService adapterDictEntryService) {
        this.schemaName = schemaName;
        this.adapterDictEntryModel = adapterDictEntryModel;
        this.matchVO = matchVO;
        this.std_version = std_version;
        this.adapter_std_version = adapter_std_version;
        this.version = version;

        standardExistStrategy = new DictitemStandardExistStrategy();
        standardExistStrategy.setStdDictionaryDao(stdDictionaryDao);
        standardExistStrategy.setStdDictionaryEntryDao(stdDictionaryEntryDao);
        standardExistStrategy.setAdapterDictEntryService(adapterDictEntryService);
        standardExistStrategy.setSchemaName(schemaName);
        dictitemStandardNameStrategy = new DictitemStandardNameStrategy();
        dictitemStandardNameStrategy.setAdapterDictEntryService(adapterDictEntryService);
        dictitemStandardNameStrategy.setStdDictionaryDao(stdDictionaryDao);
        dictitemStandardNameStrategy.setSchemaName(schemaName);
    }

    public static void setVersionYes(String version) {
        List<Integer> sizeList = new ArrayList<Integer>();
        size.put(version, sizeList);
    }

    @Override
    public void run() {
        try {
            if (dictitemStandardNameStrategy.match(adapterDictEntryModel, version,std_version)) {
                updateAdapt(version);
                return;
            }

            if (matchVO != null) {
                if (standardExistStrategy.match(adapterDictEntryModel, matchVO,std_version,adapter_std_version, version)) {
                    updateAdapt(version);
                    return;
                }
            }
            updateAdapt(version);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, List<Integer>> getSize() {
        return size;
    }

    public synchronized static void updateAdapt(String version) {
        List<Integer> sizeList = size.get(version);
        sizeList.add(1, sizeList.get(1) + 1);
        logger.debug("更新匹配数目:" + sizeList.get(0) + "/" + (sizeList.get(1)));
    }

    public synchronized static void setFirst(String version, Integer allCount) {
        List<Integer> sizeList = new ArrayList<Integer>();
        sizeList.add(0, allCount);
        sizeList.add(1, 0);
        size.put(version, sizeList);
    }

    public synchronized static void removeAdapt(String version) {
        size.remove(version);
    }

    public synchronized static boolean isAdapt(String version) {
        if (size.get(version) == null) {
            return false;
        } else {
            return true;
        }
    }

    public synchronized static Map getAllNumAndAdaptNum(String version) {
        Map<String, Integer> adapt = new HashMap<String, Integer>();
        List<Integer> list = size.get(version);
        if(list!=null && !list.isEmpty()){
            adapt.put("all", list.get(0));
            adapt.put("adapt", list.get(1));
        }
        return adapt;
    }
}
