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.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.paging.PagingData
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.activivty.SetWallpaperActivity
import com.wall.photography.wallpaper.adapter.AdapterCategory
import com.wall.photography.wallpaper.adapter.AdapterHomePaing
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.databinding.FragmentHomeBinding
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 HomeFragment : Fragment() {

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

    private lateinit var viewModel: HomeViewModel


    private lateinit var adapterHomepaging: AdapterHomePaing

    private lateinit var vb: FragmentHomeBinding

    private   var list :MutableList<PagingData<Bean>>  = mutableListOf()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = FragmentHomeBinding.inflate(layoutInflater)
        return vb.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        adapterHomepaging = AdapterHomePaing(mContext = requireContext()) {

            AdManager.showTopOn(requireActivity()){
                startActivity(Intent(requireActivity(), SetWallpaperActivity::class.java).apply {
                    putExtra(SetWallpaperActivity.KEY_DATA, it)
                })
            }

        }
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]

        CoroutineScope(Dispatchers.Main).launch {
            viewModel.getPagingData(MyApp.strings[0]).collectLatest {
                Log.d(MyApp.TAG, "------------collectLatest ")
                adapterHomepaging.submitData(it)
                list.add(it)
            }
        }

        initWallpaper()

    }


    private fun initWallpaper() {
        vb.wallpaperRecycler.run {
            layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = adapterHomepaging
            addItemDecoration(MyItemDecoration(requireContext(), 8, 8, 0))
            addOnScrollListener(object : OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        Glide.with(context).resumeRequests()
                    } else {
                        Glide.with(context).pauseRequests()
                    }
                }
            })
        }

    }

}