package com.jld.vod.view.fragment

import android.content.Intent
import android.os.BaseBundle
import android.os.Bundle
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 androidx.room.Room
import com.jld.kotilntest.room.adapter.AboutAdapter
import com.jld.vod.R
import com.jld.vod.base.BaseFragment
import com.jld.vod.db.AboutDatabase
import com.jld.vod.model.bean.BaseBean
import com.jld.vod.model.bean.FindAboutAllBean
import com.jld.vod.model.bean.ModelAllBean
import com.jld.vod.utils.LogUtils
import com.jld.vod.utils.SharedPrefUtils
import com.jld.vod.view.AboutActivity
import com.jld.vod.viewmodel.AboutViewModel
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.collectLatest

@Page(anim = CoreAnim.none)
class AboutFragment : BaseFragment() {
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var aboutViewModel: AboutViewModel


    override fun getLayoutId(): Int {
        return R.layout.fragment_about
    }

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

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

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

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


        val lid = SharedPrefUtils.getString(activity,"language_lid")
        val mid = SharedPrefUtils.getString(activity,"RES_ABOUT")
        if (lid != "" && mid !="")
        {
            lifecycleScope.launchWhenCreated {
                aboutViewModel.getAboutAllList(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<FindAboutAllBean>>>{
//            if (it == null)
//            {
//               // showNullBrowswer()
//            }
//        }
//        aboutViewModel.findAboutAllliveData.observe(this,nameObserver)



    //    aboutViewModel.aboutusList.observe(this, Observer(adapter::submitList))

        adapter.setOnBrowserClickListener(object : AboutAdapter.IBrowserItemClickListener{
            override fun onItemClickListener(info: String,pictures : String) {
//                var args = Bundle()
//                args.putString("info",info)
//                args.putString("pictures",pictures)
//                openPage("AboutDetailsFragment",args)
                val intent = Intent(activity, AboutActivity::class.java)
                intent.putExtra("info",info)
                intent.putExtra("pictures",pictures)
                startActivity(intent)
            }
        })
    }
    override fun initTitle(): TitleBar? {
        return null
    }

    private fun showNullBrowswer() {
//        val db = Room.databaseBuilder(context!!,
//            AboutDatabase::class.java, "aboutus").build()
//
//         val mItemTexts = arrayOf(
//             "电影", "音乐",
//             "游戏", "浏览器"
//        )
//         val mItemImgs = intArrayOf(
//            R.mipmap.test_about_us, R.mipmap.test_about_us,
//            R.mipmap.test_about_us, R.mipmap.test_about_us)
//
//        for ((index,e) in mItemImgs.withIndex()){
//            Thread(Runnable{ run {
//                db.aboutDao().insertAll(AboutBean(index, mItemImgs[index], mItemTexts[index]))
//            }}).start()
//        }
    }
}