package com.owrawww.service;

import com.owrawww.domain.Bbs;
import com.owrawww.domain.mapper.BbsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class BbsService {

    private static final int PAGE_SIZE  = 12;
    private static final int PAGE_BLOCK = 5;

    private final BbsMapper bbsMapper;

    public Map<String, Object> getList(int page, String searchType, String keyword, int topCode, int leftCode, int subGubun) {
        if (page < 1) page = 1;

        Map<String, Object> params = new HashMap<>();
        params.put("size",       PAGE_SIZE);
        params.put("offset",     (page - 1) * PAGE_SIZE);
        params.put("topCode",    topCode);
        params.put("leftCode",   leftCode);
        params.put("subGubun",   subGubun);
        params.put("searchType", searchType);
        params.put("keyword",    keyword);

        List<Bbs> list       = bbsMapper.selectList(params);
        int       totalCount = bbsMapper.selectCount(params);
        int       totalPages = Math.max(1, (int) Math.ceil((double) totalCount / PAGE_SIZE));

        if (page > totalPages) page = totalPages;

        int blockStart = ((page - 1) / PAGE_BLOCK) * PAGE_BLOCK + 1;
        int blockEnd   = Math.min(blockStart + PAGE_BLOCK - 1, totalPages);

        Map<String, Object> model = new HashMap<>();
        model.put("list",        list);
        model.put("totalCount",  totalCount);
        model.put("currentPage", page);
        model.put("totalPages",  totalPages);
        model.put("blockStart",  blockStart);
        model.put("blockEnd",    blockEnd);
        model.put("searchType",  searchType);
        model.put("keyword",     keyword);
        model.put("topCode",     topCode);
        model.put("leftCode",    leftCode);
        model.put("subGubun",    subGubun);
        return model;
    }

    public Map<String, Object> getDetail(Long id, int topCode, int leftCode, int subGubun, int page, String searchType, String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("id",       id);
        params.put("topCode",  topCode);
        params.put("leftCode", leftCode);
        params.put("subGubun", subGubun);
        params.put("page",     page);
        params.put("searchType", searchType);
        params.put("keyword",    keyword);

        bbsMapper.incrementViewCnt(id); 

        Bbs bbs  = bbsMapper.selectById(params);
        Bbs prev = bbsMapper.selectPrev(params);
        Bbs next = bbsMapper.selectNext(params);

        Map<String, Object> model = new HashMap<>();
        model.put("bbs",        bbs);
        model.put("prev",       prev);
        model.put("next",       next);
        model.put("currentPage", page); 
        model.put("searchType",  searchType);   
        model.put("keyword",     keyword);
        return model;
    }

    public List<Bbs> getPreview(int topCode, int leftCode, int subGubun, int limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("topCode",  topCode);
        params.put("leftCode", leftCode);
        params.put("subGubun", subGubun);
        params.put("limit",    limit);
        return bbsMapper.selectPreview(params);
    }
}