package com.sobot.screencapturesdk.utils;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
 * Created by Neo on 2017/2/9.
 */

public class DisplayUtils {

    /**
     * 获取屏幕分辨率
     * @param context
     * @return Point.  x: Width  y :Height
     */
    public static Point getDisplaySize(Context context) {
        Point point = getScreenSize(context);
        if (null == point)
            return new Point(720, 1280);

        int width  = point.x;
        int height = point.y;
        boolean b6 = (width % 6 == 0) && (height % 6 == 0);
        boolean b4 = (width % 4 == 0) && (height % 4 == 0);
        int displayW = width, displayH = height;
        //调整显示图像大小
        int minSize = Math.min(width, height);
        if (minSize >= 1400) {
            if (b6) {
                displayW = width / 3;
                displayH = height / 3;
            } else if (b4) {
                displayW = width / 2;
                displayH = height / 2;
            }
        } else if (minSize >= 1080) {
            if (b4) {
                displayW = width / 2;
                displayH = height / 2;
            }
        }
        return new Point(displayW, displayH);
    }


    /**
     * 获取当前屏幕旋转角度
     *
     * @param context 上下文Context
     * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏
     */
    public static int getDisplayRotation(Context context) {
        if (context == null) return 0;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (null == windowManager) return 0;
        int rotation = windowManager.getDefaultDisplay().getRotation();
        switch (rotation) {
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
        }
        return 0;
    }

    /**
     * 获取屏幕分辨率
     * @param context
     * @return Point.  x: Width  y :Height
     */
    public static Point getScreenSize(Context context) {
        if (context == null) return null;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return null;
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        Class c;
        try {
            c = Class.forName("android.view.Display");
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            return new Point(width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}