package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
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.PositionUpdateListener;
import com.cygnusa.hyperm.wrappers.ContentFile;
import com.cygnusa.hyperm.wrappers.MyContent;
import com.cygnusa.hyperm.wrappers.MyTopic;

import java.util.ArrayList;

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

    private ArrayList<MyContent> items;
    private ArrayList<ContentFile> CONTENT = new ArrayList<>();;
    private Context context;
    private String CONTENT_ID = "";
    private PositionUpdateListener listener;

    public TopicAdapter(Context context, ArrayList<MyContent> items, String id, PositionUpdateListener l) {
        this.items = items;
        this.context = context;
        this.CONTENT_ID = id;
        this.listener = l;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyContent item = items.get(position);
        holder.view.setTag(item);
        holder.text.setText(item.getTitle());
        if (item.getStatus().trim().equalsIgnoreCase("0")) {
            holder.statusBar.setBackgroundColor(Color.parseColor("#B5B5B5"));
        } else if (item.getStatus().trim().equalsIgnoreCase("1")) {
            holder.statusBar.setBackgroundColor(Color.parseColor("#F98315"));
        } else if (item.getStatus().trim().equalsIgnoreCase("2")) {
            holder.statusBar.setBackgroundColor(Color.parseColor("#1D7210"));
        }
        Glide.with(context).load(item.getContent_files().get(0).getThumb_fileurl())
                .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(MyContent item, int position) {
        items.add(position, item);
        notifyItemInserted(position);
    }

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

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

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

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

    }
}
