package com.flurgle.camerakit;

import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;

import com.flurgle.camerakit.utils.AspectRatio;

public abstract class PreviewImpl {

    interface Callback {
        void onSurfaceChanged();
    }

    private Callback mCallback;

    private int mWidth;
    private int mHeight;

    private int mTrueWidth;
    private int mTrueHeight;

    void setCallback(Callback callback) {
        mCallback = callback;
    }

    abstract Surface getSurface();

    abstract View getView();

    abstract Class getOutputClass();

    abstract void setDisplayOrientation(int displayOrientation);

    abstract boolean isReady();

    protected void dispatchSurfaceChanged() {
        mCallback.onSurfaceChanged();
    }

    SurfaceHolder getSurfaceHolder() {
        return null;
    }

    SurfaceTexture getSurfaceTexture() {
        return null;
    }

    void setSize(int width, int height) {
        mWidth = width;
        mHeight = height;

        // Refresh true preview size to adjust scaling
        setTruePreviewSize(mTrueWidth, mTrueHeight);
    }

    int getWidth() {
        return mWidth;
    }

    int getHeight() {
        return mHeight;
    }

    void setTruePreviewSize(int width, int height) {
        this.mTrueWidth = width;
        this.mTrueHeight = height;

        AspectRatio aspectRatio = AspectRatio.of(width, height);
        int targetHeight = (int) (getView().getWidth() * aspectRatio.toFloat());
        float scaleY;
        if (getView().getHeight() > 0) {
            scaleY = (float) targetHeight / (float) getView().getHeight();
        } else {
            scaleY = 1;
        }

        if (scaleY > 1) {
            getView().setScaleY(scaleY);
        } else {
            getView().setScaleX(1 / scaleY);
        }
    }

    int getTrueWidth() {
        return mTrueWidth;
    }

    int getTrueHeight() {
        return mTrueHeight;
    }

}

@@ -1383,32 +1383,77 @@
 ight = height;%0A%0A
+        if (width != 0 && height != 0) %7B%0A    
         AspectRa
@@ -1497,24 +1497,28 @@
 h, height);%0A
+    
         int 
@@ -1594,16 +1594,20 @@
         
+   
+ 
 float sc
@@ -1612,16 +1612,20 @@
 scaleY;%0A
+    
         
@@ -1653,24 +1653,28 @@
 ht() %3E 0) %7B%0A
+    
             
@@ -1732,24 +1732,28 @@
 etHeight();%0A
+    
         %7D el
@@ -1761,32 +1761,36 @@
 e %7B%0A            
+    
 scaleY = 1;%0A    
@@ -1789,27 +1789,35 @@
  1;%0A        
+   
+ 
 %7D%0A%0A
+    
         if (
@@ -1834,32 +1834,36 @@
 ) %7B%0A            
+ 
+   
 getView().setSca
@@ -1867,32 +1867,36 @@
 ScaleY(scaleY);%0A
+    
         %7D else %7B
@@ -1900,32 +1900,36 @@
 e %7B%0A            
+    
 getView().setSca
@@ -1937,32 +1937,46 @@
 eX(1 / scaleY);%0A
+            %7D%0A
         %7D%0A    %7D%0A

fix crash with no camera avail