package com.wall.exquisite.wallpaper;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class CollectionFragment extends Fragment {
    private RecyclerView rvFavoriteImages;
    private AnimalsImageAdapter adapter;
    private CollectionManager favoriteManager;
    private List<Wallpaper_Item> favoriteList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_animals, container, false);

        rvFavoriteImages = view.findViewById(R.id.rv_4d_images);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        rvFavoriteImages.setLayoutManager(gridLayoutManager);

        favoriteManager = new CollectionManager(getContext());
        loadFavoriteData();

        adapter = new AnimalsImageAdapter(getContext(), favoriteList);
        rvFavoriteImages.setAdapter(adapter);

        adapter.setOnItemClickListener((position, wallpaperItem) -> {
            if (getContext() == null || wallpaperItem == null) {
                Toast.makeText(getContext(), "Data error", Toast.LENGTH_SHORT).show();
                return;
            }

            String highResImageUrl = wallpaperItem.getUrls().getRegular();
            if (highResImageUrl == null || highResImageUrl.isEmpty()) {
                highResImageUrl = wallpaperItem.getUrls().getFull();
            }

            if (highResImageUrl == null || highResImageUrl.isEmpty()) {
                Toast.makeText(getContext(), "NULL", Toast.LENGTH_SHORT).show();
                return;
            }

            Intent intent = new Intent(getContext(), FullImageActivity.class);
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_URL, highResImageUrl);
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_ID, wallpaperItem.getId());

            String description = wallpaperItem.getAlt_description();
            if (description == null || description.isEmpty()) {
                description = "Favorited wallpapers";
            }
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_DESC, description);

            startActivity(intent);
        });

        return view;
    }

    private void loadFavoriteData() {
        favoriteList = favoriteManager.getFavoriteItems();
        // 移除此处的Toast提示，避免重复触发
    }

    @Override
    public void onResume() {
        super.onResume();
        loadFavoriteData();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}