package com.its.web.service.common;

import com.its.web.dto.common.CodeDto;
import com.its.web.dto.common.ConnStatisticsDto;
import com.its.web.dto.common.TbWwwOrgDto;
import com.its.web.mapper.its.common.CommonMapper;
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 javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
@Service
public class CommonService {
    private final CommonMapper mapper;

    @Value("${image-location}")
    String imageLocation;

    /**
     * 공공기관 사이트 URL
     * @return List<TbWwwOrgDto>
     */
    public List<TbWwwOrgDto> findAllOrganization() {
        return this.mapper.findAllOrganization();
    }

    /**
     * Day Code 리스트
     * @return List<CodeDto>
     */
    public List<CodeDto> findDayList() {
        return this.mapper.findDayList();
    }

    /**
     * 접속자 조회 통계
     * @return List<ConnStatisticsDto>
     */
    public List<ConnStatisticsDto> getConnStatistics(Map<String, String> paramMap) {
        String type = paramMap.get("type");
        if ("week".equals(type)) {
            return this.mapper.getConnWeekStatistics(paramMap);
        }
        else if ("month".equals(type)) {
            return this.mapper.getConnMonthStatistics(paramMap);
        }
        return this.mapper.getConnStatistics(paramMap);
    }

    /**
     * 접속자 이력 등록
     * @return DB 등록 수
     */
    public int insertConnHs() { return this.mapper.insertConnHs(); }


    public String imageUpload(HttpServletRequest req) {
        String uploadImageInfo = "";
        String fileName = req.getHeader("file-name");
        try {
            fileName = URLDecoder.decode(fileName, "UTF-8");
        }
        catch (UnsupportedEncodingException exception) {
            log.error("Can not Decoding UTF-8");
        }
        String filePath = imageLocation;
        File file = new File(filePath);
        boolean isMkdirs = true;
        if (!file.exists()) {
            isMkdirs = file.mkdirs();
        }

        if (isMkdirs) {
            String realFileNm;
            realFileNm = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
            String rlFileNm = filePath + File.separator + realFileNm;

            try (
                    InputStream is = req.getInputStream();
                    OutputStream os = new FileOutputStream(rlFileNm);
            ) {
                int numRead;
                byte[] byteArray = new byte[Integer.parseInt(req.getHeader("file-size"))];
                if (is != null) {
                    while ((numRead = is.read(byteArray, 0, byteArray.length)) != -1) {
                        os.write(byteArray, 0, numRead);
                    }
                }
                os.flush();
            } catch (IOException e) {
                log.error("noticeImgupload() IO Exception");
            } catch (Exception e1) {
                log.error("noticeImgupload() Exception");
            }
            uploadImageInfo += "&bNewLine=true";
            uploadImageInfo += "&sFileName=" + fileName;
            uploadImageInfo += "&sFileURL=" + "/api/common/upload/" + realFileNm;
        }

        return uploadImageInfo;
    }

    public byte[] getImage(String imageName) {
        byte[] image = null;
        File file = new File(imageLocation, imageName);
        int fileSize = (int) file.length();
        if (fileSize > 0) {
            try {
                byte[] byteArray = FileUtils.readFileToByteArray(file);
                if (byteArray.length > 0) {
                    int ii = 0;
                    image = new byte[byteArray.length];
                    for (byte b : byteArray) {
                        image[ii++] = b;
                    }
                }
            } catch (FileNotFoundException e) {
                log.error("Not Found File - {}", imageLocation + imageName);
            } catch (IOException e) {
                log.error("Can Not Send Image Cause IOException - {}", imageLocation + imageName);
            }
        }
        return image;
    }
}
