package org.aohe.webcam;

import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.Control;
import org.aohe.constant.SocketEnum;
import org.aohe.result.R;
import org.aohe.web.SocketUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class WebcamShowTask extends Thread {

     private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    @Override
    public void run() {
        scheduler.scheduleAtFixedRate(() -> {
            try {
                while (Control.SHOW_CAMERA) {
                    WebCamUtils.open();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedImage bi = WebCamUtils.getImage();
                    try {
                        ImageIO.write(bi, "JPG", baos);
                    } catch (IOException e) {
                        //log.error(e.getMessage(), e);
                    }finally {
                        if (bi != null){
                            bi.flush();
                            bi = null;
                        }
                    }
                    String base64 = null;
                    try {
                        base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
                    } catch (UnsupportedEncodingException e) {
                        log.error(e.getMessage(), e);
                    }finally {
                        baos.close();
                    }
                    SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(R.ok(base64).toJsonStr());
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }, 0, 100, TimeUnit.MILLISECONDS);

         try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            log.error("Main thread interrupted", e);
        }

    }

    public static void main(String[] args) {
        SocketUtils.start(SocketEnum.CAM_SOCKET);
        File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
        while (true){

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                BufferedImage image = ImageIO.read(file);
                ImageIO.write(image, "PNG", baos);
            } catch (IOException e) {
                //log.error(e.getMessage(), e);
            }

            String base64 = null;
            base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), StandardCharsets.UTF_8);

            SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(base64);
        }
    }
}
