package com.cooleshow.teacher.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.TeacherSelfStyleInfoBean;

import java.util.List;

/**
 * 创建日期：2022/5/13 15:46
 *
 * @author Ryan
 * 类说明：
 */
public class MineStyleVideoAdapter extends BaseMultiItemQuickAdapter<TeacherSelfStyleInfoBean.StyleVideoBean, BaseViewHolder> {
    public static final String VIDEO_AUDIT_STATUS_DOING = "DOING";//审核中
    public static final String VIDEO_AUDIT_STATUS_PASS = "PASS";//审核通过
    public static final String VIDEO_AUDIT_STATUS_UNPASS = "UNPASS";//审核失败

    public MineStyleVideoAdapter(List<TeacherSelfStyleInfoBean.StyleVideoBean> data) {
        super(data);
        addItemType(0, R.layout.item_teacher_style_video_layout);
        addItemType(1, R.layout.item_teacher_style_video_layout_add);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, TeacherSelfStyleInfoBean.StyleVideoBean styleVideoBean) {
        switch (styleVideoBean.getItemType()) {
            case 0:
                //加载视频缩略图
                ImageView iv_video_bg = holder.getView(R.id.iv_video_bg);
                LinearLayout ll_upload_cover=holder.getView(R.id.ll_upload_cover);
                TextView tv_upload_cover=holder.getView(R.id.tv_upload_cover);
                ImageView iv_upload_video_cover=holder.getView(R.id.iv_upload_video_cover);
                if (TextUtils.isEmpty(styleVideoBean.cover)) {
                    GlideUtils.INSTANCE.loadVideoThumbnail(getContext(), styleVideoBean.videoUrl, iv_video_bg);
                    tv_upload_cover.setText("请上传视频封面");
                    tv_upload_cover.setTextColor(getContext().getResources().getColor(R.color.white));
                    ll_upload_cover.setBackgroundResource(R.drawable.shape_2dc7aa_14dp);
                    iv_upload_video_cover.setImageResource(R.drawable.icon_upload_video_cover);
                } else {
                    GlideUtils.INSTANCE.loadImage(getContext(), styleVideoBean.cover, iv_video_bg, com.cooleshow.base.R.drawable.bg_video_placeholder);
                    tv_upload_cover.setText("更换视频封面");
                    tv_upload_cover.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
                    ll_upload_cover.setBackgroundResource(R.drawable.bg_select_2dc7aa_radius_14dp);
                    iv_upload_video_cover.setImageResource(R.drawable.icon_replace_video_cover);
                }
                ll_upload_cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != itemAddClickAction) {
                            itemAddClickAction.onUploadCover(holder.getLayoutPosition());
                        }
                    }
                });

                ImageView im_delete = holder.getView(R.id.im_delete);
                im_delete.setOnClickListener(view -> {
                    if (null != itemAddClickAction) {
                        itemAddClickAction.delete(styleVideoBean.videoUrl);
                    }
                });
                //视频审核状态
                TextView tv_audit_status = holder.getView(R.id.tv_audit_status);
                if (TextUtils.equals(VIDEO_AUDIT_STATUS_DOING, styleVideoBean.authStatus)) {
                    tv_audit_status.setVisibility(View.VISIBLE);
                    tv_audit_status.setText("审核中");
                } else if (TextUtils.equals(VIDEO_AUDIT_STATUS_UNPASS, styleVideoBean.authStatus)) {
                    tv_audit_status.setVisibility(View.VISIBLE);
                    tv_audit_status.setText("审核失败");
                } else {
                    tv_audit_status.setVisibility(View.GONE);
                }
                break;
            case 1:
                ImageView iv_video_add = holder.getView(R.id.iv_video_add);
                iv_video_add.setOnClickListener(view -> {
                    if (null != itemAddClickAction) {
                        itemAddClickAction.add();
                    }
                });
                break;
        }
        holder.itemView.setOnClickListener(v -> {
            if (getItemViewType(holder.getAbsoluteAdapterPosition()) == 0) {
                if (null != itemAddClickAction) {
                    itemAddClickAction.play(styleVideoBean.videoUrl);
                }
            }
        });
    }

    private ItemAddClickAction itemAddClickAction;

    public void setItemAddClickAction(ItemAddClickAction itemAddClickAction) {
        this.itemAddClickAction = itemAddClickAction;

    }

    public interface ItemAddClickAction {
        void add();

        void play(String videoUrl);

        void delete(String url);

        void onUploadCover(int position);
    }
}
