package com.cooleshow.student.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
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.base.utils.GlideUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.student.R;
import com.cooleshow.student.bean.HomeStyleBean;

import org.w3c.dom.Text;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * Author by pq, Date on 2022/8/30.
 */
public class HomeTalentStyleAdapter extends BaseQuickAdapter<HomeStyleBean.RowsBean, BaseViewHolder> implements LoadMoreModule {
    private int long_img_height = 0;
    private int normal_img_height = 0;

    public HomeTalentStyleAdapter() {
        super(R.layout.item_home_talent_style_layout);
        long_img_height = SizeUtils.dp2px(222);
        normal_img_height = SizeUtils.dp2px(168);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, HomeStyleBean.RowsBean data) {
        ImageView coverImagView = holder.getView(R.id.iv_cover);
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) coverImagView.getLayoutParams();
        int position = holder.getLayoutPosition();
        if (position == 0) {
            layoutParams.height = long_img_height;
        } else {
            layoutParams.height = normal_img_height;
        }
        coverImagView.setLayoutParams(layoutParams);
        boolean isLiving = TextUtils.equals(data.liveStatus, "1");
        //封面
        GlideUtils.INSTANCE.loadTopRoundImage(getContext(), isLiving ? data.liveCoverImg : data.cover, coverImagView, 10, com.cooleshow.base.R.drawable.bg_video_placeholder);
        //直播标题
        TextView tv_title = holder.getView(R.id.tv_title);
        tv_title.setVisibility(isLiving ? View.VISIBLE : View.GONE);
        tv_title.setText(data.liveTitle);

        //头像
        ImageView iv_avatar = holder.getView(R.id.iv_avatar);
        GlideUtils.INSTANCE.loadImage(getContext(), data.avatar, iv_avatar, R.drawable.icon_teacher_default_head);
        //昵称
        holder.setText(R.id.tv_name, data.username);
        TextView tv_tip = holder.getView(R.id.tv_tip);
        View tv_live_anim_tip = holder.getView(R.id.tv_live_anim_tip);
        LottieAnimationView iv_live_anim = holder.getView(R.id.iv_live_anim);
        View view_live_bg = holder.getView(R.id.view_live_bg);
        if (isLiving) {
            view_live_bg.setVisibility(View.VISIBLE);
            tv_live_anim_tip.setVisibility(View.VISIBLE);
            iv_live_anim.setVisibility(View.VISIBLE);
            iv_live_anim.playAnimation();
            tv_tip.setText("正在直播");
            tv_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff0000));
        } else {
            view_live_bg.setVisibility(View.GONE);
            tv_live_anim_tip.setVisibility(View.GONE);
            iv_live_anim.pauseAnimation();
            iv_live_anim.setVisibility(View.GONE);
            tv_tip.setText(String.format("%d人观看", data.browse));
            tv_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
        }
    }

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