package com.prank.funnypranksounds.ui.adapter;

import android.app.Activity;
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.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.prank.funnypranksounds.R;
import com.prank.funnypranksounds.data.local.entity.AudioData;
import com.prank.funnypranksounds.ui.activity.AudioPlayerActivity;
import com.prank.funnypranksounds.ui.activity.CategoryActivity;
import com.prank.funnypranksounds.viewmodel.AudioViewModel;

import java.util.List;

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

    private List<AudioData> audioDataList;
    private AudioViewModel audioViewModel;
    private final Context context;
    private final Activity activity;
    private final int type;
    private final int[] colors;

    public AudioAdapter(List<AudioData> audioDataList, AudioViewModel audioViewModel, Context context, Activity activity, int type) {
        this.context = context;
        this.audioViewModel = audioViewModel;
        this.audioDataList = audioDataList;
        this.activity = activity;
        this.type = type;

        colors = new int[]{
                R.color.color1,
                R.color.color2,
                R.color.color3,
                R.color.color4,
                R.color.color5,
                R.color.color6,
                R.color.color7
        };
    }

    public void updateData(List<AudioData> newAudioDataList) {
        this.audioDataList = newAudioDataList;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AudioData audioData = audioDataList.get(position);
        String name = "";

        if (type == 0) {
            name = audioData.getCategoryName();
        } else if (type == 1) {
            name = audioData.getTitle();
        }

        holder.bind(audioData, name,position);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView like;
        TextView textView;
        ConstraintLayout constraintLayout;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            like = itemView.findViewById(R.id.like);
            textView = itemView.findViewById(R.id.textView);
            constraintLayout = itemView.findViewById(R.id.main);
        }

        void bind(AudioData audioData, String name,int position) {
            String audioDataImagePath = "";
            textView.setVisibility(View.VISIBLE);
            setItemText(name);

            constraintLayout.setBackgroundTintList(ContextCompat.getColorStateList(context, colors[position % colors.length]));

            if (type == 0) {
                audioDataImagePath = audioData.getCategoryUrl();
            }else if (type == 1){
                audioDataImagePath = audioData.getPreUrl();
            }

            loadImage(audioDataImagePath);
            setLike(audioData);
            setClickListeners(audioData, name);
        }

        private void loadImage(String imagePath) {
            Glide.with(context)
                    .load(imagePath)
                    .placeholder(R.mipmap.placeholder)
                    .error(R.mipmap.placeholder)
                    .transform(new RoundedCorners(32))
                    .into(imageView);
        }

        private void setItemText(String name) {
            textView.setText(name);
        }

        private void setLike(AudioData audioData) {
            like.setImageResource(audioData.isFavorite()
                    ? R.drawable.like
                    : R.drawable.un_like);
        }

        private void setClickListeners(final AudioData audioData, String name) {
            imageView.setOnClickListener(v -> {
                Intent intent;
                if (type == 0) {
                    intent = new Intent(context, CategoryActivity.class);
                    intent.putExtra("name", name);
                } else {
                    intent = new Intent(context, AudioPlayerActivity.class);
                    intent.putExtra("audioData", audioData);
                }
                context.startActivity(intent);
            });

            like.setOnClickListener(v -> toggleFavorite(audioData));
        }

        private void toggleFavorite(AudioData audioData) {
            audioViewModel.update(audioData);

            boolean newStatus = !audioData.isFavorite();
            audioData.setFavorite(newStatus);

            notifyItemChanged(getAdapterPosition());
        }

    }
}