package com.handong.smartservice.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.mapper.ResourceBoardMapper;


@Service
public class ResourceBoardService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ResourceBoardMapper resourceBoardMapper;


    public ResourceBoardService(ResourceBoardMapper resourceBoardMapper) {
        this.resourceBoardMapper = resourceBoardMapper;
    }

    public CtResponse getResourceBoardDetail(String resource_board_id) {
        CtResponse result = new CtResponse();

        Map<String, Object> detail = resourceBoardMapper.selectDetailResourceBoard(resource_board_id);

        if (resource_board_id == null || resource_board_id.isEmpty()) {
            result.setErrCode(ErrorCode.INVALID_PARAMETER);
            return result;
        }

        result.put("result", detail);
        return result;
    }

    public Map<String, Object> getResourceBoardList(Long offset, Long limit, String date_start, String date_end, 
                String title, String content) {
        Map<String, Object> mapResult = new HashMap<>();

        List<Map<String, Object>> listMap = (List<Map<String, Object>>)resourceBoardMapper.selectResourceBoard(false, offset, limit,
            date_start, date_end, title, content);
        mapResult.put("list", listMap);
        
        //Long totalCount = _AG.getTotalCount(resourceBoardMapper.selectResourceBoard(true, offset, limit, 
        //    date_start, date_end, title, content));
        Long totalCount = _AG.getNumberFromQuery("COUNT(*)", resourceBoardMapper.selectResourceBoard(true, offset, limit, 
            date_start, date_end, title, content));
        mapResult.put("totalCount", totalCount);
        mapResult.put("offset", offset);
        return mapResult;
    }

    public CtResponse addOrModifyResourceBoard(boolean isModify, String resource_board_id, String title, String content) {
        CtResponse result = new CtResponse();

        if (isModify) {
            if (resource_board_id == null || resource_board_id.isEmpty()) {
                result.setErrCode(ErrorCode.INVALID_PARAMETER);
                return result;
            }

            resourceBoardMapper.updateResourceBoard(_AG.toLong(resource_board_id), title, content);
        }
        else {
            resourceBoardMapper.insertResourceBoard(title, content, 2L);
        }
        
        return result;
    }

    public CtResponse removeResourceBoard(String ids) {
        CtResponse result = new CtResponse();

        List<Long> arrId = Arrays.stream(ids.split(","))
            .map(Long::parseLong)
            .collect(Collectors.toList());
        
        int changed = resourceBoardMapper.changeStateResourceBoard(arrId, 2);
        //if (changed == 0) {
            //result.setErrCode(ErrorCode.QUERY_ERROR);
        //}
        
        return result;
    }
}
