package com.cygnusa.hyperm.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.MyTopic;
import com.cygnusa.hyperm.wrappers.TargetSales;

import java.util.ArrayList;


public class TargetSalesAdapter extends RecyclerView.Adapter<TargetSalesAdapter.ViewHolder> {

    private ArrayList<TargetSales> items;
    private Context context;
    private MyTopic TOPIC;

    public TargetSalesAdapter(Context context, ArrayList<TargetSales> items) {
        this.items = items;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.target_sales_item, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TargetSales item = items.get(position);
        holder.view.setTag(item);
        holder.title.setText(item.getModel_name());
        holder.targetText.setText(String.format("%s/%s",item.getAchieved(),item.getTarget()));
        holder.title.setSelected(true);
            Glide.with(context).load(item.model_image_url)
                    .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                    .apply(new RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
                            .placeholder(R.drawable.placeholder)).into(holder.image);




    }



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

    public void add(TargetSales item, int position) {
        items.add(position, item);
        notifyItemInserted(position);
    }

    public void add(TargetSales item) {
        items.add(item);
        notifyItemInserted(items.size());
    }

    public void remove(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView statusBar, logo;
        ImageView image;
        TextView title, targetText;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.text);
            targetText = itemView.findViewById(R.id.targetText);


        }

    }
}
