package com.wall.photography.picture.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.wall.photography.picture.Activity.Wallpaper
import com.wall.photography.picture.Connect.ItemClick
import com.wall.photography.picture.Data.Data
import com.wall.photography.picture.Project.List
import com.wall.photography.picture.databinding.FragmentMainChildBinding
import com.wall.photography.picture.tool.CommonUtils
import com.wall.photography.picture.tool.GridItemDecoration
import com.wall.photography.picture.tool.ObjectBoxManager

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [MainChildFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MainChildFragment : Fragment(), ItemClick {
    // TODO: Rename and change types of parameters
    private var param1Tag: String? = null
    private var param2: String? = null
    private lateinit var vb: FragmentMainChildBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1Tag = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = FragmentMainChildBinding.inflate(inflater, container, false)
        initData()

        return vb.root
    }


    private fun initData() {
        Log.d("-----------", "----------------initData")
        val queryWallpapersByTag = ObjectBoxManager.queryWallpapersByTag(param1Tag, 500)
        Log.d("-----------", "----------------queryWallpapersByTag")
        val itemHelper = GridItemDecoration(10, 10, 0)
        val gridLayoutManager = GridLayoutManager(requireContext(), 3)
        vb.recyclerview.addItemDecoration(itemHelper)
        vb.recyclerview.setLayoutManager(gridLayoutManager)
        vb.recyclerview.setHasFixedSize(true)
        vb.recyclerview.setItemViewCacheSize(20)

        val height = CommonUtils.dp2Px(180)
        val itemList = List(requireContext(), height, false)
        itemList.setDataInfo(queryWallpapersByTag)
        itemList.setInfoWallpaperListener(this@MainChildFragment)
        vb.recyclerview.setAdapter(itemList)

    }

    override fun onItemClickAction(info: Data?) {
        val intent = Intent(requireContext(), Wallpaper::class.java)
        intent.putExtra(CommonUtils.key_info, info)
        startActivity(intent)
    }

    companion object {

        @JvmStatic
        fun newInstance(tag: String) =
            MainChildFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, tag)
//                    putString(ARG_PARAM2, param2)
                }
            }
    }
}