package com.prank.happypranksounds.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.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.prank.happypranksounds.R;
import com.prank.happypranksounds.data.entity.HappyData;
import com.prank.happypranksounds.topon.AdManager;
import com.prank.happypranksounds.topon.onActionListener;
import com.prank.happypranksounds.ui.activity.CategoryActivity;
import com.prank.happypranksounds.ui.activity.SoundActivity;
import com.prank.happypranksounds.ui.viewmodel.HappyViewModel;

import java.util.List;

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

    private List<HappyData> happyDataList;
    private final HappyViewModel happyViewModel;
    private final Context context;
    private final int type;

    private static final int PLACEHOLDER_IMAGE = R.mipmap.placeholder;
    private static final int FAVORITE_ICON = R.drawable.favorite;
    private static final int UN_FAVORITE_ICON = R.drawable.un_favorite;

    public SoundAdapter(List<HappyData> happyDataList, HappyViewModel happyViewModel, Context context, int type) {
        this.context = context;
        this.happyViewModel = happyViewModel;
        this.happyDataList = happyDataList;
        this.type = type;
    }

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

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HappyData happyData = happyDataList.get(position);
        String name = getNameByType(happyData);
        holder.bind(happyData, name);
    }

    @Override
    public int getItemCount() {
        return happyDataList != null ? happyDataList.size() : 0;
    }

    private String getNameByType(HappyData happyData) {
        if (type == 0) {
            return happyData.getCategoryName();
        } else if (type == 1) {
            return happyData.getTitle();
        }
        return "";
    }

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

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

        void bind(HappyData happyData, String name) {
            textView.setVisibility(View.VISIBLE);
            setItemText(name);
            loadImageBasedOnType(happyData);
            setFavorite(happyData);
            setClickListeners(happyData, name);
            loadFavoriteUI();
        }

        private void loadFavoriteUI() {
            if (type == 0) {
                favorite.setVisibility(View.GONE);
            } else {
                favorite.setVisibility(View.VISIBLE);
            }
        }

        private void loadImageBasedOnType(HappyData happyData) {
            String imagePath = (type == 0) ? happyData.getCategoryUrl() : happyData.getPreUrl();
            loadImage(imagePath);
        }

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

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

        private void setFavorite(@NonNull HappyData happyData) {
            int favoriteIcon = happyData.isFavorite() ? FAVORITE_ICON : UN_FAVORITE_ICON;
            favorite.setImageResource(favoriteIcon);
        }

        private void setClickListeners(final HappyData happyData, String name) {
            imageView.setOnClickListener(v -> {
                if (context instanceof Activity) {
                    Activity activity = (Activity) context;
                    AdManager.showTopOn(activity, new onActionListener() {
                        @Override
                        public void onAction() {
                            Intent intent = getIntentBasedOnType(happyData, name);
                            context.startActivity(intent);
                        }
                    });
                }
            });


            favorite.setOnClickListener(v -> toggleFavorite(happyData));
        }

        @NonNull
        private Intent getIntentBasedOnType(HappyData happyData, String name) {
            Intent intent;
            if (type == 0) {
                intent = new Intent(context, CategoryActivity.class);
                intent.putExtra("name", name);
            } else {
                intent = new Intent(context, SoundActivity.class);
                intent.putExtra("audioData", happyData);
            }
            return intent;
        }

        private void toggleFavorite(@NonNull HappyData happyData) {
            boolean newStatus = !happyData.isFavorite();
            happyData.setFavorite(newStatus);
            happyViewModel.update(happyData);
            favorite.setImageResource(newStatus ? FAVORITE_ICON : UN_FAVORITE_ICON);
        }
    }
}
