package com.wall.turing.wallpapers.turingfragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wall.turing.wallpapers.accctivities.TuringWallpaperActivity;
import com.wall.turing.wallpapers.databinding.RecommendPageBinding;
import com.wall.turing.wallpapers.adapteres.RecommendAdapter;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;
import com.wall.turing.wallpapers.uutils.AppUtils;
import com.wall.turing.wallpapers.uutils.DatabaseManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecommendPage extends Fragment {

    private RecommendPageBinding vb;
    private RecommendAdapter adapter;
    private List<Wallpaper> allDataList = new ArrayList<>();

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

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = RecommendPageBinding.inflate(inflater, container, false);
        return vb.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        vb.recommendRecycler.setLayoutManager(new LinearLayoutManager(requireContext()));
        adapter = new RecommendAdapter(requireContext(), allDataList, new RecommendAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Wallpaper data) {
                Intent intent = new Intent(requireContext(), TuringWallpaperActivity.class);
                intent.putExtra(AppUtils.key_info,data);
                startActivity(intent);
            }

            @Override
            public void onLikeClick(Wallpaper data) {
                // 收藏点击事件
                data.setLike(!data.isLike());
                DatabaseManager.getObjectBoxLike().put(data);
                adapter.notifyDataSetChanged();
            }
        });
        vb.recommendRecycler.setAdapter(adapter);
        
        // 加载数据
        loadData();
    }

    private void loadData() {
        vb.loadingPb.setVisibility(View.VISIBLE);
        
        // 从数据库获取所有数据
        List<Wallpaper> allData = DatabaseManager.getObjectBoxLike().getAll();
        
        if (allData != null && !allData.isEmpty()) {
            allDataList.clear();
            allDataList.addAll(allData);
            
            // 随机排序
            Collections.shuffle(allDataList);
            
            adapter.notifyDataSetChanged();
        }
        
        vb.loadingPb.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新收藏状态
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        vb = null;
    }
}

