package cc.smtweb.system.bpm.web.sys.dev;

import cc.smtweb.framework.core.annotation.SwBody;
import cc.smtweb.framework.core.annotation.SwService;
import cc.smtweb.framework.core.common.AbstractEnum;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.mvc.service.SwListData;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageService;
import cc.smtweb.system.bpm.web.engine.dynPage.DynRetBean;
import org.springframework.util.Assert;

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

/**
 * Created by 1 at 2022-08-03 16:09:30
 * 页面【[枚举下拉]的服务类
 */
@SwService
public class EnumComboService extends DynPageService {
    //public final static String TYPE_DEMO = "demo";
    @Override
    protected AbstractHandler createHandler(String type) {
        return super.createHandler(type);
    }


    //自定义
    public R loadOne(@SwBody SwMap params, UserSession us) {
        SwListData swListData = buildListData(params.readString("cls"));
        DynRetBean bean = DynRetBean.createList(swListData);
        return R.success(bean);
    }

    private SwListData buildListData(String cls) {
        if (StringUtil.isEmpty(cls)) return SwListData.create(new ArrayList(), 0);
        List<SwMap> list = new ArrayList<>();
        try {
            Class tempClass;
            Class wantClass = null;
            try {
                tempClass = Class.forName(cls);
                wantClass = tempClass;
            } catch (Exception e) {
//                e.printStackTrace();
                //
                int lIndex = cls.lastIndexOf(".");
                if (lIndex > 0) {
                    String outClass = cls.substring(0, lIndex);
                    String innerClass = outClass + "$" + cls.substring(lIndex + 1);
                    tempClass = Class.forName(outClass);
                    Class[] declaredClasses = tempClass.getDeclaredClasses();
                    for (Class declaredClass : declaredClasses) {
                        if (declaredClass.getName().equals(innerClass)) {
                            wantClass = declaredClass;
                            break;
                        }
                    }
                }
            }
            Assert.notNull(wantClass, "classNotFound");
            AbstractEnum e;
            try {
                e = (AbstractEnum) wantClass.getDeclaredField("inst").get(null);
            } catch (Exception e1) {
                e = (AbstractEnum) wantClass.getDeclaredField("instance").get(null);
            }
            for (Object v : e.mapAll.values()) {
                AbstractEnum.EnumBean bean = (AbstractEnum.EnumBean) v;
                SwMap row = new SwMap();
                row.put("value", bean.value.toString());
                row.put("name", bean.name);
                list.add(row);
            }
        } catch (Exception ue) {
            return SwListData.create(new ArrayList(), 0);
        }
        return SwListData.create(list, 0);
    }


}
