package com.design.mischiefgo.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.design.mischiefgo.databinding.FragmentYellowCenterBinding
import com.design.mischiefgo.NewApp
import com.design.mischiefgo.ut.YellowCenterAdapter
import com.design.mischiefgo.entity.MainViewModel


class YellowCenterFragment : Fragment() {
    private lateinit var binding: FragmentYellowCenterBinding
    private lateinit var viewModel: MainViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentYellowCenterBinding.inflate(inflater, container, false)
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        initView()
        return binding.root
    }

    private fun initView() {
        val adapter = YellowCenterAdapter()
        binding.recyclerView.adapter=adapter
        val flexboxLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

        binding.recyclerView.layoutManager=flexboxLayoutManager

//        viewModel.randomItems.observe(viewLifecycleOwner) { randomItems ->
//            // 更新UI
//            adapter.setList(randomItems)
//        }
//        viewModel.loadRandomItems()
        viewModel.debugDatabase()
        NewApp.usuals?.let { adapter.setList(it) }
    }
}