package cn.axzo.msg.center.message.service.todo;

import cn.axzo.msg.center.inside.notices.config.PendingMessageBizConfig;
import cn.axzo.msg.center.service.domain.UrlConfig;
import cn.axzo.msg.center.service.dto.ButtonRouterDTO;
import cn.axzo.msg.center.service.dto.DetailRouterDTO;
import cn.axzo.msg.center.service.dto.MessageCardContentItemDTO;
import cn.axzo.msg.center.service.enums.BizDetailJumpStrategyEnum;
import cn.axzo.msg.center.service.enums.BizDetailShowStrategyEnum;
import cn.axzo.msg.center.service.enums.KVContentType;
import cn.axzo.msg.center.service.enums.RouterCategoryEnum;
import cn.axzo.msg.center.service.enums.TerminalTypeEnum;
import cn.axzo.msg.center.service.pending.ClientRequest;
import cn.axzo.msg.center.service.pending.ModelVersion;
import cn.axzo.msg.center.service.pending.response.PendingMessageResponse;
import cn.axzo.msg.center.service.pending.response.v3.ParsedModel3Visitor;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedButtonV3;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedGroupV3;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedKV;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedTemplateV3;
import cn.axzo.msg.center.service.pending.response.v3.model.SessionUrlSelector;

import javax.annotation.Nullable;
import java.util.ArrayList;

/**
 * @author yanglin
 */
public class ModelV2PropsPopulator implements ParsedModel3Visitor {

    private final PendingMessageBizConfig cfg;
    private final PendingMessageResponse response;
    @Nullable private final ClientRequest request;
    @Nullable private final SessionUrlSelector sessionUrlSelector;

    public ModelV2PropsPopulator(PendingMessageBizConfig cfg,
                                 PendingMessageResponse response,
                                 @Nullable ClientRequest request) {
        this.cfg = cfg;
        this.response = response;
        this.request = request;
        this.sessionUrlSelector = request == null
                ? null
                : new SessionUrlSelector(request);
    }

    @Override
    public void visitGroupKeyValue(ParsedGroupV3 group, ParsedKV kv) {
        if (kv.getContentType() == KVContentType.ATTACHMENT) return;
        MessageCardContentItemDTO cardItem = new MessageCardContentItemDTO();
        cardItem.setLabel(kv.getKey());

        if (kv.getContentType() == KVContentType.TEXT) {
            cardItem.setValue(kv.getValue());
        } else if (kv.getContentType() == KVContentType.PERSON_ID) {
            if (kv.getPersonInfo() != null)
                cardItem.setValue(kv.getPersonInfo().getPlainPersonInfo());
        } else if (kv.getContentType() == KVContentType.PHONE) {
            if (kv.getPhoneInfo() != null)
                cardItem.setValue(kv.getPhoneInfo().getMaskPhone());
        }
        if (response.getCardItems() == null)
            response.setCardItems(new ArrayList<>());
        response.getCardItems().add(cardItem);
    }

    @Override
    public void visitButton(ParsedButtonV3 buttonV3) {
        if (buttonV3.getCategory() == RouterCategoryEnum.ACTION) {
            ButtonRouterDTO buttonV2 = addButtonV2(buttonV3);
            buttonV2.setUrl(buttonV3.getApiUrl());
        }
    }

    @Override
    public void visitButtonUrlConfig(ParsedButtonV3 buttonV3, UrlConfig urlConfig) {
        if (buttonV3.getCategory() != RouterCategoryEnum.JUMP)
            return;
        if (sessionUrlSelector == null) return;
        ButtonRouterDTO buttonV2 = addButtonV2(buttonV3);
        sessionUrlSelector.selectFrom(urlConfig).ifPresent(session -> {
            buttonV2.setUrl(session.getUrl().getUrl());
            buttonV2.setTerminalType(TerminalTypeEnum.WEB_VIEW);
        });
    }

    private ButtonRouterDTO addButtonV2(ParsedButtonV3 buttonV3) {
        ButtonRouterDTO buttonV2 = new ButtonRouterDTO();
        buttonV2.setDesc(buttonV3.getName());
        buttonV2.setCategory(buttonV3.getCategory());
        buttonV2.setPresetButtonType(buttonV3.getPresetButtonType());
        buttonV2.setStyle(buttonV3.parseStyle());
        buttonV2.setExecutorShow(buttonV3.getExecutorShow());
        buttonV2.setPendingShow(buttonV3.getPendingShow());
        buttonV2.setKey(buttonV3.getCode());
        buttonV2.setSource(buttonV3.getSource());
        if (response.getButtonRouters() == null)
            response.setButtonRouters(new ArrayList<>());
        response.getButtonRouters().add(buttonV2);
        return buttonV2;
    }

    @Override
    public void visitTemplateCardUrlConfig(UrlConfig urlConfig) {
        if (sessionUrlSelector == null || !urlConfig.hasUrl()) return;
        sessionUrlSelector.selectFrom(urlConfig).ifPresent(session -> {
            DetailRouterDTO detail = new DetailRouterDTO();
            response.setDetailRouter(detail);
            detail.setShowStrategy(BizDetailShowStrategyEnum.JUMP_TO);
            detail.setJumpStrategy(BizDetailJumpStrategyEnum.BUSINESS);
            detail.setUrl(session.getUrl().getUrl());
            detail.setTerminalType(TerminalTypeEnum.WEB_VIEW);
        });
    }

    @Override
    public void exitTemplate(ParsedTemplateV3 template) {
        adaptTodoDetailPageForV2NativeApp();
    }

    private void adaptTodoDetailPageForV2NativeApp() {
        if (response.getDetailRouter() != null) return;
        if (!cfg.isAdaptTodoDetailPageForNativeApp()) return;
        ModelVersion version = ModelVersion.from(request);
        if (version.isNot(ModelVersion.V2)) return;
        DetailRouterDTO detail = new DetailRouterDTO();
        response.setDetailRouter(detail);
        detail.setShowStrategy(BizDetailShowStrategyEnum.JUMP_TO);
        detail.setJumpStrategy(BizDetailJumpStrategyEnum.BUSINESS);
        detail.setUrl(String.format("%s?identityCode=%s",
                cfg.getTodoDetailUrl(), response.getIdentityCode()));
        detail.setTerminalType(TerminalTypeEnum.WEB_VIEW);
    }

}