package com.imcode.search;

import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ruslan on 08.11.16.
 */
public class SearchCriteries {

    public enum Order {ASC, DESC}

    private List<SearchCriteriaResult> searchCriteriaResults;
    private int index = 0;
    private String orderBy;
    private Order order;

    private SearchCriteries() {
    }

    public static <T> SearchCriteries select() {
        SearchCriteries builder = new SearchCriteries();
        builder.searchCriteriaResults = new LinkedList<>();
        return builder;
    }

    public List<SearchCriteriaResult> where(@NotNull SearchCriteria searchCriteria) {
        SearchCriteriaResult result = buildSearchCriteriaResult(searchCriteria);
        if (orderBy != null && order != null) {
            result.orderBy = orderBy;
            result.order = order;
        }
        searchCriteriaResults.add(result);
        return searchCriteriaResults;
    }

    public SearchCriteries whereAnd(@NotNull SearchCriteria searchCriteria) {
        SearchCriteriaResult result = buildSearchCriteriaResult(searchCriteria);
        result.nextAnd = true;
        searchCriteriaResults.add(result);
        return this;
    }

    public SearchCriteries whereOr(@NotNull SearchCriteria searchCriteria) {
        SearchCriteriaResult result = buildSearchCriteriaResult(searchCriteria);
        result.nextAnd = false;
        searchCriteriaResults.add(result);
        return this;
    }

    public SearchCriteries orderBy(@NotNull String orderBy, @NotNull Order order) {
        this.orderBy = orderBy;
        this.order = order;
        return this;
    }

    public SearchCriteriaResult buildSearchCriteriaResult(SearchCriteria searchCriteria) {
        SearchCriteriaResult result = new SearchCriteriaResult();
        result.fieldName = searchCriteria.getFieldName();
        result.operation = searchCriteria.getOperation();
        result.value = searchCriteria.getValue();
        result.nextAnd = null;
        result.index = index++;
        return result;
    }

    public static SearchCriteria statement(@NotNull String fieldName, @NotNull SearchOperation so, @NotNull Object value) {
        return new SearchCriteria(fieldName, so, value);
    }

    public static class SearchCriteriaResult {

        private String fieldName;
        private SearchOperation operation;
        private Object value;
        private Boolean nextAnd;
        private Integer index;
        private String orderBy;
        private Order order;

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public SearchOperation getOperation() {
            return operation;
        }

        public void setOperation(SearchOperation operation) {
            this.operation = operation;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public Boolean getNextAnd() {
            return nextAnd;
        }

        public void setNextAnd(Boolean nextAnd) {
            this.nextAnd = nextAnd;
        }

        public Integer getIndex() {
            return index;
        }

        public void setIndex(Integer index) {
            this.index = index;
        }

        public String getOrderBy() {
            return orderBy;
        }

        public void setOrderBy(String orderBy) {
            this.orderBy = orderBy;
        }

        public Order getOrder() {
            return order;
        }

        public void setOrder(Order order) {
            this.order = order;
        }
    }

    public enum Conjunction {AND, OR}

}

@@ -294,35 +294,8 @@
 ts;%0A
-    private int index = 0;%0A
     
@@ -314,24 +314,24 @@
 ng orderBy;%0A
+
     private 
@@ -2017,40 +2017,8 @@
 ll;%0A
-        result.index = index++;%0A
     
@@ -2423,39 +2423,8 @@
 nd;%0A
-        private Integer index;%0A
     
@@ -3205,170 +3205,8 @@
  %7D%0A%0A
-        public Integer getIndex() %7B%0A            return index;%0A        %7D%0A%0A        public void setIndex(Integer index) %7B%0A            this.index = index;%0A        %7D%0A%0A
     
@@ -3534,52 +3534,13 @@
   %7D%0A
+
     %7D%0A%0A
-    public enum Conjunction %7BAND, OR%7D%0A%0A
 %7D%0A

Fix search criteries.