package cn.com.capcloud.api.action.service;

import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.noentitydb.DefaultActionFace;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

/**
 * @Author LiaoJiaHui
 * @Create 2026/4/29
 */
@Service
public class ActionResolveService {

    @Autowired
    private List<DefaultActionFace> defaultActionFaces;
    @Autowired
    private ApiService apiService;
    private DefaultActionFace get(String actionClassName) {
        DefaultActionFace defaultActionFace = null;
        for (int i = 0; i < defaultActionFaces.size(); i++) {
            if (defaultActionFaces.get(i).getClass().getName().indexOf(actionClassName) >= 0) {
                defaultActionFace = defaultActionFaces.get(i);
            }
        }
        return defaultActionFace;
    }
    public ActionResolveResult resolveAction(Map<String, Object> dsMap, JackJSONObject retJson) {
        String actionClass = String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass) || actionClass.length() == 0) {
            return new ActionResolveResult(this.get(DefaultAction.class.getName()), false, DefaultAction.class.getName());
        }
        DefaultActionFace actionImpl = this.get(actionClass);
        if (actionImpl == null) {
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("6010")));
            return null;
        }
        return new ActionResolveResult(actionImpl, true, actionClass);
    }
    public static class ActionResolveResult {
        private final DefaultActionFace actionImpl;
        private final boolean existsAction;
        private final String actionClass;
        public ActionResolveResult(DefaultActionFace actionImpl, boolean existsAction, String actionClass) {
            this.actionImpl = actionImpl;
            this.existsAction = existsAction;
            this.actionClass = actionClass;
        }
        public DefaultActionFace getActionImpl() {
            return actionImpl;
        }
        public boolean isExistsAction() {
            return existsAction;
        }
        public String getActionClass() {
            return actionClass;
        }
    }

}

