package com.upyuns.platform.rs.universal.controller;

import cn.hutool.core.img.ImgUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;
import com.upyuns.platform.rs.universal.constant.VideoConstant;
import com.upyuns.platform.rs.universal.dto.ImgDTO;
import com.upyuns.platform.rs.universal.dto.UploadImgDTO;
import com.upyuns.platform.rs.universal.service.FileUploadService;
import com.upyuns.platform.rs.universal.service.UploadService;
import com.upyuns.platform.rs.universal.service.UploadZipService;
import com.upyuns.platform.rs.universal.utils.ImgBase64Util;
import com.upyuns.platform.rs.universal.utils.PublicMsg;
import com.upyuns.platform.rs.universal.vo.Ueditor;
import com.upyuns.platform.rs.website.entity.FileData;
import com.upyuns.platform.rs.website.entity.GainData;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 图片上传
 */
@RestController
@RequestMapping("file")
@IgnoreUserToken
@Slf4j
public class UploadController{

    @Autowired
    UploadService uploadService;
    @Autowired
    FileUploadService fileUploadService;
    @Autowired
    UploadZipService uploadZipService;

    @Data
    static public class DisposeDTO {
        private String context = "人民星云";
        private Integer size = 36;
        private Integer x = 0;
        private Integer y = 0;
        private Integer style = Font.BOLD;
        private String styleName = "宋体";
        private Float alpha = 0.5f;
        private Float scaleHight = 320f;
        private Float scaleWidth = 360f;
        private Color color = Color.WHITE;
        private Integer colorNum;
        private Integer degree = -40;

        public void setColorNum(Integer colorNum) {
            this.colorNum = colorNum;
            if(1 == colorNum) {
                this.color = Color.WHITE;
            }else if (2 == colorNum) {
                this.color = Color.BLACK;
            }else if (3 == colorNum) {
                this.color = Color.GRAY;
            }
        }
    }

    @RequestMapping(value = "/app/unauth/upload", method = RequestMethod.POST)
    public JSONObject upload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "app")String prefix
     )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
    }

    @RequestMapping(value = "/app/unauth/uploadDispose", method = RequestMethod.POST)
    public JSONObject uploadDispose(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "app")String prefix,
            DisposeDTO dto
    )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFileDispose(file, prefix, dto));
    }

    @ApiOperation("视频上传")
    @RequestMapping(value = "/app/unauth/uploadVideo", method = RequestMethod.POST)
    public JSONObject uploadVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "app")String prefix
    )throws Exception {
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
    }

    @RequestMapping(value = "/admin/uploadDispose", method = RequestMethod.POST)
    public JSONObject adminUploadDispose(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "app")String prefix,
            DisposeDTO dto
    )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFileDispose(file, prefix, dto));
    }


    @RequestMapping(value = "/app/unauth/admin/upload", method = RequestMethod.POST)
    public JSONObject uploads(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "admin")String prefix
    )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
//        String fileName = file.getOriginalFilename();  //图片名字
       /* if (!contentType.equals("image/jpeg") && !contentType.equals("image/gif")&&!contentType.equals("image/png")) {
            return JsonResultUtil.createFailedResult(2001,"格式不对!!!");
        }*/
