package com.friday.tooldriver.dto.uipath.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UiPathUrlParam implements Serializable {
    private String expand;
    private String filter;
    private String select;
    private String orderBy;
    private int top;
    private int skip;
    private boolean count;
    private String id;

    private UiPathUrlParam(Builder builder) {
        this.count = builder.count;
        this.skip = builder.skip;
        this.top = builder.top;
        this.expand = builder.expand;
        this.filter = builder.filter;
        this.orderBy = builder.orderBy;
        this.select = builder.select;
        this.id = builder.id;
    }

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

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (top > 0) {
            sb.append("$top=").append(this.top);
        }
        if (skip > 0) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append("$skip=").append(this.skip);
        }
        if (StringUtils.isNotEmpty(this.expand)) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append("$expand=").append(this.expand);
        }
        if (count) {
            sb.setLength(0);
            sb.append("&count=").append(this.count);
        }
        if (StringUtils.isNotEmpty(this.filter)) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append("$filter=").append(this.filter);
        }
        if (StringUtils.isNotEmpty(this.select)) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append("$select=").append(this.select);
        }
        if (StringUtils.isNotEmpty(this.orderBy)) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append("$orderby=").append(this.orderBy);
        }
        return sb.toString();
    }

    public static class Builder {
        private String expand;
        private String filter;
        private String select;
        private String orderBy;
        private int top;
        private int skip;
        private boolean count;
        private String id;

        public Builder expand(ExpandEntity expandEntity) {
            this.expand = expandEntity.toString();
            return this;
        }

        public Builder filter(FilterEntity filterEntity) {
            this.filter = filterEntity.toString();
            return this;
        }

        public Builder select(List<String> selectEntity) {
            this.select = String.join(",", selectEntity);
            return this;
        }

        public Builder orderBy(OrderByEntity orderByEntity) {
            this.orderBy = orderByEntity.toString();
            return this;
        }

        public Builder top(int top) {
            this.top = top;
            return this;
        }

        public Builder skip(int skip) {
            this.skip = skip;
            return this;
        }

        public Builder count(boolean count) {
            this.count = count;
            return this;
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public UiPathUrlParam build() {
            return new UiPathUrlParam(this);
        }
    }
}