package com.brilliance.mda.runtime.request;

import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.mda.runtime.mda.util.Systems;

import java.util.List;

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

public class SelectedParams {

    public static final String SEL_IDS ="selIds";
    public static final String SEL_DST="selDst";
    public static final String SEL_BTN_ID="selBtnId";

    public static final String ButtonIndex = "ButtonIndex";
    public static final String ButtonId = "ButtonId";


    public static void selectedParamsInit(BaseVO req){
        List<Integer> selIds = null;
        String selDst = null;
        if(req.getParams().containsKey(SEL_IDS)){
            selIds = (List<Integer>) req.getParams().get(SEL_IDS);
        }
        if(req.getParams().containsKey(SEL_DST)){
            selDst = (String)req.getParams().get(SEL_DST);
        }

        if(selIds == null || selIds.size() == 0){
            return ;
        }
        int tdSel = selIds.get(0);
        if(Strings.isEmpty(selDst)){
            setNomalSelected(tdSel);
            return;
        }
        IBaseObject dist = MdaDriver.getBaseObject(null,selDst);
        if(dist == null){
            return ;
        }

        if(dist instanceof IModuleList){
            setSelectedRow((IModuleList)dist,tdSel);
            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 <selIds.size(); i++) {
                selStm.addLine(distStm.getLine(selIds.get(i)));
                selIdsStrem.addLine(String.valueOf(selIds.get(i)));
            }
            distStm.setAttribute(tdAttrSelected,selStm);
            distStm.setAttribute(tdAttrSelectedIdx,selIdsStrem);

            Systems.setAttribute(dist.getParent(),dist.getName(),ButtonIndex,selIds.get(0));
            String selBtnId = "";
            if(req.getParams().containsKey(SEL_BTN_ID)){
                selBtnId = (String) req.getParams().get(SEL_BTN_ID);
                Systems.setAttribute(dist.getParent(),dist.getName(),ButtonId,selBtnId);
            }
        }
    }

    public static String buildSelKey(Object obj,String ATTR){
        return obj.toString()+ATTR;
    }

    public static void setSelectedLine(IStream stm,int idx){
        IStream selected = MdaEnv.getContext().getParam(buildSelKey(stm,tdAttrSelectedIdx));
        if(selected == null){
            selected = new StreamImpl();
            stm.setAttribute(tdAttrSelectedIdx,selected);
        }else{
            selected.getRows().clear();
        }
        selected.getRows().add(String.valueOf(idx));
    }
    public static int getSelectedLine(IStream stream){
        IStream selected = stream.getAttribute(tdAttrSelectedIdx);
//        IStream selected = MdaEnv.getContext().getParam(buildSelKey(stream,tdAttrSelectedIdx));
        if(selected == null || selected.isEmpty()){
            return 0;
        }
        return Integer.valueOf(selected.getRows().get(0));
    }

    public  static void setSelectedRow(IModuleList<? extends IModule> list, int index) {
        MdaEnv.getContext().setAttribute(list,"SELECT_INDEX","index",index);
    }

    public  static int getSelectedRow(IModuleList<? extends IModule> list) {
        Integer idx = (Integer) MdaEnv.getContext().getAttribute(list,"SELECT_INDEX","index");
        return idx == null?0:idx;
    }

    public  static int getSelectedRowIndex(IModuleList<? extends IModule> list) {
        return getSelectedRow(list);
    }

    public static void setNomalSelected(int index){
        MdaEnv.getContext().setParam("SELECTED_INDEX_NOMAL",index);
    }
    public static Integer getNomalSelected(){
        Integer idx = MdaEnv.getContext().getParam("SELECTED_INDEX_NOMAL");
        if(idx == null){
            return 0;
        }
        return idx;
    }

}
