package com.app.baselibrary.utils;

import android.view.View;

import com.app.baselibrary.R;
import com.app.baselibrary.commonUtil.LogUtil;

import java.util.Random;

/**
 * Created by henry  16/3/8.
 */
public class PointUtils {

    public static int mMinDistance = 200;


    private static int getXYDistance(int[] locationA, int[] locationB) {

        int x1 = locationA[0];
        int y1 = locationA[1];
        int x2 = locationB[0];
        int y2 = locationB[1];

        int pointA = Math.abs(x1 - x2);
        int pointB = Math.abs(y1 - y2);

        double a = Math.pow(pointA, 2);
        double b = Math.pow(pointB, 2);
        double c = Math.sqrt(a + b);

        return (int) c;
    }


    public static boolean isDistance(int[] locationOld, int[] locationNew) {

        int newDistance = getXYDistance(locationOld, locationNew);

        return newDistance >= mMinDistance;
    }

    public static boolean isDistance(View oldView, View newView) {
        int x1 = (int) oldView.getX();
        int y1 = (int) oldView.getY();
        int x2 = (int) newView.getX();
        int y2 = (int) newView.getY();

        return isDistance(x1, y1, x2, y2);
    }

    public static boolean isDistance(int x1, int y1, int x2, int y2) {
        int[] locationOld = {x1, y1};
        int[] locationNew = {x2, y2};
        int newDistance = getXYDistance(locationOld, locationNew);

        return newDistance >= mMinDistance;
    }


    //d距离边缘
    private static int mXDistance = 380;//360
    private static int mYDistance = 264;//264

    // 0 ~ screen.width/2 +- d
    public static int randomXLocation(int mMaxX) {
        Random random = new Random();
        int halfScreen = mMaxX / 2;
        int x = random.nextInt(halfScreen);

        LogUtil.e("mX_randomX:"+halfScreen+"--"+x + "");

        if (x < mXDistance) {
            x = mXDistance;
        }
        if (x > halfScreen - mXDistance) {
            x = halfScreen - mXDistance;
        }

        if (x < mXDistance) {
            x = mXDistance;
        }
        return x;
    }

    // 0 ~ screen.height + - d
    public static int randomYLocation(int mMaxY) {
        Random random = new Random();

        int y = random.nextInt(mMaxY);

        //LogUtil.e("mY_randomY:", mMaxY+"---"+y + "");
        //marginTop
        if (y < mYDistance) {
            y = mYDistance;
        }

        //marginBottom
        if (y > mMaxY - mYDistance) {
            y = mMaxY - mYDistance;
        }

        if (y < mYDistance) {
            y = mYDistance;
        }
        return y;
    }

    public static int randomColor() {
        Random random = new Random();
        int r = random.nextInt(3);
        int resId;
        if (r == 0) {
            resId = R.mipmap.icon_point_red;
        } else if (r == 1) {
            resId = R.mipmap.icon_point_green;
        } else {
            resId = R.mipmap.icon_point_blue;
        }
        return resId;
    }


}
