package com.fun.airhornprank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
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.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fun.airhornprank.PrankSkin;
import com.fun.airhornprank.R;
import com.fun.airhornprank.activity.ListActivity;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.MainRvItemBinding;
import com.fun.airhornprank.tool.StaticValue;

import java.util.List;

public class ThumAdapter extends RecyclerView.Adapter<ThumAdapter.ThumVH> {
    private List<Mydata> myDataList;

    private LayoutInflater minflater;
    private int[] randomcolor;
    private int[] draw;

    public ThumAdapter(Activity activity, List<Mydata> list) {
        myDataList = list;
        minflater = LayoutInflater.from(activity);

        randomcolor = new int[]{
                ContextCompat.getColor(minflater.getContext(), R.color.color_1),
                ContextCompat.getColor(minflater.getContext(), R.color.color_2),
                ContextCompat.getColor(minflater.getContext(), R.color.color_3),
                ContextCompat.getColor(minflater.getContext(), R.color.color_4),
                ContextCompat.getColor(minflater.getContext(), R.color.color_5),
                ContextCompat.getColor(minflater.getContext(), R.color.color_6),
        };

        draw = new int[]{
                R.drawable.bg_1,
                R.drawable.bg_2,
                R.drawable.bg_3,
                R.drawable.bg_4,
                R.drawable.bg_5,
                R.drawable.bg_6,
        };
    }

    @NonNull
    @Override
    public ThumVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MainRvItemBinding binding = MainRvItemBinding.inflate(minflater, parent, false);
        return new ThumVH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ThumVH holder, int position) {
        Mydata myData = myDataList.get(position);
        String url = myData.getCategoryUrl();

        if (url.equals("custom_url")) {
            holder.getImageViewThumb().setImageResource(R.mipmap.save_image);
        } else {
            Glide.with(minflater.getContext())
                    .load(myData.getCategoryUrl())
                    .placeholder(R.mipmap.logo)
                    .into(holder.getImageViewThumb());
        }

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
        } else if (position == getItemCount() - 2) {
            holder.getView().setVisibility(View.VISIBLE);
        } else {
            holder.getView().setVisibility(View.GONE);
        }

        if (myData.getCategoryname().length() > 6) {
            String name = myData.getCategoryname().substring(0, 6);

            holder.getTextView().setText(name + "...");
        } else {
            holder.getTextView().setText(myData.getCategoryname());
        }

        int i = position % 6;
        Drawable backgroundDrawable1 = ContextCompat.getDrawable(minflater.getContext(), draw[i]);
        if (backgroundDrawable1 != null) {
            holder.getMainlistcons().setBackground(backgroundDrawable1);
        }

        holder.getConstraintLayout().setOnClickListener(v -> {
            Intent intent = new Intent(minflater.getContext(), ListActivity.class);
            intent.putExtra(StaticValue.KEY_CategoryName, myData.getCategoryname());
            intent.putExtra(StaticValue.KEY_Color, randomcolor[i]);
            minflater.getContext().startActivity(intent);
        });
    }


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

    public class ThumVH extends RecyclerView.ViewHolder {

        private ImageView imageViewThumb;
        private ConstraintLayout constraintLayout, mainlistcons;
        private TextView textView;
        private View view;


        public ThumVH(@NonNull MainRvItemBinding binding) {
            super(binding.getRoot());
            imageViewThumb = binding.mainRvImage;
            constraintLayout = binding.mainCons;
            mainlistcons = binding.mainListCons;
            textView = binding.mainRvText;
            view = binding.view;
        }

        public View getView() {
            return view;
        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

        public ConstraintLayout getMainlistcons() {
            return mainlistcons;
        }

        public TextView getTextView() {
            return textView;
        }
    }
}
