package com.brilliance.mda.runtime.mda.util;

import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.utils.SpringUtils;
import com.brilliance.mda.runtime.mda.CodetableItem;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class Codetables {

    private static final Logger log = LoggerFactory.getLogger(Codetables.class);


    public static List<CodetableItem> getCodeTableByName(String codeName)
    {
        return null;
    }

    public static String getCodetableLabelOfTd(String key, String codetable,String language) {
        String label = "";
        List<CodetableItem> codeTable = getCodeTable(codetable,language);
        if(!CollectionUtils.isEmpty(codeTable)){
            for (CodetableItem codetableItem : codeTable) {
                if(key.equalsIgnoreCase(codetableItem.getValue())){
                    label = codetableItem.getLabel();
                    break;
                }
            }
        }
        return label;
    }

    public static String getCodeTableKeyOfTd(String txt, String codetable, String language) {
        String key = "";
        List<CodetableItem> codeTable = getCodeTable(codetable ,  language);
        if(!CollectionUtils.isEmpty(codeTable)){
            for (CodetableItem codetableItem : codeTable) {
                if(txt.equals(codetableItem.getLabel())){
                    key = codetableItem.getValue();
                }
            }
        }
        return key;
    }



    public static IStream getTblKeyList(String codetable, String language) {
        IStream keylst = new StreamImpl();
        List<CodetableItem> codeTable = getCodeTable(codetable , language);
        if(!CollectionUtils.isEmpty(codeTable)){
            for (CodetableItem codetableItem : codeTable) {
                keylst.addLine(codetableItem.getValue());
            }
        }
        return keylst;
    }


    public static List<CodetableItem> getCodeTable(String codeName,String lang){
            //尝试从组件中加载
        CodetableService codetableService = SpringUtils.getBean(CodetableService.class);
        List<CodetableItem> dbCodeTable = codetableService.selectList(codeName.toUpperCase(),lang);
        if(dbCodeTable != null && !dbCodeTable.isEmpty())
            return dbCodeTable;
        else {
            return new ArrayList<>();
        }
    }

}
