package com.example.controller;

import com.example.baidu.ueditor.ActionEnter;
import com.example.baidu.ueditor.ConfigManager;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UEditorController {

	@Value("${imageUrl}")
	private String imageUrl;
	
	
	
	@Value("${oss.folder}")
	private String ossPath;
	
    @RequestMapping("/")
    private String showPage() {
        return "index";
    }

    @RequestMapping(value = "/uploadImage")
    public void config(HttpServletRequest request, HttpServletResponse response) {
    	
    	
        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
        	ConfigManager configManger=ConfigManager.getInstance(rootPath, request.getContextPath(), request.getRequestURI());
        	String imageUrlPrefix=(String) configManger.getAllConfig().get("imageUrlPrefix");
        	
        	
            String exec = new ActionEnter(request, rootPath).exec();
            exec=exec.replace("\"imageUrlPrefix\":\"https://imgb.mofangx.com/\"","'imageUrlPrefix':'"+imageUrlPrefix+"'");
            exec=exec.replace("\"basePath\":\"erp/\"","'basePath':'"+ossPath+"'");
        	/*if(!exec.contains("videoMaxSize")) {
        		 JSONObject jsonConfig =new JSONObject(exec);
                jsonConfig.put("url", imageUrl+jsonConfig.getString("url"));
                System.out.println(jsonConfig);
                exec=jsonConfig.toString();
        	}*/
            
           
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
    }
}
