package com.ym.mec.education.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ym.mec.education.enums.ReturnCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.snaker.engine.access.Page;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @version V1.0
 * @Description: TODO
 * @date Date : 2019年09月25日 17:53
 */
public class PageResponse<T> extends Response implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<T> records;
    private Integer total;
    private Integer size;
    private Integer pages;
    private Integer current;
    private boolean searchCount;
    private boolean optimizeCount;
    private String orderByField;
    private boolean isAsc;

    public PageResponse() {
        this.records = Collections.emptyList();
        this.size = 10;
        this.current = 1;
        this.searchCount = true;
        this.optimizeCount = false;
        this.isAsc = true;
    }

    public static PageResponse success(IPage page) {
        PageResponse pageResponse = new PageResponse(Math.toIntExact(page.getCurrent()), Math.toIntExact(page.getSize()));
        pageResponse.setReturnCode(ReturnCodeEnum.CODE_200.getCode());
        pageResponse.setMessage(ReturnCodeEnum.CODE_200.getValue());
        pageResponse.setRecords(page.getRecords());
        pageResponse.setTotal(Math.toIntExact(page.getTotal()));
        return pageResponse;
    }

    public static PageResponse success(Page page) {
        PageResponse pageResponse = new PageResponse(page.getPageNo(), page.getPageSize());
        pageResponse.setReturnCode(ReturnCodeEnum.CODE_200.getCode());
        pageResponse.setMessage(ReturnCodeEnum.CODE_200.getValue());
        pageResponse.setRecords(page.getResult());
        pageResponse.setTotal(Math.toIntExact(page.getTotalCount()));
        return pageResponse;
    }

    public PageResponse(Integer current, Integer size) {
        this(current, size, true);
    }

    public PageResponse(Integer current, Integer size, String orderByField) {
        this.records = Collections.emptyList();
        this.setOrderByField(orderByField);
    }

    public PageResponse(Integer current, Integer size, boolean searchCount) {
        this.records = Collections.emptyList();
        this.size = 10;
        this.current = 1;
        this.searchCount = true;
        this.optimizeCount = false;
        this.isAsc = true;
        if (current > 1) {
            this.current = current;
        }

        this.size = size;
        this.searchCount = searchCount;
    }

    public List<T> getRecords() {
        return this.records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    protected static Integer offsetCurrent(Integer current, Integer size) {
        return current > 0 ? (current - 1) * size : 0;
    }

    public Integer getOffsetCurrent() {
        return offsetCurrent(this.current, this.size);
    }

    public boolean hasPrevious() {
        return this.current > 1;
    }

    public boolean hasNext() {
        return this.current < this.pages;
    }

    public Integer getTotal() {
        return this.total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getSize() {
        return this.size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getPages() {
        if (this.size != null && 0 != this.size && this.total != null) {
            this.pages = this.total / this.size;
            if (this.total % this.size != 0) {
                this.pages = this.pages + 1;
            }

            return this.pages;
        } else {
            return null;
        }
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public Integer getCurrent() {
        return this.current;
    }

    public boolean isSearchCount() {
        return this.searchCount;
    }

    public void setSearchCount(boolean searchCount) {
        this.searchCount = searchCount;
    }

    public boolean isOptimizeCount() {
        return this.optimizeCount;
    }

    public void setOptimizeCount(boolean optimizeCount) {
        this.optimizeCount = optimizeCount;
    }

    public String getOrderByField() {
        return this.orderByField;
    }

    public void setOrderByField(String orderByField) {
        if (StringUtils.isNotEmpty(orderByField)) {
            this.orderByField = orderByField;
        }

    }

    public boolean isAsc() {
        return this.isAsc;
    }

    public void setAsc(boolean isAsc) {
        this.isAsc = isAsc;
    }

    @Override
    public String toString() {
        StringBuffer pg = new StringBuffer();
        pg.append(" Page:{ [").append(super.toString()).append("], ");
        if (this.records != null) {
            pg.append("records-size:").append(this.records.size());
        } else {
            pg.append("records is null");
        }

        return pg.append(" }").toString();
    }

    public static PageResponse errorParam(){
        PageResponse pageResponse = new PageResponse();
        pageResponse.setReturnCode(ReturnCodeEnum.CODE_206.getCode());
        pageResponse.setMessage(ReturnCodeEnum.CODE_206.getValue());
        return pageResponse;
    }

    public static PageResponse noDataExists() {
        PageResponse pageResponse = new PageResponse();
        pageResponse.setReturnCode(ReturnCodeEnum.CODE_207.getCode());
        pageResponse.setMessage(ReturnCodeEnum.CODE_207.getValue());
        return pageResponse;
    }
}
