package com.gyee.frame.controller.gen;


import com.gyee.frame.common.conf.V2Config;
import com.gyee.frame.model.auto.Weatherfd;
import com.gyee.frame.model.auto.Windpowerinfoday;
import com.gyee.frame.model.custom.ImgVo;
import com.gyee.frame.util.ImgTools;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 用户Controller
 * @ClassName: UserController
 * @author gyee
 * @date 2019-11-20 22:35
 */

@Controller
@RequestMapping("/photoController")
public class PhotoController {


    @PostMapping("/uploadFile")
    @ResponseBody
    public String uploadFiles(@RequestParam("file")MultipartFile file,@RequestParam("type")String type){

        if(null!=file && !file.isEmpty() && null!=type)
        {

            String fileName = file.getOriginalFilename();//获取某个上传文件的文件名
            Calendar c = Calendar.getInstance();
            int year=c.get(Calendar.YEAR);
            int month=c.get(Calendar.MONTH)+1;


            StringBuilder directoryurl=new StringBuilder();
            if(!type.equals("1"))
            {
                directoryurl.append(year).append(month).append("/");
            }
            File dest =null;
            if(fileName.indexOf(".mp3")!=-1)
            {
                String fn=toFilename(type,c,year,month,".mp3");
                directoryurl.append(c.getTime().getTime()).append(".mp3");
                dest = new File(fn);
            }
            else if(fileName.indexOf(".jpg")!=-1 )
            {
                String fn=toFilename(type,c,year,month,".jpg");
                directoryurl.append(c.getTime().getTime()).append(".jpg");
                dest = new File(fn);
            }else if(fileName.indexOf(".jpeg")!=-1 )
            {
                String fn=toFilename(type,c,year,month,".jpeg");
                directoryurl.append(c.getTime().getTime()).append(".jpeg");
                dest = new File(fn);
            }else if( fileName.indexOf(".ico")!=-1)
            {
                String fn=toFilename(type,c,year,month,".ico");
                directoryurl.append(c.getTime().getTime()).append(".ico");
                dest = new File(fn);
            }else if( fileName.indexOf(".gif")!=-1)
            {
                String fn=toFilename(type,c,year,month,".gif");
                directoryurl.append(c.getTime().getTime()).append(".gif");
                dest = new File(fn);
            }else if(fileName.indexOf(".png")!=-1)
            {
                String fn=toFilename(type,c,year,month,".png");
                directoryurl.append(c.getTime().getTime()).append(".png");
                dest = new File(fn);
            }else if(fileName.indexOf(".mp4")!=-1)
            {
                String fn=toFilename(type,c,year,month,".mp4");
                directoryurl.append(c.getTime().getTime()).append(".mp4");
                dest = new File(fn);

            }


            try {
                file.transferTo(dest); //写入文件流
                String.valueOf(directoryurl);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(fileName.indexOf(".mp4")!=-1)
            {
                String fn=toFilename(type,c,year,month,".mp4");
                String temp=fn.substring(0,fn.length()-4);
                dest = new File(temp+".jpg");

                try {
                    ImgTools.randomGrabberFFmpegVideoImage(fn,dest);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return String.valueOf(directoryurl);
        }
        else
        {
            return null;
        }


    }

    private  String toFilename(String type,Calendar c,int year,int month,String ext)
    {
        StringBuilder directoryName=new StringBuilder();
        switch (type) {
            case "1":
                directoryName.append(V2Config.iconurl);
                break;
            case "2":
                directoryName.append(V2Config.audurl).append(year).append(month).append("/");
                break;
            case "3":
                directoryName.append(V2Config.imgurl).append(year).append(month).append("/");
                break;
            case "4":
                directoryName.append(V2Config.videourl).append(year).append(month).append("/");
                break;
            default:
                break;
        }

        File directory = new File(String.valueOf(directoryName));
        if(!directory.exists()){

            directory.mkdirs();
        }
        String dir=String.valueOf(directoryName.append(c.getTime().getTime()).append(ext));
        return  dir;
    }


    @PostMapping(value="/uploadUrl")
    @ResponseBody
    public String uploadimg(@RequestBody ImgVo vo) throws Exception {


        String data="";
        String dataprefix="";

        String imgurl=null;
        String[] str=vo.getFile().split("base64,");
        if(str==null||str.length!=2)
            return null;
        dataprefix=str[0];
        data=str[1];
        String suffix = "";
        if("data:image/jpeg;".equalsIgnoreCase(dataprefix)){//data:image/jpeg;base64,base64编码的jpeg图片数据
            suffix = ".jpg";
        } else if("data:image/x-icon;".equalsIgnoreCase(dataprefix)){//data:image/x-icon;base64,base64编码的icon图片数据
            suffix = ".ico";
        } else if("data:image/gif;".equalsIgnoreCase(dataprefix)){//data:image/gif;base64,base64编码的gif图片数据
            suffix = ".gif";
        } else if("data:image/png;".equalsIgnoreCase(dataprefix)){//data:image/png;base64,base64编码的png图片数据
            suffix = ".png";
        }else{
            throw new Exception("上传图片格式不合法");
        }

        //因为BASE64Decoder的jar问题，此处使用spring框架提供的工具包
        byte[] bs = Base64Utils.decodeFromString(data);
        //FileUtils.writeByteArrayToFile(new File(savepath+System.currentTimeMillis()+suffix), bs);
        Calendar c = Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH)+1;
        StringBuilder directoryName=new StringBuilder();
        StringBuilder directoryurl=new StringBuilder();

        String url= V2Config.imgurl;

        directoryName.append(url).append(year).append(month).append("/");
        directoryurl.append(year).append(month).append("/");
        File directory = new File(String.valueOf(directoryName));

        if(!directory.exists()){

            directory.mkdirs();
        }
        directoryurl.append(vo.getTimestamp()).append(suffix);
        imgurl= String.valueOf(directoryName.append(vo.getTimestamp()).append(suffix));
        FileOutputStream out=new FileOutputStream(new File(imgurl));
        out.write(bs);
        out.flush();
        out.close();
        imgurl= String.valueOf(directoryurl);
       // imgurl="E:\\idea_workspace\\videochat\\websocket\\target\\classes\\static\\images_upload\\20208\\1596534403032.png";
        return imgurl;
    }
}
