package com.takensoft.common;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class Pagination {
    private int currentPage = 1;    // 현재 페이지
    private int recordSize = 10;    // 한 페이지에 표시할 데이터 개수
    private int pageSize = 10;      // 하단에 표시할 페이지 번호 개수

    private int totalRecordCount;   // 전체 데이터 개수
    private int totalPageCount;     // 전체 페이지 개수
    private int startPage;          // 첫 페이지 번호
    private int endPage;            // 끝 페이지 번호
    private int limitStart;         // LIMIT 시작 위치
    private boolean existPrevPage;  // 이전 페이지 존재 여부
    private boolean existNextPage;  // 다음 페이지 존재 여부
    
    private String searchType;      // 검색조건
    private String searchText;      // 검색어

    private String startDt;         // 시작일
    private String endDt;           // 종료일

    private String id;              // 게시판 아이디 (게시판 내용 목록 조회 조건시 사용)

    private String cateId;          // 상세 분류 아이디
    private String cateValue;       // 상세 분류 내용

    private String mvnInten;        // 입주의향 (기업정보 전용)
    private String mouInten;        // MOU의향 (기업정보 전용)

    private String type;            // 형태 (검토사항 전용)
    private String prgrsCrs;        // 진행단계 (검토사항 전용)
    
    private String mbrId;           // 멤버 Id (권한 확인 용도)
    private String isAdmin;        // 멤버 권한 관리자 여부 (권한 확인 용도)

    public Pagination(int totalRecordCount, Map<String, String> params) {
        this.currentPage = Integer.parseInt(params.getOrDefault("currentPage", "1"));
        this.recordSize = Integer.parseInt(params.getOrDefault("recordSize", "1"));
        this.pageSize = Integer.parseInt(params.getOrDefault("pageSize", "10"));
        this.searchType = (String)params.get("searchType");
        this.searchText = (String)params.get("searchText");
        this.startDt = (String)params.get("startDt");
        this.endDt = (String)params.get("endDt");
        this.id = (String)params.get("id");
        this.cateId = (String)params.get("cateId");
        this.cateValue = (String)params.get("cateValue");
        this.mvnInten = (String)params.get("mvnInten");
        this.mouInten = (String)params.get("mouInten");
        this.type = (String)params.get("type");
        this.prgrsCrs = (String)params.get("prgrsCrs");
        this.mbrId = (String)params.get("mbrId");
        this.isAdmin = (String)params.get("isAdmin");

        if (totalRecordCount > 0) {
            // 전체 데이터 개수
            this.totalRecordCount = totalRecordCount;

            // 전체 페이지 개수
            this.totalPageCount = ((this.totalRecordCount - 1) / this.recordSize) + 1;
            if (this.currentPage > this.totalPageCount) {
                this.currentPage = this.totalPageCount;
            }

            // 첫 페이지 번호
            this.startPage = ((this.currentPage - 1) / this.pageSize) * this.pageSize + 1;

            // 끝 페이지 번호
            this.endPage = this.startPage + this.pageSize - 1;
            if (this.endPage > this.totalPageCount) {
                this.endPage = this.totalPageCount;
            }

            // LIMIT 시작 위치
            this.limitStart = (this.currentPage - 1) * this.recordSize;

            // 이전 페이지 존재 여부
            this.existPrevPage = this.startPage != 1;

            // 다음 페이지 존재 여부
            this.existNextPage = (this.endPage * this.recordSize) < this.totalRecordCount;
        }
    }
}