package com.bokecc.livemodule.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bokecc.dwlivedemo.R;

import java.lang.ref.WeakReference;
import java.util.Random;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author:
 * Date: 2023/1/9
 * Description:
 */
public class LivePlayAnimationView extends View {
    private Context mContext;
    Paint mPaint;
    int mColor = Color.BLUE;
    int viewWidth = 0;
    int viewHeight = 0;

    private PlayHandler mPlayHandler;

    int oneHeight = 0;
    int twoHeight = 0;
    int threeHeight = 0;

    public LivePlayAnimationView(Context context) {
        this(context, null);
    }

    public LivePlayAnimationView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LivePlayAnimationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mColor = mContext.getResources().getColor(R.color.vertical_live_primary_orange);
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(SizeUtils.dp2px(mContext,2f));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        oneHeight = viewHeight / 3 * 2;
        twoHeight = viewHeight / 3;
        threeHeight = 0;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(mColor);

        canvas.drawLine(viewWidth / 4, oneHeight, viewWidth / 4, viewHeight, mPaint);
        canvas.drawLine(viewWidth / 2, twoHeight, viewWidth / 2, viewHeight, mPaint);
        canvas.drawLine(viewWidth / 4 * 3, threeHeight, viewWidth / 4 * 3, viewHeight, mPaint);
    }

    public void playVideo() {
        mPlayHandler = new PlayHandler(this);
        mPlayHandler.sendEmptyMessageDelayed(0, 500);
    }

    public void stopVideo() {
        mPlayHandler.removeCallbacksAndMessages(null);
    }

    private static class PlayHandler extends Handler {
        private WeakReference<LivePlayAnimationView> weakReference;
        private int height = 0;

        public PlayHandler(LivePlayAnimationView videoPlayAnimationView) {
            weakReference = new WeakReference<>(videoPlayAnimationView);
        }

        @Override
        public void handleMessage(Message msg) {
            LivePlayAnimationView view = weakReference.get();
            if (view != null && view.viewHeight > 0) {
                view.oneHeight = new Random().nextInt(view.viewHeight);
                view.twoHeight = new Random().nextInt(view.viewHeight);
                view.threeHeight = new Random().nextInt(view.viewHeight);
                view.invalidate();
            }
            sendEmptyMessageDelayed(0, 500);
            super.handleMessage(msg);
        }
    }

}
