package com.jld.vod.view.fragment

import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jld.kotilntest.room.adapter.BrowserAdapter
import com.jld.vod.R
import com.jld.vod.base.BaseFragment
import com.jld.vod.model.bean.BaseBean
import com.jld.vod.model.bean.BrowserBean
import com.jld.vod.model.bean.ModelAllBean
import com.jld.vod.utils.AppUtil
import com.jld.vod.utils.LogUtils
import com.jld.vod.utils.SharedPrefUtils
import com.jld.vod.view.WebActivity
import com.jld.vod.viewmodel.BrowserViewModel
import com.jld.vod.viewmodel.MainViewModel
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.enums.CoreAnim
import com.xuexiang.xui.widget.actionbar.TitleBar
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest

@Page(anim = CoreAnim.none)
class BrowserFragment : BaseFragment() {
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var browserViewModel: BrowserViewModel
    override fun getLayoutId(): Int {
        return R.layout.fragment_browser
    }

    override fun initViews() {
        mRecyclerView = findViewById(R.id.rv_browser)

        //初始化viewmodel
        browserViewModel = ViewModelProviders.of(this)
            .get(BrowserViewModel::class.java)

        //初始化recyclerview
        val gridlayoutManager = GridLayoutManager(activity, 4)
        mRecyclerView!!.layoutManager = gridlayoutManager

        val adapter = BrowserAdapter()
        mRecyclerView!!.adapter = adapter


        val lid = SharedPrefUtils.getString(activity,"language_lid")
        val mid = SharedPrefUtils.getString(activity,"RES_NETWORK")

        if (lid != "" && mid !="")
        {
            lifecycleScope.launchWhenCreated {
                browserViewModel.getBrowserAllList(mid.toLong(),lid.toLong()).collectLatest { pagingData ->
                    adapter.submitData(pagingData)
                }
            }
        }
        adapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.Error -> {
                    finishLoading()
                }
                is LoadState.Loading -> {
                    showLoading()
                }
                is LoadState.NotLoading -> {
                    finishLoading()
                }
            }
        }
//        val nameObserver = Observer<BaseBean<List<BrowserBean>>>{
//            if (it == null)
//            {
//                showNullBrowswer()
//            }
//        }
//        browserViewModel.findNetWorkAllliveData.observe(this,nameObserver)

        adapter.setOnBrowserClickListener(object : BrowserAdapter.IBrowserItemClickListener{
            override fun onItemClickListener(url: String) {
                LogUtils.logD("url"+url)
                if (!url.isEmpty())
                {
                    val intent = Intent(context,WebActivity::class.java)
                    intent.putExtra("url",url)
                    startActivity(intent)
                }
            }
        })
    }

    private fun showNullBrowswer() {

    }

    override fun initTitle(): TitleBar? {
        return null
    }
}