package com.cygnusa.hyperm.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.cygnusa.hyperm.BulletinDetails;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.Bulletin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


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

    private ArrayList<Bulletin> items;
    private Context context;
    private Bulletin TOPIC;

    public BulletinAdapter(Context context, ArrayList<Bulletin> item) {

        this.items = item;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.bulletin_item, parent, false);

        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Bulletin item = items.get(position);

        holder.view.setTag(item);
        holder.title.setText(item.getBulletin_title());
        holder.description.setText(item.getBulletin_desc());
        holder.description.setSelected(true);
        holder.title.setSelected(true);
        try {
            holder.publishedDate.setText("Published On: " + dateFormate(item.getPublished_date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        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);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BulletinDetails.CONTENT = (Bulletin) v.getTag();
                Intent bulletinIntent = new Intent(context, BulletinDetails.class);
                bulletinIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                bulletinIntent.putExtra("bulletinId", item.getBulletin_id());
                context.startActivity(bulletinIntent);

            }
        });

    }


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

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

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

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView rightArrow, logo;
        CircularImageView image;
        TextView title, description, publishedDate;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.text_title);
            description = itemView.findViewById(R.id.text_desc);
            publishedDate = itemView.findViewById(R.id.publishedDate);
            logo = itemView.findViewById(R.id.logo);
            rightArrow = itemView.findViewById(R.id.right_arrow);
        }

    }

    public String dateFormate(String dte) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = null;
        try {
            newDate = format.parse(dte);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        format = new SimpleDateFormat("dd-MMM-yyyy");
        String date = format.format(newDate);


        return date;
    }
}
