package tee3.webrtc;

import android.content.Context;

import javax.annotation.Nullable;

public class ScaleScreenCapturer implements VideoCapturer, SurfaceTextureHelper.OnTextureFrameAvailableListener, ScreenCapturerAndroid.TextureFrameAvailable {


    @Nullable private SurfaceTextureHelper surfaceTextureHelper;
    @Nullable private CapturerObserver capturerObserver;
    private boolean isDisposed = false;
    private int width;
    private int height;

    private static ScaleZone scaleZone;

    public ScaleScreenCapturer() {

    }

    @Override
    public void onTextureFrameAvailable(int oesTextureId, float[] transformMatrix, long timestampNs) {
    }

    @Override
    public void initialize(SurfaceTextureHelper surfaceTextureHelper, Context applicationContext, CapturerObserver capturerObserver) {
        checkNotDisposed();

        if (capturerObserver == null) {
            throw new RuntimeException("capturerObserver not set.");
        }
        this.capturerObserver = capturerObserver;

        if (surfaceTextureHelper == null) {
            throw new RuntimeException("surfaceTextureHelper not set.");
        }
        this.surfaceTextureHelper = surfaceTextureHelper;
    }

    @Override
    public void startCapture(int width, int height, int framerate) {
        checkNotDisposed();

        this.width = width;
        this.height = height;
        surfaceTextureHelper.getSurfaceTexture().setDefaultBufferSize(width, height);
        capturerObserver.onCapturerStarted(true);
        surfaceTextureHelper.startListening(ScaleScreenCapturer.this);
    }

    @Override
    public void stopCapture() throws InterruptedException {
        checkNotDisposed();
        ThreadUtils.invokeAtFrontUninterruptibly(surfaceTextureHelper.getHandler(), new Runnable() {
            @Override
            public void run() {
                surfaceTextureHelper.stopListening();
                capturerObserver.onCapturerStopped();
            }
        });
    }

    @Override
    public void changeCaptureFormat(int width, int height, int framerate) {
        checkNotDisposed();
        this.width = width;
        this.height = height;
    }

    @Override
    public void dispose() {
        isDisposed = true;
    }

    @Override
    public boolean isScreencast() {
        return true;
    }

    private void checkNotDisposed() {
        if (isDisposed) {
            throw new RuntimeException("capturer is disposed.");
        }
    }

    @Override
    public void onTextureFrameAvailable(VideoFrame.I420Buffer i420Buffer, long timestampNs) {
        final VideoFrame frame = new VideoFrame(i420Buffer, 0 /* rotation */, timestampNs);
        capturerObserver.onFrameCaptured(frame);
        frame.release();
    }

    public static void setScaleZone(ScaleZone scaleZone) {
        ScaleScreenCapturer.scaleZone = scaleZone;
    }

    public static ScaleZone getScaleZone() {
        return scaleZone;
    }

    public static class ScaleZone {
        int startX;
        int startY;
        int width;
        int height;

        public ScaleZone(int startX, int startY, int width, int height) {
            this.startX = startX;
            this.startY = startY;
            this.width = width;
            this.height = height;
        }

        public int getStartX() {
            return startX;
        }

        public void setStartX(int startX) {
            this.startX = startX;
        }

        public int getStartY() {
            return startY;
        }

        public void setStartY(int startY) {
            this.startY = startY;
        }

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        @Override
        public String toString() {
            return "ScaleZone{" +
                    "startX=" + startX +
                    ", startY=" + startY +
                    ", width=" + width +
                    ", height=" + height +
                    '}';
        }
    }
}
