package com.wall.engine.wallpaper;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FavoriteManager {
    private static final String TAG = "FavoriteManager";
    private static final String PREF_NAME = "favorite_wallpapers";
    private static final String KEY_FAVORITE_IDS = "favorite_ids";
    private static final String KEY_FAVORITE_ITEMS = "favorite_items";

    private SharedPreferences sharedPreferences;
    private Gson gson;

    public FavoriteManager(Context context) {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        gson = new Gson();
    }

    // 添加收藏
    public void addFavorite(WallpaperItem item) {
        try {
            String itemId = item.getId();
            Log.d(TAG, "Add to Favorites: " + itemId);


            Set<String> favoriteIds = getFavoriteIds();
            favoriteIds.add(itemId);

            List<WallpaperItem> favorites = getFavoriteItems();


            for (int i = favorites.size() - 1; i >= 0; i--) {
                if (favorites.get(i).getId().equals(itemId)) {
                    favorites.remove(i);
                }
            }

            favorites.add(item);

            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putStringSet(KEY_FAVORITE_IDS, new HashSet<>(favoriteIds)); // 创建新的HashSet避免并发问题
            editor.putString(KEY_FAVORITE_ITEMS, gson.toJson(favorites));
            editor.apply();

            Log.d(TAG, "Added to Favorites: " + favorites.size());

        } catch (Exception e) {
            Log.e(TAG, "Failed to add to favorites", e);
        }
    }

    // 移除收藏
    public void removeFavorite(String itemId) {
        try {
            Log.d(TAG, "Unfavorite: " + itemId);


            Set<String> favoriteIds = getFavoriteIds();
            favoriteIds.remove(itemId);


            List<WallpaperItem> favorites = getFavoriteItems();
            for (int i = favorites.size() - 1; i >= 0; i--) {
                if (favorites.get(i).getId().equals(itemId)) {
                    favorites.remove(i);
                    break;
                }
            }

            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putStringSet(KEY_FAVORITE_IDS, new HashSet<>(favoriteIds));
            editor.putString(KEY_FAVORITE_ITEMS, gson.toJson(favorites));
            editor.apply();

            Log.d(TAG, "Unfavorited successfully: " + favorites.size());

        } catch (Exception e) {
            Log.e(TAG, "Failed to unfavorite", e);
        }
    }

    // 检查是否已收藏
    public boolean isFavorite(String itemId) {
        Set<String> favoriteIds = getFavoriteIds();
        boolean isFav = favoriteIds.contains(itemId);
        Log.d(TAG, "检查收藏状态: " + itemId + " -> " + isFav);
        return isFav;
    }

    // 获取所有收藏的ID
    public Set<String> getFavoriteIds() {
        Set<String> ids = sharedPreferences.getStringSet(KEY_FAVORITE_IDS, new HashSet<>());
        Log.d(TAG, "获取收藏ID数量: " + ids.size());
        return new HashSet<>(ids); // 返回新的HashSet避免并发问题
    }

    // 获取所有收藏项
    public List<WallpaperItem> getFavoriteItems() {
        try {
            String json = sharedPreferences.getString(KEY_FAVORITE_ITEMS, "[]");
            Type listType = new TypeToken<List<WallpaperItem>>(){}.getType();
            List<WallpaperItem> items = gson.fromJson(json, listType);
            if (items == null) {
                items = new ArrayList<>();
            }
            Log.d(TAG, "获取收藏项数量: " + items.size());
            return items;
        } catch (Exception e) {
            Log.e(TAG, "获取收藏项失败", e);
            return new ArrayList<>();
        }
    }
}