//        if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
//            return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
//        }
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
    }



    @RequestMapping(value = "/app/unauth/admin/uploadreturn", method = RequestMethod.POST)
    public JSONObject uploadreturn(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "admin")String prefix
    )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFileAndGetFileInfo(file,prefix));
    }

    /**
     * 上传成果文件
     * @param file
     * @param prefix gainDataId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/app/unauth/admin/uploadGainFile", method = RequestMethod.POST)
    public ObjectRestResponse uploadGainFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "admin")String prefix
    )throws Exception {
        FileData data = uploadService.uploadGainFile(file,prefix);
        return ObjectRestResponse.succ(data);
    }

    @RequestMapping(value = "/app/unauth/admin/uploadTempcaheFile", method = RequestMethod.POST)
    public ObjectRestResponse uploadTempcaheFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "upload")String prefix
    )throws Exception {
        FileData data = uploadService.uploadTempcaheFile(file,prefix);
        return ObjectRestResponse.succ(data);
    }

    @RequestMapping(value = "/app/unauth/admin/upload250", method = RequestMethod.POST)
    public JSONObject uploads250(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "admin")String prefix
    )throws Exception {
        String contentType = file.getContentType();   //图片文件类型
        return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile250(file,prefix));
    }

    @RequestMapping(value = "/app/unauth/shp/upload", method = RequestMethod.POST)
    public ObjectRestResponse shpUploads(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "shp")String prefix
    ) throws Exception{
        return ObjectRestResponse.succ(uploadService.uploadShpFile(file,prefix));
    }

    @IgnoreUserToken
    @RequestMapping(value = "/app/unauth/download", method = RequestMethod.GET) //匹配的是href中的download请求
    public ResponseEntity<byte[]> download(@RequestParam("realFileRelPath") String realFileRelPath) throws Exception {
        return uploadService.downloadFile(realFileRelPath);
    }

    @RequestMapping(value = "/app/unauth/uploadFiles", method = RequestMethod.POST)
    public JSONObject uploadFiles(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam(value = "prefix",defaultValue = "app")String prefix,
            HttpServletRequest request
        )throws Exception {
        if(files!=null&&files.length>0){
            String urls="";
            for(int i = 0;i<files.length;i++){
                MultipartFile file = files[i];
                //保存文件
                String contentType = file.getContentType();
//                if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
//                    log.error("大小超过限制!!!");
//                    continue;
//                }
                String path=uploadService.uploadFile(file,prefix);
                if(StringUtils.isBlank(urls)){
                    urls+=path;
                }else {
                    urls+=","+path;
                }

            }
            return JsonResultUtil.createSuccessResultWithObj(urls);

        }
        return JsonResultUtil.createDefaultFail();

    }

    @PostMapping( value = "/app/unauth/uploads")
    public JSONObject uploads(@RequestBody UploadImgDTO uploadimgDTO)throws Exception {
            if (uploadimgDTO==null){
                JsonResultUtil.createDefaultFail();
            }
            String prefix=uploadimgDTO.getPrefix();
            if(StringUtils.isBlank(prefix)){
                prefix="app";
            }
            List<ImgDTO> imgDTOS=uploadimgDTO.getImgs();
            List<Map<String,Object>> list=new ArrayList<>();
            if (imgDTOS.size()>0){
                for (ImgDTO imgDTO:imgDTOS){
                    String  imgbese =imgDTO.getImgFile();
                    String  fileName =imgDTO.getFileName();
                    Map<String,Object> map=new HashMap<>();
                    if(StringUtils.isNotBlank(imgbese)){
                        String url=uploadService.uploadImg(imgbese,prefix);
                        map.put("imgUrl",url);
                        map.put("fileName",fileName);
                        list.add(map);
                    }
                }
            }
            return JsonResultUtil.createSuccessResultWithObj(list);

        }




    //以下是图片上传的方法
    @RequestMapping(value="/app/unauth/ueditor")
    @ResponseBody
    public String ueditor(HttpServletRequest request) {

        return PublicMsg.UEDITOR_CONFIG;
    }

    @RequestMapping(value="/app/unauth/ueditor", method = RequestMethod.POST)
    public Ueditor imgUpload(MultipartFile upfile) throws Exception {
        Ueditor ueditor = new Ueditor();
        ueditor.setUrl(uploadService.uploadFile(upfile,"admin"));
        ueditor.setOriginal(upfile.getOriginalFilename());
        ueditor.setState("SUCCESS");
        ueditor.setTitle(upfile.getOriginalFilename());
        return ueditor;
    }

    @RequestMapping(value="/app/unauth/zip", method = RequestMethod.POST)
    public ObjectRestResponse zip(
            @RequestParam("file")MultipartFile upfile,
            @RequestParam(value = "prefix",defaultValue = "renovate")String prefix) throws Exception {
        return fileUploadService.handlerUpload(upfile,null,prefix);
    }

    @ApiOperation("图片压缩包上传")
    @PostMapping(value="/app/unauth/pictureZip")
    public ObjectRestResponse pictureZip(
            @RequestParam("file")MultipartFile file,
            @RequestParam(value = "prefix",defaultValue = "summit")String prefix) throws Exception {
        return uploadZipService.uploadPictureZip(file,prefix);
    }
}

