package com.ycgis.macall.personalcenter.v.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.m.adapterbean.AppGuideDetailsBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2022/11/18 14:29
 * copyright:    @ruansee.com
 * Describe:
 */
public class AppGuideDetailsAdapter extends BaseRecyclerAdapter<AppGuideDetailsBean,BaseRecyclerAdapter.ViewHolder> {

    public AppGuideDetailsAdapter(Context context, List<AppGuideDetailsBean> dataList) {
        super(context, dataList);
    }

    @Override
    public int getItemViewType(int position) {
        return getDataItem(position).getBaseType();
    }

    @NonNull
    @NotNull
    @Override
    public BaseRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 0||viewType == -1){
            View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error,parent,false);
            return new ErrorViewHolder(view);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_operation_type,parent,false);
        return new SimpleViewHolder(view).findViewById(R.id.ic_img).findViewById(R.id.tv_op_name).findViewById(R.id.iv_play);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder){
            ErrorViewHolder holder = (ErrorViewHolder)viewHolder;
            if (getDataItem(position).getBaseType() == 0){
                holder.setEmpty(getDataItem(position).getBaseMessage());
            }else {
                holder.setError(getDataItem(position).getBaseMessage());
            }
        }
        if (viewHolder instanceof BaseRecyclerAdapter.SimpleViewHolder){
            AppGuideDetailsBean dataItem = getDataItem(position);
            SimpleViewHolder holder = (SimpleViewHolder) viewHolder;
            ImageView ic_img = holder.getView(R.id.ic_img, ImageView.class);
            ImageView iv_play = holder.getView(R.id.iv_play, ImageView.class);
            TextView tv_name = holder.getView(R.id.tv_op_name, TextView.class);
            String filePath = dataItem.getFilePath();
            int i = filePath.lastIndexOf("/");
            String substring = filePath.substring(i+1);
            tv_name.setVisibility(View.VISIBLE);
            tv_name.setText(substring);
            if (dataItem.getType() == 1){
                //图片
                setImg(ic_img,getDataItem(position).getFilePath());
                iv_play.setVisibility(View.GONE);
            }else if (dataItem.getType() ==2){
                //视频
                loadVideoScreenshot(context,filePath,ic_img);
                iv_play.setVisibility(View.VISIBLE);
            }else {
                //文件
                iv_play.setVisibility(View.GONE);
                if (wd.contains(substring)){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_doc));
                }else if (ex.contains(substring)){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_xls));
                }else if (zip.contains(substring)){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_zip));
                }else if (substring.endsWith(pft)){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_pfd));
                }else if (substring.endsWith(ppt)){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_ppt));
                }else if (substring.endsWith("txt")){
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_txt));
                }else {
                    ic_img.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fileselect_img_unknown));
                }
            }
        }
    }
    private String wd = "doc,docx";
    private String ex = "xls,xlsx";
    private String zip = "rar,zip";
    private String ppt = "ppt";
    private String pft = "pfd";

    public static void loadVideoScreenshot(final Context context, String url, ImageView imageView) {
        String imgUrl = AppCache.BASE_IMAGE_URL + url;
        Glide.with(context)
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(0)
                                .centerCrop()
                )
                .load(imgUrl)
                .into(imageView);
    }

    private void setImg(ImageView img, String url){
        String imgUrl = AppCache.BASE_IMAGE_URL + url;
        //图片加载自己实现
        Glide.with(img)
                .load(imgUrl)
                .into(img);
    }

}
