package cn.axzo.msg.center.message.xxl;

import cn.axzo.msg.center.dal.MessageTemplateButtonV3Dao;
import cn.axzo.msg.center.dal.MessageTemplateGroupV3Dao;
import cn.axzo.msg.center.dal.MessageTemplateV3Dao;
import cn.axzo.msg.center.domain.entity.MessageTemplateButtonV3;
import cn.axzo.msg.center.domain.entity.MessageTemplateGroupV3;
import cn.axzo.msg.center.domain.entity.MessageTemplateV3;
import cn.axzo.msg.center.service.domain.CardUrlConfig;
import cn.axzo.msg.center.service.domain.DetailConfig;
import cn.axzo.msg.center.service.domain.UrlConfig;
import cn.axzo.msg.center.service.enums.RouterButtonSourceEnum;
import cn.axzo.msg.center.service.enums.RouterCategoryEnum;
import cn.axzo.msg.center.service.enums.WebPageOpenStrategy;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SetWebOpenStrategyJob {

    private final MessageTemplateV3Dao messageTemplateV3Dao;
    private final MessageTemplateGroupV3Dao messageTemplateGroupV3Dao;
    private final MessageTemplateButtonV3Dao messageTemplateButtonV3Dao;

    @SuppressWarnings("unused")
    @XxlJob("setWebOpenStrategyJob")
    public ReturnT<String> execute(String jsonStr) throws Exception {
        try {
            setCardUrl();
            setGroupUrl();
            setButtonUrl();
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.warn("job failed", e);
            throw e;
        }
    }

    private void setCardUrl() {
        for (MessageTemplateV3 template : messageTemplateV3Dao.list()) {
            CardUrlConfig cardUrlConfig = template.getCardUrlConfig();
            if (cardUrlConfig == null || cardUrlConfig.getUrlConfig() == null) {
                continue;
            }
            updateWebOpenStrategy(cardUrlConfig.getUrlConfig());
            MessageTemplateV3 update = new MessageTemplateV3();
            update.setId(template.getId());
            update.setCardUrlConfig(cardUrlConfig);
            messageTemplateV3Dao.updateById(update);
        }
    }

    private void setGroupUrl() {
        for (MessageTemplateGroupV3 group : messageTemplateGroupV3Dao.list()) {
            if (group.getGroupConfig() == null || group.getGroupConfig().getDetail() == null) {
                continue;
            }
            DetailConfig detail = group.getGroupConfig().getDetail();
            if (detail.getUrlConfig() == null) {
                continue;
            }
            updateWebOpenStrategy(detail.getUrlConfig());
            MessageTemplateGroupV3 update = new MessageTemplateGroupV3();
            update.setId(group.getId());
            update.setGroupConfig(group.getGroupConfig());
            messageTemplateGroupV3Dao.updateById(update);
        }
    }

    private void setButtonUrl() {
        for (MessageTemplateButtonV3 button : messageTemplateButtonV3Dao.list()) {
            boolean doUpdate = false;
            MessageTemplateButtonV3 update = new MessageTemplateButtonV3();
            update.setId(button.getId());

            if (button.getSource() == RouterButtonSourceEnum.SYSTEM) {
                String btnName = button.getName();
                if (btnName.equals("同意"))
                    update.setCode("BPMN_APPROVE");
                else if (btnName.equals("驳回"))
                    update.setCode("BPMN_REJECT");
                else if (btnName.equals("撤回"))
                    update.setCode("BPMN_REVOCATION");
                else if (btnName.equals("转交"))
                    update.setCode("BPMN_TRANSFER");
                else if (btnName.equals("加签"))
                    update.setCode("BPMN_COUNTERSIGN");
                else if (btnName.equals("评论"))
                    update.setCode("BPMN_COMMENT");
                else if (btnName.equals("回退"))
                    update.setCode("BPMN_ROLLBACK");
                doUpdate = true;
            }

            if (button.getCategory() == RouterCategoryEnum.JUMP && button.getUrlConfig() != null) {
                updateWebOpenStrategy(button.getUrlConfig());
                update.setUrlConfig(button.getUrlConfig());
                doUpdate = true;
            }

            if (doUpdate)
                messageTemplateButtonV3Dao.updateById(update);
        }
    }

    private void updateWebOpenStrategy(UrlConfig urlConfig) {
        if (urlConfig == null) return;
        if (urlConfig.getPcOms() != null) {
            urlConfig.getPcOms().setOpenStrategy(WebPageOpenStrategy.CURRENT_PAGE);
        }
        if (urlConfig.getPcCms() != null) {
            urlConfig.getPcCms().setOpenStrategy(WebPageOpenStrategy.CURRENT_PAGE);
        }
    }

}