package org.aohe.webcam;

import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.Control;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class WebCamUtils {

    static{
        //设置默认驱动
        Webcam.setDriver(new NativeDriver());
    }

    private static Webcam webcam;

    /**
     * 获取webcam
     * @return
     */
    private static Webcam initWebcam(){
        if(webcam == null){
            //没有设置摄像头就取默认的
            setWebcam(null);
        }
        Dimension[] viewSizes = webcam.getViewSizes();
        Dimension bigSize = WebcamResolution.VGA.getSize();
        for (Dimension d : viewSizes){
            //获取最大分辨率
            if(bigSize.getWidth()<d.getWidth()){
                bigSize = d;
            }
        }
        setViewSizes(bigSize.width, bigSize.height);
        return webcam;
    }

    /**
     * 设置摄像头
     * @param name
     */
    public static void setWebcam(String name){
        if(webcam!=null && webcam.isOpen()){
            webcam.close();
        }
        if(name != null){
            webcam = Webcam.getWebcamByName(name);
        }else{
            webcam = Webcam.getDefault();
        }
        webcam = initWebcam();
    }

    /**
     * 获取摄像头列表
     * @return
     */
    public static List<String> getWebcams(){
        List<Webcam> webcamList = Webcam.getWebcams();
        return webcamList.stream().map(Webcam::getName).collect(Collectors.toList());
    }

    public static BufferedImage getImage(){
        if (!webcam.isOpen()){
            webcam.open();
        }
        return webcam.getImage();
    }

    public static InputStream getImageInputStream() {
        BufferedImage bi = getImage();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(bi, "png", os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new ByteArrayInputStream(os.toByteArray());
    }

    /**
     * 打开摄像头
     */
    public static void open(){
        if (!webcam.isOpen()){
            webcam.open();
        }
    }


    /**
     * 关闭摄像头
     */
    public static void close(){
        if (webcam!=null && webcam.isOpen()){
            webcam.close();
        }
    }

    public static Webcam get(){
        return webcam;
    }

    /**
     * 获取摄像头分辨率
     * @return
     */
    public static List<String> getViewSizes(){
        List<Dimension> viewSizes = Arrays.asList(webcam.getViewSizes());
        return viewSizes.stream().map(dimension -> (int)dimension.getWidth()+"x"+(int)dimension.getHeight()).collect(Collectors.toList());
    }

    public static void setViewSizes(int width, int height){
        Control.SHOW_CAMERA = false;
        if (webcam.isOpen()){
            webcam.close();
        }
        webcam.setViewSize(new Dimension(width, height));
    }

    public static void openWebcamTransfer(){
        Control.SHOW_CAMERA = true;
    }

    public static void closeWebcamTransfer(){
        //死循环控制器置为false
        Control.SHOW_CAMERA = false;
    }
}
