package com.its.web.service.notice;

import com.its.web.dto.notice.AttachFileDto;
import com.its.web.dto.notice.NoticeDto;
import com.its.web.mapper.its.notice.NoticeMapper;
import com.its.web.pagination.Pagination;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Slf4j
@RequiredArgsConstructor
@Service
public class NoticeService {
    private final NoticeMapper mapper;

    @Value("${board-location}")
    String boardLocation;

    public Pagination findAllList(String page, String searchType, String searchText) {
        Pagination pagination = new Pagination(page, this.mapper.getNoticeTotalCount(), searchType, searchText);
        List<NoticeDto> list = this.mapper.findAllNotice(pagination);
        pagination.setList(list);
        return pagination;
    }

    public NoticeDto findNotice(String boardNo) {
        try {
            return this.mapper.findNoticeById(Long.parseLong(boardNo));
        }
        catch (NumberFormatException e){
            log.error("Cause NumberFormatException, Please Check The Board No -> {}", boardNo);
            return null;
        }
    }

    public List<NoticeDto> findMainNotice(int num) {
        return this.mapper.findMainNotice(num);
    }

    public AttachFileDto getAttachFile(String boardNo, String fileId, String fileName) {
        AttachFileDto dto = new AttachFileDto();
        dto.setBoardNo(boardNo);
        String message = null;
        if (boardNo != null && fileId != null) {
            Map<String, String> paramMap = new HashMap<>();

            paramMap.put("boardNo", boardNo);
            paramMap.put("fileId", fileId);
            int result = this.mapper.getAttachFileCount(paramMap);
            if (result > 0) {
                log.info("boardLocation : {}", boardLocation);
                File file = new File(boardLocation, fileId);
                int fileSize = (int) file.length();

                if (fileSize > 0) {
                    try {
                        byte[] byteArray = FileUtils.readFileToByteArray(file);
                        if (byteArray.length > 0) {
                            int i = 0;
                            dto.setAttachFile(new byte[byteArray.length]);
                            for(byte b : byteArray) {
                                dto.getAttachFile()[i++] = b;
                            }
                            message = "파일 전송 완료.";
                        }
                        else {
                            message = "파일을 찾을 수 없습니다.";
                        }
                    }
                    catch (FileNotFoundException e) {
                        log.error("File Not FoundException");
                        message = "파일을 찾을 수 없습니다.";
                    } catch (IOException e) {
                        log.error("IOException");
                        message = "파일을 읽는 중 오류가 발생하였습니다.";
                    }
                }
            }
        }
        dto.setMessage(message);
        return dto;
    }
}
