package cn.edu.whu.boot.xml.controller;

import cn.edu.whu.boot.common.domain.*;
import cn.edu.whu.boot.common.service.CacheService;
import cn.edu.whu.boot.common.utils.collection.OtherUtil;
import cn.edu.whu.boot.common.utils.xml.XmlFileToJsonUtil;
import cn.edu.whu.boot.xml.domain.RedisKeyType;
import cn.edu.whu.boot.xml.domain.Task;
import cn.edu.whu.boot.xml.domain.XmlEnity;
import cn.edu.whu.boot.xml.service.ITaskService;
import cn.edu.whu.boot.xml.service.IXmlEnityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * XML 文件相关的接口
 * @author Zhou Chao
 */
@RestController
@RequestMapping("xml")
public class XmlFileManagementController{

    private final IXmlEnityService xmlEnityService;
    private final ITaskService taskService;
    private final CacheService cacheService;

    @Autowired
    public XmlFileManagementController(IXmlEnityService xmlEnityService, ITaskService taskService,
                                       CacheService cacheService){
        this.xmlEnityService = xmlEnityService;
        this.taskService = taskService;
        this.cacheService = cacheService;
    }

    /**
     * 前端请求当前任务的配置内容!
     */
    @RequestMapping(value = "requesting", produces = "text/html;charset=UTF-8")
    public String requestXmlFileData(Integer id){
        Task task = taskService.getTaskById(id);
        if(task == null){
            return ResultMessage.failed(StatusCode.FAILED_AND_SHOW.getStatus(), "未查询到对应的任务").toString();
        }
        else{
            XmlEnity xmlEnity = xmlEnityService.getXmlEnityById(task.getXmlId());
            if(xmlEnity == null){
                return ResultMessage.failed(StatusCode.FAILED_AND_SHOW.getStatus(), "未查询到对应的XML").toString();
            }
            else{
                String someXmlFileDataRedisKey = RedisKeyType.XML_FILE_DATA + String.valueOf(xmlEnity.getId());
                Object redisObject = cacheService.get(someXmlFileDataRedisKey);
                if(redisObject != null){
                    return ResultMessage.success(redisObject).toString();
                }
                String xmlFileData = XmlFileToJsonUtil.xmlReader(xmlEnity.getFilePath());
                cacheService.set(someXmlFileDataRedisKey, xmlFileData, RedisExpireTime.ONE_WEEK.getTime());
                return ResultMessage.success(xmlFileData).toString();
            }
        }
    }

    /**
     * 下载 XML 文件
     */
    @RequestMapping(value = "downloading", produces = "text/html;charset=UTF-8")
    public String downloadXmlFile(Integer id, HttpServletResponse response){
        Task task = taskService.getTaskById(id);
        XmlEnity xmlEnity = xmlEnityService.getXmlEnityById(task.getXmlId());
        String filePath = xmlEnity.getFilePath();
        File file = new File(filePath);
        response.setContentType("text/xml");
        response.addHeader("Content-Disposition", "attachment;fileName=application.xml");
        return ResultMessage.success(OtherUtil.uploadFileByResponse(file, response)).toString();
    }
}
