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

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

import org.springblade.etl.connector.base.SeatunnelConfig.SchemaField;
import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.source.config.SeatunnelHttpSourceConfig;
import org.springblade.etl.connector.source.config.SeatunnelHttpSourceConfig.PageParam;
import org.springblade.etl.connector.source.enums.FieldType;
import org.springblade.etl.util.EtlJsonUtil;
import org.springblade.etl.util.ValidateUtil;
import org.springframework.stereotype.Component;

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

/**
 * @author wookvn
 *
 */
@Component
public class PlatomixHttpSource implements SeatunnelConnector<SeatunnelHttpSourceConfig> {
	
	@Data
	@ApiModel("HTTP-Source配置信息")
	public static class Config {
		@ApiModelProperty("URL地址")
		@NotNull(message = "URL地址不能为空")
		private String url;
		
		@NotEmpty(message = "HTTP响应字段不能为空")
		@ApiModelProperty("HTTP-Source字段信息")
		private List<Field> fields;
		
		@NotEmpty(message = "HTTP请求方式不能为空")
		@ApiModelProperty("请求方式[GET/POST]")
		private String method;
		
		@ApiModelProperty("请求头")
		private Map<String, String> headers;
		
		@ApiModelProperty("请求参数")
		private Map<String, String> params;
		
		@ApiModelProperty("请求体数据")
		private String body;
		
		@ApiModelProperty("深度解析json字符串[要求解析的值必须是string]")
		private List<String> deepParseFields;
		
		@ApiModelProperty("分页高级属性")
		private PageParam pageParam;
	}
	
	@Data
	public static class Field {
		@ApiModelProperty("字段名")
		private String name;
		@ApiModelProperty("jsonpath路径")
		private String jsonpath;
		@ApiModelProperty("字段类型")
		private FieldType type; 
	}

	@Override
	public SeatunnelHttpSourceConfig buildConfig(String configJson) {
		Config ptConfig = (Config)validate(configJson);
		SeatunnelHttpSourceConfig seatunnelConfig = new SeatunnelHttpSourceConfig();
		seatunnelConfig.setUrl(ptConfig.getUrl());
		seatunnelConfig.setBody(ptConfig.getBody());
		seatunnelConfig.setHeaders(ptConfig.getHeaders());
		seatunnelConfig.setParams(ptConfig.getParams());
		seatunnelConfig.setMethod(ptConfig.getMethod());
		seatunnelConfig.setDeepParseFields(ptConfig.getDeepParseFields());
		seatunnelConfig.setPageParam(ptConfig.getPageParam());
		Map<String, String> fieldJson = new LinkedHashMap<>();
		Map<String, FieldType> fields = new LinkedHashMap<>();
		ptConfig.getFields().forEach(item -> {
			fields.put(item.getName(), item.getType());
			fieldJson.put(item.getName(), item.getJsonpath());
		});
		seatunnelConfig.setJsonField(fieldJson);
		seatunnelConfig.setSchema(new SchemaField(fields));
		return seatunnelConfig;
	}

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