package com.lightinit.hsdataplatformresdir.controller;

import com.lightinit.hsdataplatformresdir.common.FTPConfigurationUtilsPro;
import com.lightinit.hsdataplatformresdir.common.FTPTookitUtils;
import com.lightinit.hsdataplatformresdir.json.pojo.FTPConfigurationParameter;
import com.lightinit.hsdataplatformresdir.json.pojo.UploadResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by Mr.Yao on 2017/5/8.
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private FTPTookitUtils ftpTookitUtils;
    @Autowired
    private FTPConfigurationUtilsPro ftpConfigurationUtilsPro;
    @ResponseBody
    @RequestMapping(value = "deployment/{product}/zip/package.action")
    public String print_save_file(HttpServletRequest request, @PathVariable String product){
        try {
            CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
            if(multipartResolver.isMultipart(request)){
                MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
                Iterator<String> iterator=multipartHttpServletRequest.getFileNames();
                while (iterator.hasNext()){
                    MultipartFile file=multipartHttpServletRequest.getFile(iterator.next());
                    if(file!=null){
                        String filename=file.getOriginalFilename();
                        if(StringUtils.isEmpty(filename.trim())){
                            continue;
                        }
                        FTPConfigurationParameter ftpConfigurationParameter=new FTPConfigurationParameter(ftpConfigurationUtilsPro.getDeploy_host(),ftpConfigurationUtilsPro.getDeploy_port(),ftpConfigurationUtilsPro.getDeploy_username(),ftpConfigurationUtilsPro.getDeploy_password());
                        ftpTookitUtils.uploadFile(file.getInputStream(),String.format("deployment/%s/%s",product,filename),ftpConfigurationParameter);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }

    @ResponseBody
    @RequestMapping(value = "file/zip/upload.action")
    public UploadResultEntity print_save_mtas_client_apk_file(HttpServletRequest request, @RequestParam String product) {
        UploadResultEntity jsonResult=new UploadResultEntity();
        try {
            String remotePath = "test";
            String itunsUrl = "";
            if (StringUtils.isEmpty(remotePath) && StringUtils.isEmpty(itunsUrl))
            {
                jsonResult.setStatus(-1);
                jsonResult.setMessage("参数异常");
                jsonResult.setFilename("");
                jsonResult.setDownloadurl("");
                return jsonResult;
            }
            else if (!StringUtils.isEmpty(itunsUrl))
            {
                jsonResult.setStatus(1);
                jsonResult.setMessage("上传成功");
                jsonResult.setFilename("ios");
                jsonResult.setDownloadurl(itunsUrl);
                return jsonResult;
            }
            else {
                CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
                if (multipartResolver.isMultipart(request)) {
                    MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                    Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
                    while (iterator.hasNext()) {
                        MultipartFile file = multipartHttpServletRequest.getFile(iterator.next());
                        if (file != null) {
                            String filename = file.getOriginalFilename();
                            if (StringUtils.isEmpty(filename.trim())) {
                                continue;
                            }
                            FTPConfigurationParameter ftpConfigurationParameter = new FTPConfigurationParameter(ftpConfigurationUtilsPro.getApk_host(), ftpConfigurationUtilsPro.getApk_port(), ftpConfigurationUtilsPro.getApk_username(), ftpConfigurationUtilsPro.getApk_password());
                            ftpTookitUtils.uploadFile(file.getInputStream(), String.format("%s/%s", remotePath, filename), ftpConfigurationParameter);
                            jsonResult.setStatus(1);
                            jsonResult.setMessage("上传成功");
                            jsonResult.setFilename(filename);
                            jsonResult.setDownloadurl(String.format("http://localhost/file/down.action?p=%s", remotePath));
                            return jsonResult;
                        }
                    }
                } else {
                    jsonResult.setStatus(-1);
                    jsonResult.setMessage("参数错误");
                }
            }
        } catch (IOException e) {
            jsonResult.setStatus(-1);
            jsonResult.setMessage("服务端异常");
            e.printStackTrace();
        }
        return jsonResult;
    }
}
