package com.app.input.personalised.board.listhelpers;

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.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.boartactivity.ThemeDetailActivity;
import com.app.input.personalised.board.helpers.AppUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class RecommendedGridAdapter extends RecyclerView.Adapter<RecommendedGridAdapter.RecommendedViewHolder> {

    private Context mContext;
    private List<KeyboardTheme> mList = new ArrayList<>();

    public RecommendedGridAdapter(Context context) {
        mContext = context;
    }

    public void setForYouList(List<KeyboardTheme> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecommendedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_theme_with_name, parent, false);
        return new RecommendedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendedViewHolder holder, int position) {
        KeyboardTheme beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        String title = beanDetails.getTitleName();

        holder.tvName.setText(title != null ? title : "");

        if (thumbGif != null && !thumbGif.isEmpty()) {
            AppUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else if (thumb != null && !thumb.isEmpty()) {
            Glide.with(mContext)
                    .load(thumb)
                    .placeholder(R.drawable.placeholder)
                    .error(R.drawable.placeholder)
                    .centerCrop()
                    .into(holder.itemImg);
        } else {
            holder.itemImg.setImageResource(R.drawable.placeholder);
        }

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ThemeDetailActivity.class);
                intentApply.putExtra(ThemeDetailActivity.SOURCE_KEY, beanDetails);
                intentApply.putExtra(ThemeDetailActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
                intentApply.putExtra(ThemeDetailActivity.ZIP_URL_KEY, beanDetails.getZipPath());
                intentApply.putExtra(ThemeDetailActivity.NAME_KEY, beanDetails.getTitleName());
                intentApply.putExtra(ThemeDetailActivity.GIF_KEY, beanDetails.getImgGif());
                String intent_thumb;
                if (thumbGif != null && !thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(ThemeDetailActivity.THUMB_KEY, intent_thumb);
                mContext.startActivity(intentApply);
            }
        });
    }

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

    public static class RecommendedViewHolder extends RecyclerView.ViewHolder {

        private CardView cardView;
        private ImageView itemImg;
        private TextView tvName;

        public RecommendedViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            itemImg = itemView.findViewById(R.id.imPreview);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}

