package com.dbnt.faisp.config;

import com.dbnt.faisp.main.menuMgt.service.MenuMgtService;
import org.springframework.core.env.Environment;
import com.dbnt.faisp.main.organMgt.service.OrganConfigService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;

import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import com.dbnt.faisp.util.ParamMap;
import com.dbnt.faisp.util.Utils;

import lombok.RequiredArgsConstructor;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;



import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@RestController
@RequiredArgsConstructor
public class EditorController {
	
  @Value("${site.domain}")
  protected String siteDomain;
	
  @Value("${file.dir}")
  protected String fileDir;
  
  @Value("${file.dir.editor}")
  protected String editorPath;
  
  @Value("${editor.img.view}")
  protected String imgView;


  @PostMapping("/Crosseditor/uploadImg")
  public @ResponseBody JSONObject uploadImg(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) { 
	  
	  JSONObject jsonObject = new JSONObject();
  	  JSONObject data = new JSONObject();
  	  JSONArray req_array = new JSONArray();
		
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
			MultipartFile mFile = multipartRequest.getFile("imageFile");
			if(!"".equals(mFile.getOriginalFilename())){
				
		        String attach_file_Name = mFile.getOriginalFilename();
		        String attach_save_Name = Utils.generationSaveName();
		        String imageKind = request.getParameter("imageKind");
		        String editorFrame = request.getParameter("editorFrame");
		        
		        //파일 타입
		        String extNm = "." + attach_file_Name.substring( attach_file_Name.lastIndexOf( "." ) + 1, attach_file_Name.length());
		        if(".jpg,.png,.jpeg".indexOf(extNm.toLowerCase()) > -1) {
		        	
		        	File dir = new File(fileDir+File.separator+editorPath);
		        	if (!dir.exists()) {
		        		try{
		        			
		        			// 생성
		        			boolean result2 = dir.mkdir();
		        			if (result2) {
		        				System.out.println("Directory is created.");
		        			} else {
		        				System.out.println("Failed to create directory.");
		        			}
		        		} catch(Exception e){
		        			System.out.println("Exception occurred.");
		        			e.getStackTrace(); 
		        		}
		        	} else {
		        		System.out.println("Directory already exists");
		        	}
		        	
		        	File file = new File(fileDir+File.separator+editorPath, attach_save_Name + extNm);        	
						FileCopyUtils.copy(mFile.getBytes(), file);
						
		        	String webPath = siteDomain+ imgView + attach_save_Name + extNm;

		        	jsonObject.put("result","success");
		        	data.put("imageURL",webPath);
		        	data.put("imageKind", imageKind);
		        	data.put("editorFrame", editorFrame);
		        	req_array.add(data);
		        	jsonObject.put("addmsg", req_array);

		        } else {
		        	ParamMap error = new ParamMap();
		        	error.set("message", "Check File Extentions.");
				}
			} else {
	        	ParamMap error = new ParamMap();
	        	error.set("message", "Check File Extentions.");

			}
		} catch (Exception e) {
      	ParamMap error = new ParamMap();
      	error.set("message", "Check File Extentions.");
		}

		return jsonObject;
  }
  

  
}
