package com.cooleshow.base.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cooleshow.base.R;
import com.cooleshow.base.ui.video.VideoPlayActivity;

/**
 * Author by pq, Date on 2022/4/27.
 */
public class VideoThumbnailView extends FrameLayout {

    private QMUIRadiusImageView mIvBg;
    private String videoPath;

    public VideoThumbnailView(@NonNull Context context) {
        this(context, null);
    }

    public VideoThumbnailView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public VideoThumbnailView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_homework_video_thumbnail_layout, this);
        mIvBg = findViewById(R.id.iv_bg);
        mIvBg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(videoPath)) {
                    VideoPlayActivity.start(getContext(), videoPath);
                }
            }
        });
    }

    public void setData(String videoUrl) {
        this.videoPath = videoUrl;
        Glide.with(getContext())
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(0)
                                .centerCrop()
                                .error(com.cooleshow.base.R.drawable.bg_video_placeholder)
                                .placeholder(com.cooleshow.base.R.drawable.bg_video_placeholder)
                )
                .load(videoUrl)
                .into(mIvBg);
    }
}
