package com.brilliance.mda.runtime.request;

import com.brilliance.mda.runtime.mda.IBaseObject;
import com.brilliance.mda.runtime.mda.IDatafield;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;

import java.util.List;

import static com.brilliance.mda.runtime.mda.Constants.tdAttrChecked;
import static com.brilliance.mda.runtime.mda.Constants.tdAttrCheckedIdx;

public class CheckedParams {

    public static final String CHK_IDS ="chkIds";
    public static final String CHK_DST="chkDst";

    public static void checkedParamsInit(BaseVO req) {
        List<Integer> chkIds = null;
        String chkDst = null;
        if(req.getParams().containsKey(CHK_IDS)){
            chkIds = (List<Integer>) req.getParams().get(CHK_IDS);
        }
        if(req.getParams().containsKey(CHK_DST)){
            chkDst = (String)req.getParams().get(CHK_DST);
        }
        if(chkDst == null){
            return ;
        }
        IBaseObject dist = MdaDriver.getBaseObject(null,chkDst);
        if(dist == null){
            return ;
        }
        if(dist instanceof IDatafield){
            Object val = ((IDatafield)dist).getValue();
            if(!(val instanceof IStream)){
                return;
            }
            IStream distStm = (IStream)val;
            IStream selStm = new StreamImpl();
            IStream selIdsStrem = new StreamImpl();
            for (int i = 0; i <chkIds.size(); i++) {
                selStm.addLine(distStm.getLine(chkIds.get(i)));
                selIdsStrem.addLine(String.valueOf(chkIds.get(i)));
            }
            distStm.setAttribute(tdAttrChecked,selStm);
            distStm.setAttribute(tdAttrCheckedIdx,selIdsStrem);
        }
    }
}
