package com.oqpo.api.web.dto.request;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.service.CommonService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class GridRequest {
    @ApiModelProperty(value = "그리드 사이즈")
    private Integer gridSize;

    @ApiModelProperty(value = "그리드 페이지")
    private Integer gridPage;

    @ApiModelProperty(value = "정렬 컬럼명")
    private String sidx;

    @ApiModelProperty(value = "정렬 방향")
    private String sord;

    @ApiModelProperty(value = "페이징 유무 여부")
    private Boolean pagingYn;

    @JsonIgnore
    @ApiModelProperty(value = "그리드 첫 페이지")
    private Integer gridFirst;

    public static GridRequest toDTO(GridRequest request) {
        CommonService commonService = new CommonService();
        int gridSize = request.gridSize == null || request.gridSize == 0 ? 20 : request.gridSize;
        int gridPage = request.gridPage == null || request.gridPage == 0 ? 1 : request.gridPage;
        return GridRequest.builder()
                .gridSize(gridSize)
                .gridPage(gridPage)
                .sidx(request.sidx)
                .sord(request.sord)
                .pagingYn(request.pagingYn == null ? true : request.pagingYn)
                .gridFirst(commonService.fnCalculateGridFirst(gridPage, gridSize))
                .build();
    }
}
