package nckd.jxccl.hr.hstu.plugin.form;

import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.ConfirmTypes;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.portal.util.SerializationUtils;
import kd.bos.schedule.api.TaskInfo;
import kd.bos.schedule.api.TaskStatusConstant;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.EventObject;
import java.util.Map;

/**
* 【优秀生】-综合测评问卷
* @author W.Y.C
* @date 2025/7/14 15:39
* @version 1.0
*/
public class EvalQuestListPlugin extends AbstractListPlugin implements Plugin {

    private static final Log logger = LogFactory.getLog(EvalQuestListPlugin.class);

    @Override
    public void registerListener(EventObject e) {
        //监听工具栏
        this.addItemClickListeners(FormConstant.TOOLBARAP);

    }
    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        String operationKey = evt.getOperationKey();
        if(HonorStudentConstant.OP_CREATEEVAL.equalsIgnoreCase(operationKey)){
            //获取评价对象
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId(HonorStudentConstant.NCKD_CREATEEVALQUEST_ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCloseCallBack(new CloseCallBack(this, HonorStudentConstant.NCKD_CREATEEVALQUEST_ENTITYID));
            showParameter.setCaption("生成综合测评问卷");
            this.getView().showForm(showParameter);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        boolean success = e.getOperationResult() != null && e.getOperationResult().isSuccess();

        if(HonorStudentConstant.OP_GENERATE_QUEST.equalsIgnoreCase(operateKey) && success) {
            //生成问卷（答卷） & 执行成功后刷新列表
            this.getView().invokeOperation(FormConstant.REFRESH_OP);
            this.getView().showSuccessNotification("生成问卷成功");
        }else if(HonorStudentConstant.OP_BEGINEVAL.equalsIgnoreCase(operateKey) && success) {
            //发起评测
            this.getView().invokeOperation(FormConstant.REFRESH_OP);
            this.getView().showSuccessNotification("发起评测成功");
        }else if(HonorStudentConstant.OP_ENDEVAL.equalsIgnoreCase(operateKey) && success) {
            //结束评测
            this.getView().invokeOperation(FormConstant.REFRESH_OP);
            this.getView().showSuccessNotification("结束评测成功");
        }else if(HonorStudentConstant.OP_GETSCORE.equalsIgnoreCase(operateKey) && success){
            //获取并更新评分结果
            this.getView().invokeOperation(FormConstant.REFRESH_OP);
            this.getView().showSuccessNotification("更新考评结果成功");
        }

    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if(HonorStudentConstant.TASK_CREATEEVAL.equalsIgnoreCase(actionId)){
            //生成评价问卷任务完成回调
            Map<String, Object> result = (Map<String, Object>)closedCallBackEvent.getReturnData();
            if(result != null) {
                String taskInfoStr = (String) result.get("taskinfo");
                if (StringUtils.isNotBlank(taskInfoStr)) {
                    TaskInfo taskInfo = SerializationUtils.fromJsonString(taskInfoStr, TaskInfo.class);
                    if (taskInfo.isTaskEnd()) {
                        String status = taskInfo.getStatus();
                        if (TaskStatusConstant.COMPLETED.equalsIgnoreCase(status)) {
                            //刷新列表
                            this.getView().invokeOperation(FormConstant.REFRESH_OP);
                            //成功提示
                            this.getView().showConfirm(
                                    "成功提示",
                                    "生成综合评测问卷完成",
                                    MessageBoxOptions.OK,
                                    ConfirmTypes.Save,
                                    new ConfirmCallBackListener()
                            );
                        }
                    }
                }
            }
        }
    }

    @Override
    public void setFilter(SetFilterEvent e) {
        super.setFilter(e);
        e.setOrderBy("createtime asc");
    }
}