package w.soulofpluto.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Model> arrayList;
    public CustomAdapter(Context context, ArrayList<Model> list){
     this.context   = context;
     this.arrayList = list;
    }
    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
        View View = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_list_item, viewGroup, false);
        return new CustomAdapter.ViewHolder(View);
    }

    public void addItem(int position, Model model) {
        arrayList.add(position, model);
        notifyItemRangeChanged(position, arrayList.size());
        notifyItemInserted(position);
    }

    public void modifyItem(final int position, final Model model) {
        //arrayList.remove(position);
        arrayList.set(position, model);
        notifyItemChanged(position);
    }
    @Override
    public void onBindViewHolder(CustomAdapter.ViewHolder viewHolder, int i){
        final Model model = arrayList.get(i);
        viewHolder.item_sku.setText(model.getSKU());
        //Bitmap bitmap = BitmapFactory.decodeFile(model.getFILEPATH());
        Glide.with(context)
                .load(model.getFILEPATH())
                //.asBitmap()
                //.load(bitmap)
                .into(viewHolder.item_image);
        //viewHolder.item_no.setText(String.valueOf(model.getID()));
        //viewHolder.item_image.setImageBitmap(bitmap);
        //System.out.println("Status ==>   "+model.getSTATUS());
        if(model.getSTATUS() == 0){
            viewHolder.item_progress.setText("In Progress");
        }else if(model.getSTATUS() ==1){
            viewHolder.item_progress.setText("Completed");
        }
    }
    @Override
    public int getItemCount(){
        return arrayList.size();
    }
    public void updateAt(int pos, int i) {
        Model todoEntity = arrayList.get(pos);
        //todoEntity.setText(text);
        todoEntity.setSTATUS(i);
        notifyItemChanged(pos);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView item_sku,item_progress,item_no;
        private ImageView item_image;
        public ViewHolder(View itemView){
            super(itemView);
            item_sku           = itemView.findViewById(R.id.item_sku);
            item_image         = itemView.findViewById(R.id.item_image);
            item_progress      = itemView.findViewById(R.id.item_progress);
            //item_no            = itemView.findViewById(R.id.item_no);

        }
    }
}
