
package cn.orionsec.ops.entity.importer;

import cn.orionsec.kit.lang.utils.convert.TypeStore;
import cn.orionsec.kit.office.excel.annotation.ImportField;
import cn.orionsec.ops.constant.webhook.WebhookType;
import cn.orionsec.ops.entity.domain.WebhookConfigDO;
import cn.orionsec.ops.entity.vo.data.DataImportCheckRowVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Optional;

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "webhook 导入")
public class WebhookImportDTO extends BaseDataImportDTO {

    @ApiModelProperty(value = "名称")
    @ImportField(index = 0)
    private String name;

    /**
     * @see WebhookType
     */
    @ApiModelProperty(value = "类型")
    @ImportField(index = 1)
    private String type;

    @ApiModelProperty(value = "url")
    @ImportField(index = 2)
    private String url;

    static {
        TypeStore.STORE.register(WebhookImportDTO.class, DataImportCheckRowVO.class, p -> {
            DataImportCheckRowVO vo = new DataImportCheckRowVO();
            vo.setSymbol(p.name);
            vo.setIllegalMessage(p.getIllegalMessage());
            vo.setId(p.getId());
            return vo;
        });
        TypeStore.STORE.register(WebhookImportDTO.class, WebhookConfigDO.class, p -> {
            WebhookConfigDO d = new WebhookConfigDO();
            d.setId(p.getId());
            d.setWebhookName(p.name);
            d.setWebhookUrl(p.url);
            Optional.ofNullable(p.type)
                    .map(WebhookType::of)
                    .map(WebhookType::getType)
                    .ifPresent(d::setWebhookType);
            return d;
        });
    }

}
