package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.content.Intent;
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.ContentListActivity;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.PositionUpdateListener;
import com.cygnusa.hyperm.util.StorePreference;
import com.cygnusa.hyperm.wrappers.MyTopic;
import com.cygnusa.hyperm.wrappers.TrainingCategory;

import java.util.ArrayList;

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

    ArrayList<MyTopic> items;
    private Context context;
    private String CATEGORY_ID = "";
    // private String TRAINING_ID = "";
    private PositionUpdateListener listener;
    private TrainingCategory TRAINING_ID;
    StorePreference storePreference;
    public TrainingAdapter(Context context, ArrayList<MyTopic> items,TrainingCategory trainingId) {
        this.items = items;
        this.TRAINING_ID=trainingId;
        this.context = context;
        storePreference = new StorePreference(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.training_item, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storePreference.setBoolean(GlobalData.ISMYLEARNING,true);
                MyTopic myTopic = (MyTopic) v.getTag();
                ContentListActivity.TOPIC = myTopic;
                ContentListActivity.TRAINING_ID = TRAINING_ID;
                if (listener != null) {
                    listener.onPositionUpdate(items.indexOf(myTopic));
                }
                context.startActivity(new Intent(context, ContentListActivity.class));
            }
        });
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyTopic item = items.get(position);
        holder.view.setTag(item);
        holder.text.setText(item.getTopicname());
        Glide.with(context).load(item.getImage_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(MyTopic item,TrainingCategory myTrainingItem, int position) {
        items.add(position, item);
        notifyItemInserted(position);
    }

    public void add(MyTopic item,TrainingCategory myTrainingItem) {
        items.add(item);
        notifyItemInserted(items.size());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView text, targetText;
        View view;

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

        }

    }
}
