package com.brilliance.isc.common.component;

import com.alibaba.fastjson.JSONArray;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.vo.collection.Docgrd;
import com.brilliance.isc.vo.collection.Docgrdm;
import com.brilliance.mda.runtime.mda.Constants;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Streams;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

@Component
public class DocgrdmComponent {

    private static Logger logger = LoggerFactory.getLogger(DocgrdmComponent.class);

    public String toJsonString(Docgrdm docgrdm) {
        Map<String, Object> map = new HashMap<>();
        map.put("left", docgrdm.getDocgrd());
        map.put("right", docgrdm.getRightdocgrd());
        return Utils.toJson(map);
    }

    public Docgrdm jsonStrToObj(String jsonStr) {
        Docgrdm docgrdm = new Docgrdm();
        docgrdm.reset();
        Map map = new HashMap();
        try {
            map = Utils.toObject(Map.class, jsonStr);
        } catch (Exception e) {
            // 兼容老国结数据
            logger.warn("Deserialize Str to Json Exception!");
            map = fillDocumentsGrid(jsonStr);
        }
        if (map != null) {
            docgrdm.setDocgrd(map.get("left") == null ? new ArrayList<>() : JSONArray.parseArray(Utils.toJson(map.get("left")), Docgrd.class));
            docgrdm.setRightdocgrd(map.get("right") == null ? new ArrayList<>() : JSONArray.parseArray(Utils.toJson(map.get("right")), Docgrd.class));
        }
        return docgrdm;
    }

    public Docgrdm creatDefault(List<String> leftDocnams, List<String> rightDocnams) {
        Docgrdm docgrdm = new Docgrdm();
        List<Docgrd> leftDocList = new ArrayList<>();
        for (String docnam : leftDocnams) {
            Docgrd docgrd = new Docgrd();
            docgrd.setDocnam(docnam);
            leftDocList.add(docgrd);
        }
        docgrdm.setDocgrd(leftDocList);
        List<Docgrd> rightDocList = new ArrayList<>();
        for (String docnam : rightDocnams) {
            Docgrd docgrd = new Docgrd();
            docgrd.setDocnam(docnam);
            rightDocList.add(docgrd);
        }
        docgrdm.setRightdocgrd(rightDocList);
        return docgrdm;
    }

    /**
     * 根据传入的单据组件信息，判断是否要出二次面函，如果单据组件中的cmail2有一个不为空，则需要出二次面函
     *
     * @param docgrdm
     * @return
     */
    public boolean semFlg(Docgrdm docgrdm) {
        if(docgrdm == null){
            return false;
        }
        docgrdm.reset();

        // 如果左侧单据cmail2的值不为空，则返回true
        for(Docgrd leftdocgrd:docgrdm.getDocgrd()){
            if(leftdocgrd!=null&&!MdaUtils.isEmpty(leftdocgrd.getCmail2())){
                return true;
            }
        }

        // 如果右侧单据cmail2的值不为空，则返回true
        for (Docgrd rightdocgrd:docgrdm.getRightdocgrd()) {
            if(rightdocgrd!=null&&!MdaUtils.isEmpty(rightdocgrd.getCmail2())){
                return true;
            }
        }
        // default false
        return false;
    }

    public Map fillDocumentsGrid(String jsonStr) {
        Map map = new HashMap();
        String delimiter;
        if (Strings.pos(jsonStr, Constants.TAB) > 0) {
            delimiter = Constants.TAB;
        } else {
            delimiter = " ";
        }
        IStream docStm = new StreamImpl();
        int docCnt = 0;
        String line = NULLSTR;
        int pos = 0;
        List<Docgrd> left = new ArrayList<>();
        if (!MdaUtils.isEmpty(jsonStr)) {
            Streams.streamSet(docStm, jsonStr);
            docCnt = Streams.streamCount(docStm);
            for (int i = 1; i <= docCnt; i++) {
                line = Strings.getLine(docStm,i);
                if (!MdaUtils.isEmpty(line)) {
                    pos = Strings.pos(line,delimiter);
                    if (pos > 0) {
                        Docgrd docgrd = new Docgrd();
                        docgrd.setCmail1( Strings.mid(line,1,pos - 1));
                        line = Strings.mid(line,pos + 1,Strings.len(line));
                        pos = Strings.pos(line,delimiter);
                        if( pos > 0 ) {
                            docgrd.setCmail2(Strings.mid(line,1,pos - 1));
                            docgrd.setDocnam(Strings.trim(Strings.mid(line,pos + 1,57)));
                        }
                        left.add(docgrd);
                    }
                }
            }
        }
        map.put("left", left);
        map.put("right", new ArrayList<>());
        return map;
    }
}
