package com.cooleshow.teacher.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.LiveListBean;

import androidx.annotation.NonNull;

/**
 * Author by pq, Date on 2022/7/7.
 */
public class LiveListAdapter extends BaseQuickAdapter<LiveListBean.RowsBean, BaseViewHolder> implements LoadMoreModule {

    private AnimationDrawable mEnterLiveAnim;

    public LiveListAdapter() {
        super(R.layout.item_live_list_layout);
        addChildClickViewIds(R.id.ll_enter_live, R.id.tv_look_playback);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, LiveListBean.RowsBean rowsBean) {
        //title
        holder.setText(R.id.tv_title, rowsBean.roomTitle);
        //直播内容
        holder.setText(R.id.tv_des, rowsBean.liveRemark);
        //结束时间
        holder.setText(R.id.tv_end_time, "结束时间：" + rowsBean.liveEndTime);
        LinearLayout ll_enter_live = holder.getView(R.id.ll_enter_live);
        ImageView iv_enter_live = holder.getView(R.id.iv_enter_live);
        TextView tv_look_playback = holder.getView(R.id.tv_look_playback);
        if (rowsBean.liveState == 1) {
            //已开始
            ll_enter_live.setVisibility(View.VISIBLE);
            tv_look_playback.setVisibility(View.INVISIBLE);
            startEnterLiveAnim(iv_enter_live);
        } else if (rowsBean.liveState == 2) {
            //已结束
            ll_enter_live.setVisibility(View.INVISIBLE);
            tv_look_playback.setVisibility(View.VISIBLE);
            iv_enter_live.clearAnimation();
        } else {
            ll_enter_live.setVisibility(View.INVISIBLE);
            tv_look_playback.setVisibility(View.INVISIBLE);
            iv_enter_live.clearAnimation();
        }
    }

    /**
     * 进入直播间动画
     */
    private void startEnterLiveAnim(ImageView targetView) {
        if (targetView != null) {
            targetView.setVisibility(View.VISIBLE);
            if (mEnterLiveAnim == null) {
                mEnterLiveAnim = new AnimationDrawable();
                mEnterLiveAnim.addFrame(getContext().getDrawable(R.drawable.icon_home_live_image1), 200);
                mEnterLiveAnim.addFrame(getContext().getDrawable(R.drawable.icon_home_live_image2), 200);
                mEnterLiveAnim.addFrame(getContext().getDrawable(R.drawable.icon_home_live_image3), 200);
                mEnterLiveAnim.addFrame(getContext().getDrawable(R.drawable.icon_home_live_image4), 200);
                mEnterLiveAnim.addFrame(getContext().getDrawable(R.drawable.icon_home_live_image5), 200);
            }
            mEnterLiveAnim.setOneShot(false);
            targetView.setImageDrawable(mEnterLiveAnim);
            mEnterLiveAnim.start();
        }
    }

    private void release() {
        if (mEnterLiveAnim != null) {
            mEnterLiveAnim.stop();
        }
    }

    @NonNull
    @Override
    public BaseLoadMoreModule addLoadMoreModule(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
