package com.quiescent.wallpaper.collection.test.ui

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.quiescent.wallpaper.collection.test.WallpaperCollection
import com.quiescent.wallpaper.collection.test.bean.InfoList
import com.quiescent.wallpaper.collection.test.databinding.FragmentHomeBinding
import com.quiescent.wallpaper.collection.test.tool.HomeAdapter


class HomeListFragment : Fragment() {
    companion object {

        const val KEY_CATEGORY_NAME = "category_name"

        @JvmStatic
        fun newInstance(category: String) =
            HomeListFragment().apply {
                arguments = Bundle().apply {
                    putString(KEY_CATEGORY_NAME, category)
                }
            }
    }

    private lateinit var homeBinding: FragmentHomeBinding
    private var className:String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        arguments?.let {
            className = it.getString(KEY_CATEGORY_NAME)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeBinding = FragmentHomeBinding.inflate(layoutInflater)
        val view = homeBinding.root

        homeBinding.list.run {
            layoutManager = GridLayoutManager(requireContext(),2)
            val filter = WallpaperCollection.instance.list.filter {
                it.name == className
            }
            adapter = HomeAdapter(filter[0].data.shuffled()){
                goToSet(it)
            }
        }
        return view
    }
    private fun goToSet(data: InfoList){
        val intent = Intent(requireContext(), ActivitySetWallpaper::class.java)
        intent.putExtra(ActivitySetWallpaper.KEY_DATA,data)
        startActivity(intent)
    }
}