package com.sheep.jiuyan.samllsheep.ui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.sheep.jiuyan.samllsheep.R;

import java.util.Random;

/**
 * Created by: zhoujuncai.
 * Created date: 2018/11/2.
 * Description: 跳动扭蛋机
 */
public class EggView extends View {

    /* 控件宽度 */
    private int width;
    /* 控件高度 */
    private int height;
    /* 扭蛋图片集合 */
    private Bitmap[] bitmaps;
    /* 扭蛋集合 */
    private Egg[] eggs;
    /* 画笔 */
    private Paint paint;
    /* 是否停止 */
    private boolean isStop = false;

    public EggView(Context context) {
        super(context);
    }

    public EggView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EggView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 初始化
     */
    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
        bitmaps = new Bitmap[]{
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan1),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan2),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan3),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan4),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan5),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan6),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan7),
                BitmapFactory.decodeResource(getResources(), R.drawable.niudan8),
        };
    }

    @Override
    protected void onDraw(Canvas canvas) {
        width = getWidth();
        height = getHeight();
        if (eggs == null) {
            initEggs();
        }
        if (!isStop) {
            for (Egg egg : eggs) {
                if (egg.getX() > egg.getWidth() || egg.getX() < 0) {
                    egg.xValue.reverse();
                }
                if (egg.getY() > egg.getHeight() || egg.getY() < 0) {
                    egg.yValue.reverse();
                }
                canvas.drawBitmap(egg.getBitmap(), egg.getX(), egg.getY(), paint);
            }
        }
        super.onDraw(canvas);
        if (!isStop)
            invalidate();
    }

    public void setStop(boolean stop) {
        isStop = stop;
        if (!isStop)
            invalidate();
    }

    /**
     * 初始化扭蛋
     */
    private void initEggs() {
        eggs = new Egg[8];
        for (int i = 0; i < eggs.length; i++) {
            Egg egg = new Egg(width, height, bitmaps[i]);
            eggs[i] = egg;
        }
    }

    //退出时销毁
    public void onDestroy() {
        for (int i = 0; i < eggs.length; i++) {
            if (eggs[i] != null) {
                eggs[i].destroy();
            }
        }
        for (Bitmap item : bitmaps) {
            try {
                item.recycle();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 扭蛋对象
     */
    private class Egg {

        /* 活动范围宽度 */
        private int width;
        /* 活动范围高度 */
        private int height;
        /* X坐标 */
        private int x;
        /* Y坐标 */
        private int y;
        /* X轴动画偏移量 */
        private int offsetX;
        /* Y轴动画偏移量 */
        private int offsetY;
        /* 扭蛋图片 */
        private Bitmap bitmap;
        private ValueAnimator xValue;
        private ValueAnimator yValue;

        /**
         * 构造方法
         *
         * @param width  活动范围的宽度
         * @param height 活动范围的高度
         * @param bitmap 扭蛋图片
         */
        public Egg(int width, int height, Bitmap bitmap) {
            this.width = width - bitmap.getWidth();
            this.height = height - bitmap.getHeight();
            this.bitmap = bitmap;
            this.init();
        }

        /* 初始化 */
        private void init() {
//            this.x = random(0, width);
            this.x = 0;
//            this.y = random(0, height);
            this.y = 0;
            xValue = ValueAnimator.ofInt(0, width);
            xValue.setDuration(random(200, 1000));
            xValue.setRepeatCount(-1);
            xValue.setRepeatMode(ValueAnimator.REVERSE);
            xValue.setInterpolator(new LinearInterpolator());
            xValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    offsetX = (int) animation.getAnimatedValue();
                }
            });
            xValue.start();
            yValue = ValueAnimator.ofInt(0, height);
            yValue.setDuration(random(200, 1000));
            yValue.setRepeatCount(-1);
            yValue.setRepeatMode(ValueAnimator.REVERSE);
            yValue.setInterpolator(new LinearInterpolator());
            yValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    offsetY = (int) animation.getAnimatedValue();
                }
            });
            yValue.start();
        }

        public int getX() {
            return x + offsetX;
        }

        public int getY() {
            return y + offsetY;
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }

        /**
         * 随机取数
         *
         * @param min 最小值
         * @param max 最大值
         * @return 返回一个在最小值和最大值之间的数
         */
        private int random(int min, int max) {
            Random r = new Random();
            return r.nextInt(max) % (max - min + 1) + min;
        }

        public void destroy() {
            if (xValue != null) {
                xValue.removeAllUpdateListeners();
                xValue.cancel();
            }
            if (yValue != null) {
                yValue.removeAllUpdateListeners();
                yValue.cancel();
            }
        }
    }
}
