package com.idle.wall.idlefragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.idle.wall.R;
import com.idle.wall.RecentAdapter;
import com.idle.wall.IdleWallpaper;
import com.idle.wall.RecentManager;

import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FragmentIdleRecent extends Fragment {
    private static final String TAG = "RecentWallpaperFragment";
    private static final int THREAD_POOL_SIZE = 2;

    private GridView gvRecentWallpapers;
    private TextView tvEmpty;
    private ProgressBar pbLoading;
    private RecentAdapter adapter;
    private RecentManager recentManager;
    private ExecutorService executorService;
    private Handler mainHandler;

    public static FragmentIdleRecent newInstance() {
        return new FragmentIdleRecent();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_idle_recent, container, false);
        gvRecentWallpapers = view.findViewById(R.id.gv_recent_wallpapers);
        tvEmpty = view.findViewById(R.id.tv_empty);
        pbLoading = view.findViewById(R.id.pb_loading);
        executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        mainHandler = new Handler(Looper.getMainLooper());
        recentManager = RecentManager.getInstance(requireContext());
        adapter = new RecentAdapter(requireContext(), new ArrayList<>());
        gvRecentWallpapers.setAdapter(adapter);
        loadRecentWallpapers();

        return view;
    }
    private void loadRecentWallpapers() {
        pbLoading.setVisibility(View.VISIBLE);
        tvEmpty.setVisibility(View.GONE);
        gvRecentWallpapers.setVisibility(View.GONE);

        executorService.execute(() -> {
            List<IdleWallpaper> list = recentManager.getRecentWallpapers();
            mainHandler.post(() -> {
                pbLoading.setVisibility(View.GONE);

                if (list.isEmpty()) {
                    tvEmpty.setVisibility(View.VISIBLE);
                    gvRecentWallpapers.setVisibility(View.GONE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                    gvRecentWallpapers.setVisibility(View.VISIBLE);
                    adapter.updateData(list); // 触发预加载
                }
            });
        });
    }
    public void refreshWallpaperList() {
        loadRecentWallpapers();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
        if (mainHandler != null) {
            mainHandler.removeCallbacksAndMessages(null);
        }
    }
}