package com.wallpaper.wallpaperbox.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.wallpaper.wallpaperbox.R;
import com.wallpaper.wallpaperbox.data.entity.WallpaperEntity;
import com.wallpaper.wallpaperbox.topon.AdManager;
import com.wallpaper.wallpaperbox.topon.onActionListener;
import com.wallpaper.wallpaperbox.ui.activity.DetailActivity;
import com.wallpaper.wallpaperbox.ui.activity.WallpaperListActivity;
import com.wallpaper.wallpaperbox.ui.viewmodel.WallpaperViewModel;

import java.util.List;

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.ViewHolder> {
    private final WallpaperViewModel wallpaperViewModel;
    private final Context context;
    private List<WallpaperEntity> wallpaperEntries;
    private final Activity activity;
    private final int type;

    public WallpaperAdapter(WallpaperViewModel wallpaperViewModel, Context context, List<WallpaperEntity> wallpaperEntries, Activity activity, int type) {
        this.wallpaperViewModel = wallpaperViewModel;
        this.context = context;
        this.wallpaperEntries = wallpaperEntries;
        this.activity = activity;
        this.type = type;
    }

    public void updateData(List<WallpaperEntity> newWallpaperEntries) {
        this.wallpaperEntries = newWallpaperEntries;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WallpaperEntity wallpaperEntity = wallpaperEntries.get(position);
        holder.bind(wallpaperEntity);
        int randomHeight = (position % 2 == 0) ? 800 : 1000;
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = randomHeight;
        holder.itemView.setLayoutParams(params);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageView;
        private final ImageView favorite;
        private final TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_image_view);
            favorite = itemView.findViewById(R.id.item_favorite);
            title = itemView.findViewById(R.id.item_title);
        }

        public void bind(WallpaperEntity wallpaperEntity) {
            String imagePath = wallpaperEntity.getPreviewThumb();
            loadImage(imagePath);

            if (type == 0) {
                title.setText(wallpaperEntity.getName());
                Log.d("sds", "dsdwdwd" + wallpaperEntity.getName());
                favorite.setVisibility(View.GONE);
            } else {
                title.setVisibility(View.GONE);
                setFavoriteButton(wallpaperEntity);
            }

            setClickListeners(wallpaperEntity);
        }

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

        private void setFavoriteButton(WallpaperEntity wallpaperEntity) {
            favorite.setImageResource(wallpaperEntity.getLike() ? R.drawable.like : R.drawable.un_like);
        }

        private void setClickListeners(WallpaperEntity wallpaperEntity) {
            imageView.setOnClickListener(view -> {
                AdManager.showTopOn(activity, new onActionListener() {
                    @Override
                    public void onAction() {
                        Intent intent;
                        if (type == 0) {
                            intent = new Intent(activity, WallpaperListActivity.class);
                            intent.putExtra("name", wallpaperEntity.getName());
                        } else {
                            intent = new Intent(activity, DetailActivity.class);
                            intent.putExtra("wallpaper", wallpaperEntity);
                        }
                        activity.startActivity(intent);
                    }
                });
            });

            favorite.setOnClickListener(view -> toggleFavorite(wallpaperEntity));
        }

        private void toggleFavorite(WallpaperEntity wallpaperEntity) {
            boolean newStatus = !wallpaperEntity.getLike();
            wallpaperEntity.setLike(newStatus);
            updateImageInDatabase(wallpaperEntity);
            notifyItemChanged(getAdapterPosition());
        }

        private void updateImageInDatabase(WallpaperEntity wallpaperEntity) {
            wallpaperViewModel.update(wallpaperEntity);
        }
    }
}

