package com.app.simplitend.articles;

import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.databinding.ArticleViewHolderBinding;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class ArticlesAdapter extends ListAdapter<ArticleResult, ArticlesAdapter.ArticleViewHOlder> {

    private final ArticleClickListener articleClickListener;

    private static final DiffUtil.ItemCallback<ArticleResult> DIFF_UTIL = new DiffUtil.ItemCallback<ArticleResult>() {
        @Override
        public boolean areItemsTheSame(@NonNull ArticleResult oldItem, @NonNull ArticleResult newItem) {
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull ArticleResult oldItem, @NonNull ArticleResult newItem) {
            try {
                return oldItem.large_image.equals(newItem.large_image) &&
                        oldItem.article_title.equals(newItem.article_title) &&
                        oldItem.article_author.equals(newItem.article_author) &&
                        oldItem.created_at.equals(newItem.created_at);
            } catch (Exception e) {
                return false;
            }
        }
    };

    public ArticlesAdapter(@NonNull ArticleClickListener articleClickListener){
        super(DIFF_UTIL);
        this.articleClickListener = articleClickListener;
    }

    @NonNull
    @Override
    public ArticleViewHOlder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ArticleViewHolderBinding binding = ArticleViewHolderBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent, false);
        return new ArticleViewHOlder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ArticleViewHOlder holder, int position) {
        holder.setData(getItem(position));

        holder.binding.card.setOnClickListener(v -> {
            articleClickListener.onArticleClick(getItem(position));
        });
    }

    public static class ArticleViewHOlder extends RecyclerView.ViewHolder{
        public ArticleViewHolderBinding binding;

        public ArticleViewHOlder(@NonNull ArticleViewHolderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(ArticleResult articleResult){
            if (articleResult == null) return;

            Glide.with(binding.articleImg)
                    .load(RetrofitHelper.IMAGE_BASE_URL + articleResult.large_image)
                    .placeholder(android.R.color.darker_gray)
                    .error(android.R.color.darker_gray)
                    .into(binding.articleImg);

            if (articleResult.article_title == null){
                articleResult.article_title = "No title";
            }

            binding.title.setText(articleResult.article_title);

            if (articleResult.article_author == null){
                articleResult.article_author = "No author";
            }

            binding.authorName.setText(articleResult.article_author);

            try {
                if (articleResult.created_at == null) throw new Exception();
                // parsing time of created at
                SimpleDateFormat format = new SimpleDateFormat(
                        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
                format.setTimeZone(TimeZone.getTimeZone("UTC"));

                Date date = format.parse(articleResult.created_at);
                if (date == null) throw new Exception();

                SimpleDateFormat pretty_format = new SimpleDateFormat("dd MMM yy, hh:mm a", Locale.getDefault());
                String time = pretty_format.format(date);

                binding.createdAt.setText(time);

            }catch (Exception e){
                // do nothing
                String none = "None";
                binding.createdAt.setText(none);
            }

        }
    }

    // interfaces
    @FunctionalInterface
    public interface ArticleClickListener{
        void onArticleClick(ArticleResult articleResult);
    }
}
