package com.bonait.bnframework.common.filepicker.adapter;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.filepicker.model.FileEntity;
import com.bonait.bnframework.common.filepicker.util.FileUtils;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.modules.home.adapter.image_sp_adapter;
import com.bumptech.glide.Glide;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.io.File;
import java.util.ArrayList;

/**
 * 作者：chs on 2017-08-28 10:54
 * 邮箱：657083984@qq.com
 * 选择附件后显示
 */

public class FilePickerShowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private ArrayList<FileEntity> mDataList;
    private OnDeleteListener mOnDeleteListener;
    private OnFileItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnFileItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        mOnDeleteListener = onDeleteListener;
    }

    public FilePickerShowAdapter(Context context, ArrayList<FileEntity> dataList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mDataList = dataList;
    }

    @Override
    public FileShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =mLayoutInflater.inflate(R.layout.item_file_picker_show, parent, false);
        return new FileShowViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder myViewHolder, int position) {
        try {
            FileShowViewHolder holder = (FileShowViewHolder) myViewHolder;
            final FileEntity fileEntity = mDataList.get(position);
            final File file = fileEntity.getFile();
            holder.mTvName.setText(file.getName());
            holder.mTvDetail.setText(FileUtils.getReadableFileSize(file.length()));
            if (fileEntity.getFileType() != null) {
                String title = fileEntity.getFileType().getTitle();
                if (title.equals("IMG")) {
                    Glide.with(mContext).load(new File(fileEntity.getPath())).into(holder.mIvType);
                } else {
                    holder.mIvType.setImageResource(fileEntity.getFileType().getIconStyle());
                }
            } else {
                holder.mIvType.setImageResource(R.mipmap.file_picker_def);
            }

            holder.image_open.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.click(holder.getAdapterPosition());
                    }
                }
            });
            holder.mIvDelete.setVisibility(View.VISIBLE);
            holder.mIvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=holder.getAdapterPosition();


                    File file= mDataList.get(position).getFile();
                    if (file.exists() && file.isFile())
                    {
                        file.delete();
                        mDataList.remove(position);
                    }
                    notifyDataSetChanged();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    class FileShowViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvType, mIvDelete;
        private TextView mTvName, mTvDetail;
        private LinearLayout image_open;
        public FileShowViewHolder(View itemView) {
            super(itemView);
            mIvType = (ImageView) itemView.findViewById(R.id.iv_type);
            mIvDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mTvDetail = (TextView) itemView.findViewById(R.id.tv_detail);
            image_open=itemView.findViewById(R.id.image_open);
        }
    }
}
