/**
 * 
 */
package org.springblade.etl.connector.sink;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.sink.config.SeatunnelPreviewSinkConfig;
import org.springblade.etl.properties.PreviewRedisProperties;
import org.springblade.etl.util.EtlJsonUtil;
import org.springblade.etl.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author wookvn
 *
 */
@Component
public class PlatomixPreviewSink implements SeatunnelConnector<SeatunnelPreviewSinkConfig> {

	@Autowired
	private PreviewRedisProperties previewRedisConfig;
	
	@Data
	public static class Config{
		@ApiModelProperty("预览条数，默认：10")
		@Min(value = 1, message = "预览：数据条数不能小于1")
		@Max(value = 100, message = "预览：数据条数不能大于100")
		private Integer maxSize = 10;
		
		@NotEmpty(message = "预览：key不能为空")
		@ApiModelProperty("随机字符串，比如：UUID，保证不重复即可【不要包含特殊字符】")
		private String key;
	}
	
	@Override
	public SeatunnelPreviewSinkConfig buildConfig(String configJson) {
		return (SeatunnelPreviewSinkConfig)validate(configJson);
	}

	@Override
	public Object validate(String configJson) {
		Config ptConfig = EtlJsonUtil.parseJsonString(configJson, PlatomixPreviewSink.Config.class);
		ValidateUtil.validate(ptConfig);
		SeatunnelPreviewSinkConfig sinkConfig = new SeatunnelPreviewSinkConfig();
		String node1 = previewRedisConfig.getNodes().get(0);
		String host = node1.split(":")[0];
		Integer port = Integer.valueOf(node1.split(":")[1]);
		sinkConfig.setHost(host);
		sinkConfig.setPort(port);
		sinkConfig.setKey(ptConfig.getKey());
		sinkConfig.setAuth(previewRedisConfig.getPassword());
		sinkConfig.setMax(ptConfig.getMaxSize());
		sinkConfig.setMode("cluster");
		sinkConfig.setNodes(previewRedisConfig.getNodes());
		ValidateUtil.validate(sinkConfig);
		return sinkConfig;
	}

}
