package com.shape.web.util;

import com.shape.web.entity.FileDB;
import com.shape.web.repository.FileDBRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Slf4j
@Component
public class FileUtil {
    private FileDBRepository fileDBRepository;


    @Autowired
    public FileUtil(FileDBRepository fileDBRepository) {
        this.fileDBRepository = fileDBRepository;
    }

    private enum Header {

        DISPOSITION("Content-disposition"),
        TYPE("Content-Type"),
        LENGTH("Content-Length");

        Header(String headerName) {
            this.headerName = headerName;
        }

        public String getName() {
            return headerName;
        }

        private String headerName;

    }

    public static String getFileExtension(File file) {
        String name = file.getName();
        int lastIndexOf = name.lastIndexOf('.');
        if (lastIndexOf == -1) {
            return ""; // empty extension
        }
        return name.substring(lastIndexOf);
    }

    public static String getFoldername(final int project_id, final Date date) {
        Date dates = date == null ? new Date() : date;
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        String filename = "team/" + format.format(dates);
        filename += "_" + project_id;
        return filename;
    }

    public static void makeMinute(final int project_id, final String memo) {
        String filename = getFoldername(project_id, null);
        try (FileWriter fw = new FileWriter(filename + "/minute.txt")) {
            fw.write(memo);
        } catch (IOException e) {
            log.error("Open File Error ", e);
        }
    }

    public String decodeFile(String storedFileName) {
        FileDB fd = fileDBRepository.findByStoredname(storedFileName);
        return fd.getOriginalname();
    }

    private static boolean isImage(final String filename) {
        String fileExtension = filename.substring(filename.lastIndexOf('.'));
        Pattern pattern = Pattern.compile("\\.(jpg|jpeg|png|gif)$", Pattern.CASE_INSENSITIVE);
        Matcher m = pattern.matcher(fileExtension);
        return m.matches();
    }

    public static String getFileType(String filename) {
        if (isImage(filename))
            return "image";
        else
            return "file";

    }


    public static void setDownloadHeader(final String filename, final File file, final HttpServletRequest request, HttpServletResponse response) {
        String userAgent = request.getHeader("User-Agent");
        try {
            response.setHeader(Header.DISPOSITION.getName(), "attachment;filename=\"" + filename + "\"" + ";");
            if (userAgent.contains("MSIE"))
                response.setHeader(Header.DISPOSITION.getName(), "attachment; filename=" + new String(filename.getBytes("KSC5601"), "ISO8859_1"));
            else {  // IE 이외
                response.setHeader(Header.DISPOSITION.getName(), "attachment; filename=\"" + java.net.URLEncoder.encode(filename, "UTF-8") + "\"");
                response.setHeader(Header.TYPE.getName(), "application/octet-stream; charset=utf-8");    //octet-stream->다운로드 창
            }    //response 헤더 설정해서
            response.setHeader(Header.LENGTH.getName(), "" + file.length());

        } catch (IOException e) {
            log.error("Error ", e);
        }
    }
}


@@ -33,231 +33,33 @@
 ort 
-com.shape.web.entity.FileDB;%0Aimport com.shape.web.repository.FileDBRepository;%0Aimport lombok.extern.slf4j.Slf4j;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.stereotype.Component
+lombok.extern.slf4j.Slf4j
 ;%0A%0Ai
@@ -361,19 +361,8 @@
 f4j%0A
-@Component%0A
 publ
@@ -384,185 +384,8 @@
 il %7B
-%0A    private FileDBRepository fileDBRepository;%0A%0A%0A    @Autowired%0A    public FileUtil(FileDBRepository fileDBRepository) %7B%0A        this.fileDBRepository = fileDBRepository;%0A    %7D
 %0A%0A  
@@ -1052,34 +1052,33 @@
 inal int project
-_i
+I
 d, final Date da
@@ -1226,30 +1226,88 @@
 ring
- filename = %22team/%22 + 
+Builder sb = new StringBuilder();%0A        sb.append(%22team/%22);%0A        sb.append(
 form
@@ -1322,16 +1322,17 @@
 t(dates)
+)
 ;%0A      
@@ -1337,36 +1337,52 @@
     
-filename += %22_%22 + 
+sb.append(%22_%22);%0A        sb.append(
 project
-_id
+Id)
 ;%0A  
@@ -1394,24 +1394,29 @@
  return 
-filename
+sb.toString()
 ;%0A    %7D%0A
@@ -1467,18 +1467,17 @@
  project
-_i
+I
 d, final
@@ -1543,10 +1543,9 @@
 ject
-_i
+I
 d, n
@@ -1752,176 +1752,8 @@
  %7D%0A%0A
-    public String decodeFile(String storedFileName) %7B%0A        FileDB fd = fileDBRepository.findByStoredname(storedFileName);%0A        return fd.getOriginalname();%0A    %7D%0A
 %0A   

remove unused method in file util