package com.eactive.httpmockserver.common.search;

import com.eactive.httpmockserver.common.util.SpecificationUtil;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.ObjectUtils;

import java.util.Collection;

public class ColumnSearchModel implements SearchModel {

    private String column;

    private Object value;  //값의 형태는 String, Collection, Object 등이 될 수 있다.

    private SearchCondition condition;

    public ColumnSearchModel(String column, SearchCondition condition, Object value) {
        this.column = column;
        this.condition = condition;
        this.value = value;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public SearchCondition getCondition() {
        return condition;
    }

    public void setCondition(SearchCondition condition) {
        this.condition = condition;
    }

    public Object getValue() {
        return value;
    }

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

    public Specification getSpecification() {

        if (condition == null) return null;
        if (column == null) return null;
        if (ObjectUtils.isEmpty(value)) return null;

        if (condition.equals(SearchCondition.EQUAL)) {
            return SpecificationUtil.equalTo(column, value);
        } else if (condition.equals(SearchCondition.LIKE)) {
            return SpecificationUtil.like(column, value.toString());
        } else if (condition.equals(SearchCondition.IN)) {
            return SpecificationUtil.in(column, (Collection) value);
        } else if (condition.equals(SearchCondition.NOT_EQUAL)) {
            return SpecificationUtil.notEqualTo(column, value);
        }

        return null;
    }
}
