package com.sensetime.demo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;

import com.blankj.utilcode.util.LogUtils;
import com.sensetime.demo.SenseTimeMtc;

import org.jetbrains.annotations.NotNull;

public class UVCCameraTextureView extends TextureView {
    public static final String TAG = "UVCCameraTextureView";

    private double mRequestedAspect = -1.0;

    private boolean isDrawOk = true;

    public UVCCameraTextureView(final Context context) {
        this(context, null, 0);
    }

    public UVCCameraTextureView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UVCCameraTextureView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setAspectRatio(final double aspectRatio) {
        if (aspectRatio < 0) {
            throw new IllegalArgumentException();
        }
        if (mRequestedAspect != aspectRatio) {
            mRequestedAspect = aspectRatio;
            requestLayout();
        }
    }

    public void setAspectRatio(final int width, final int height) {
        setAspectRatio(width / (double) height);
    }

    // include virtual menu's height
    public static int getRealHeight(@NotNull Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        assert wm != null;
        Display display = wm.getDefaultDisplay();
        int screenHeight = 0;
        {
            DisplayMetrics dm = new DisplayMetrics();
            display.getRealMetrics(dm);
            screenHeight = dm.heightPixels;
        }
        return screenHeight;
    }

    public static int getRealWidth(@NotNull Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        assert wm != null;
        wm.getDefaultDisplay().getRealMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            isDrawOk = true;
        } else {
            isDrawOk = false;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        float screenWidth = getRealWidth(getContext());
        float screenHeight = getRealHeight(getContext());
        SenseTimeMtc senseTimeMtc = SenseTimeMtc.getInstance();
        int CAMERA_W, CAMERA_H;
        if (senseTimeMtc.getmM10_L_P() == SenseTimeMtc.LANDSCAPE_0 || senseTimeMtc.getmM10_L_P() == senseTimeMtc.LANDSCAPE_180) {
            CAMERA_W = senseTimeMtc.getCameraWidth();
            CAMERA_H = senseTimeMtc.getCameraHeight();
        } else {//竖屏状态
            CAMERA_W = senseTimeMtc.getCameraHeight();
            CAMERA_H = senseTimeMtc.getCameraWidth();
        }
        LogUtils.dTag(TAG, "width = " + screenWidth + ",height = " + screenHeight + ",requestAspect = " + mRequestedAspect);
        if (screenWidth > screenHeight) { // screen landscape
            if (CAMERA_H < CAMERA_W) {//camera landscape
                if (senseTimeMtc.ismScaleHeight()) {//Adjust width according to height
                    senseTimeMtc.setPreviewWidth((int) (screenHeight / mRequestedAspect));
                    senseTimeMtc.setPreviewHeight((int) screenHeight);
                } else {//Adjust height according to width
                    senseTimeMtc.setPreviewWidth((int) screenWidth);
                    senseTimeMtc.setPreviewHeight((int) (screenWidth * mRequestedAspect));//M10
                }
            } else {//camera portrait
                senseTimeMtc.setPreviewWidth((int) (screenHeight * mRequestedAspect));
                senseTimeMtc.setPreviewHeight((int) screenHeight);
            }
        } else { // screen portrait
            if (CAMERA_H < CAMERA_W) { // camera landscape
                senseTimeMtc.setPreviewWidth((int) (screenWidth / mRequestedAspect));
                senseTimeMtc.setPreviewHeight((int) screenWidth);
            } else { // camera portrait
                if (senseTimeMtc.ismScaleHeight()) {//Adjust width according to height
                    senseTimeMtc.setPreviewWidth((int) (screenHeight * mRequestedAspect));
                    senseTimeMtc.setPreviewHeight((int) screenHeight);
                } else {//Adjust height according to width
                    senseTimeMtc.setPreviewWidth((int) screenWidth);
                    senseTimeMtc.setPreviewHeight((int) (screenWidth / mRequestedAspect));
                }
            }
        }
        setMeasuredDimension(senseTimeMtc.getPreviewWidth(), senseTimeMtc.getPreviewHeight());
    }


    public boolean isDrawOk() {
        return isDrawOk;
    }

    public void setDrawOk(boolean drawOk) {
        isDrawOk = drawOk;
    }
}
