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

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.source.config.SeatunnelFtpFileSourceConfig;
import org.springblade.etl.connector.source.config.SeatunnelLocalFileSourceConfig;
import org.springblade.etl.util.EtlJsonUtil;
import org.springblade.etl.util.ValidateUtil;
import org.springframework.stereotype.Component;

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

/**
 * @author wookvn
 *
 */
@Component
public class PlatomixFtpFileSource implements SeatunnelConnector<SeatunnelFtpFileSourceConfig> {

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class Config extends PlatomixLocalFileSource.Config{
		@NotEmpty(message = "Source:FtpHost不能为空")
		@ApiModelProperty("ftp的地址")
		private String host;
		@NotNull(message = "Source:Ftp端口不能为空")
		@ApiModelProperty("ftp的端口")
		private Integer port;
		@NotEmpty(message = "Source:Ftp用户名不能为空")
		@ApiModelProperty("ftp的用户名")
		private String user;
		@NotEmpty(message = "Source:Ftp密码不能为空")
		@ApiModelProperty("ftp的密码")
		private String password;
//		@NotEmpty(message = "Source文件路径不能为空")
//		@ApiModelProperty("文件路径,比如:/tmp/sink/text")
//		private String path;
//		@NotNull(message = "Source文件类型不能为空")
//		@ApiModelProperty("文件格式")
//		private FileFormat format;
//		@NotNull(message = "Source字段不能为空")
//		@ApiModelProperty("文件格式")
//		private Map<String, FieldType> fields;
//		@ApiModelProperty("分隔符，主要针对csv和text")
//		private String delimiter;
//		@ApiModelProperty("跳过几行，主要针对excel")
//		private Integer skipHeaderRowNumber;
//		@ApiModelProperty("excel的sheet名称")
//		private String sheetName;
	}

	@Override
	public SeatunnelFtpFileSourceConfig buildConfig(String configJson) {
		Config ptConfig = (Config)validate(configJson);
		SeatunnelFtpFileSourceConfig seatunnelConfig = new SeatunnelFtpFileSourceConfig();
		seatunnelConfig.setDelimiter(ptConfig.getDelimiter());
		seatunnelConfig.setSheetName(ptConfig.getSheetName());
		seatunnelConfig.setFileFormatType(ptConfig.getFormat().name());
		seatunnelConfig.setPath(ptConfig.getPath());
		seatunnelConfig.setSkipHeaderRowNumber(ptConfig.getSkipHeaderRowNumber());
		seatunnelConfig.setSchema(new SeatunnelLocalFileSourceConfig.SchemaField(ptConfig.getFields()));
		seatunnelConfig.setUser(ptConfig.getUser());
		seatunnelConfig.setPassword(ptConfig.getPassword());
		seatunnelConfig.setHost(ptConfig.getHost());
		seatunnelConfig.setPort(ptConfig.getPort());
		return seatunnelConfig;
	}

	@Override
	public Object validate(String configJson) {
		Config ptConfig = EtlJsonUtil.parseJsonString(configJson, PlatomixFtpFileSource.Config.class);
		ValidateUtil.validate(ptConfig);
		return ptConfig;
	}
}
