package com.wall.photography.wallpaper.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.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.activivty.SetWallpaperActivity
import com.wall.photography.wallpaper.adapter.AdapterHomePaing
import com.wall.photography.wallpaper.adapter.AdapterLoves
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.databinding.FragmentLoveBinding
import com.wall.photography.wallpaper.manager.MyItemDecoration
import com.wall.photography.wallpaper.topon.AdManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class LoveFragment : Fragment() {

    companion object {
        fun newInstance() = LoveFragment()
    }

    private lateinit var viewModel: LoveViewModel
    private lateinit var vb: FragmentLoveBinding
    private lateinit var adapterLove: AdapterLoves

    private var adapterLovePaging: AdapterHomePaing? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = FragmentLoveBinding.inflate(layoutInflater)
        return vb.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(LoveViewModel::class.java)
        viewModel.setData()
        initWallpaper()

    }

    private fun initWallpaper() {

        adapterLovePaging = AdapterHomePaing(true, requireContext()) {
            AdManager.showTopOn(requireActivity()) {
                startActivity(Intent(requireActivity(), SetWallpaperActivity::class.java).apply {
                    putExtra(SetWallpaperActivity.KEY_DATA, it)
                })
            }
        }.apply {
            addLoadStateListener {
                if (it.source.refresh is LoadState.NotLoading && adapterLovePaging?.itemCount == 0) {
                    Log.d(MyApp.TAG, "------------love-空")
                    vb.tvNoLove.isVisible = true
                    vb.loveRecycler.isVisible = false
                } else {
                    Log.d(MyApp.TAG, "------------love-不空")
                    vb.tvNoLove.isVisible = false
                    vb.loveRecycler.isVisible = true
                }
            }
        }

        CoroutineScope(Dispatchers.Main).launch {
            viewModel.getPagingData().collectLatest {
                Log.d(MyApp.TAG, "------------love-update 分页")
                adapterLovePaging?.submitData(it)
            }
        }


        vb.loveRecycler.run {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = adapterLovePaging
            addItemDecoration(MyItemDecoration(requireContext(), 8, 8, 0))
        }

    }

    fun refreshLoves() {
        adapterLovePaging?.refresh()
    }




}