package com.hzya.frame.sysnew.plugin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.base.PluginBaseEntityVo;
import com.hzya.frame.sysnew.plugin.service.IPluginService;
import com.hzya.frame.util.PluginUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.springframework.stereotype.Service;

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

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2023-08-07 9:45
 * @Version 1.0
 */
@Service(value = "pluginService")
public class PluginServiceImpl implements IPluginService {
    @Override
    public JsonResultEntity queryPlugins(JSONObject jsonObject) {
        List<PluginBaseEntity> list = PluginUtils.getPlugins();
        List<PluginBaseEntityVo> voList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                PluginBaseEntityVo pluginBaseEntityVo = new PluginBaseEntityVo();
                pluginBaseEntityVo.setPluginName(list.get(i).getPluginName());
                pluginBaseEntityVo.setPluginId(list.get(i).getPluginId());
                pluginBaseEntityVo.setPluginLabel(list.get(i).getPluginLabel());
                pluginBaseEntityVo.setPluginType(list.get(i).getPluginType());
                voList.add(pluginBaseEntityVo);
                //System.out.println("插件名称：" + list.get(i).getPluginName());
            }
        }

        return BaseResult.getSuccessMessageEntity(voList);
    }

    @Override
    public JsonResultEntity queryPluginsByType(JSONObject jsonObject) {
        JSONObject requestData = jsonObject.getJSONObject("jsonStr");
        String pluginType = requestData.getString("pluginType");
        List<PluginBaseEntity> list = PluginUtils.getPluginsByType(pluginType);
        List<PluginBaseEntityVo> voList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                PluginBaseEntityVo pluginBaseEntityVo = new PluginBaseEntityVo();
                pluginBaseEntityVo.setPluginName(list.get(i).getPluginName());
                pluginBaseEntityVo.setPluginId(list.get(i).getPluginId());
                pluginBaseEntityVo.setPluginLabel(list.get(i).getPluginLabel());
                pluginBaseEntityVo.setPluginType(list.get(i).getPluginType());
                voList.add(pluginBaseEntityVo);
            }
        }
        return BaseResult.getSuccessMessageEntity(voList);
    }

    @Override
    public JsonResultEntity queryPluginById(JSONObject jsonObject) {
        JSONObject requestData = jsonObject.getJSONObject("jsonStr");
        String pluginId = requestData.getString("pluginId");
        PluginBaseEntity pluginBaseEntity = PluginUtils.getPluginsById(pluginId);
        PluginBaseEntityVo pluginBaseEntityVo =  null;
        if(null != pluginBaseEntity){
            pluginBaseEntityVo = new PluginBaseEntityVo();
            pluginBaseEntityVo.setPluginName(pluginBaseEntity.getPluginName());
            pluginBaseEntityVo.setPluginId(pluginBaseEntity.getPluginId());
            pluginBaseEntityVo.setPluginLabel(pluginBaseEntity.getPluginLabel());
            pluginBaseEntityVo.setPluginType(pluginBaseEntity.getPluginType());
        }

        return BaseResult.getSuccessMessageEntity(pluginBaseEntityVo);
    }

    @Override
    public JsonResultEntity executeBusinessPluginById(JSONObject jsonObject) throws Exception {
        JSONObject requestData = jsonObject.getJSONObject("jsonStr");
        String pluginId = requestData.getString("pluginId");
        PluginBaseEntity pluginBaseEntity = null;
        JsonResultEntity result;
        if (StrUtil.isEmpty(pluginId)) {
            throw new BaseSystemException("请传入pluginId参数！");
        } else {
            pluginBaseEntity = PluginUtils.getPluginsById(pluginId);
        }
        if (null == pluginBaseEntity) {
            throw new BaseSystemException("根据ID获取插件错误！请传入正确的 pluginId");
        } else {
//            JSONObject requestJsonObj = new JSONObject();
//            String type = requestData.getString("type");
//            String param = requestData.getString("param");
//            requestJsonObj.put("type", type);
//            requestJsonObj.put("param", param);
            //执行业务逻辑代码
            result = pluginBaseEntity.executeBusiness(requestData);
        }
        return BaseResult.getSuccessMessageEntity(result);
    }
}
