package com.arms.egovframework.javaservice.esframework.model.dto.request;

import com.arms.egovframework.javaservice.esframework.model.dto.esquery.CompositeSourceDTO;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CompositeRequestDTO {

	private List<CompositeSourceDTO> sources = new ArrayList<>();

	private Map<String, Object> afterKey;

	private int hitSize = 1;

	private String sortField;

	private boolean sortAscending;

	private String[] includeFields;

	private int size = 10000;

	private boolean contentView;

	public static CompositeRequestDTOBuilder builder() {
		return new CompositeRequestDTOBuilder();
	}

	public static class CompositeRequestDTOBuilder {
		private List<CompositeSourceDTO> sources = new ArrayList<>();
		private Map<String, Object> afterKey;
		private int hitSize = 1;
		private String sortField;
		private boolean sortAscending = false;
		private String[] includeFields;
		private int size = 10000;
		private boolean contentView = false;

		public CompositeRequestDTOBuilder sources(List<CompositeSourceDTO> sources) {
			this.sources = sources;
			return this;
		}

		public CompositeRequestDTOBuilder addSource(CompositeSourceDTO... source) {
			if (this.sources == null) {
				this.sources = new ArrayList<>();
			}
			this.sources.addAll(Arrays.asList(source));
			return this;
		}

		public CompositeRequestDTOBuilder afterKey(Map<String, Object> afterKey) {
			this.afterKey = afterKey;
			return this;
		}

		public CompositeRequestDTOBuilder hitSize(int hitSize) {
			this.hitSize = hitSize;
			return this;
		}

		public CompositeRequestDTOBuilder sortField(String sortField) {
			this.sortField = sortField;
			return this;
		}

		public CompositeRequestDTOBuilder sortAscending(boolean sortAscending) {
			this.sortAscending = sortAscending;
			return this;
		}

		public CompositeRequestDTOBuilder includeFields(String... includeFields) {
			this.includeFields = includeFields;
			return this;
		}

		public CompositeRequestDTOBuilder size(int size) {
			this.size = size;
			return this;
		}

		public CompositeRequestDTOBuilder contentView(boolean contentView) {
			this.contentView = contentView;
			return this;
		}

		public CompositeRequestDTO build() {
			CompositeRequestDTO dto = new CompositeRequestDTO();
			dto.setSources(this.sources);
			dto.setAfterKey(this.afterKey);
			dto.setHitSize(this.hitSize);
			dto.setSortField(this.sortField);
			dto.setSortAscending(this.sortAscending);
			dto.setIncludeFields(this.includeFields);
			dto.setSize(this.size);
			dto.setContentView(this.contentView);
			return dto;
		}
	}
}
