package com.jld.vod.view.fragment

import android.content.Intent
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jld.kotilntest.room.adapter.CategoryFatherAllAdapter
import com.jld.kotilntest.room.adapter.MusicListAdapter
import com.jld.vod.R
import com.jld.vod.base.BaseFragment
import com.jld.vod.utils.LogUtils
import com.jld.vod.utils.SharedPrefUtils
import com.jld.vod.view.MusicPlayActivity
import com.jld.vod.viewmodel.MusicFragmentViewModel
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.enums.CoreAnim
import com.xuexiang.xui.widget.actionbar.TitleBar
import com.xuexiang.xui.widget.statelayout.StatefulLayout
import kotlinx.coroutines.flow.collectLatest

@Page(anim = CoreAnim.none)
class MusicFragment : BaseFragment() {
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mRecyclerView1: RecyclerView
    private lateinit var musicFragmentViewModel: MusicFragmentViewModel
    private lateinit var sfl_device_loading: StatefulLayout
    private var isAll:Boolean = true

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

    override fun initViews() {
        mRecyclerView = findViewById(R.id.rv_musict1)
        mRecyclerView1 = findViewById(R.id.rv1_music_list)
        sfl_device_loading = findViewById(R.id.sfl_device_loading)
        //初始化viewmodel
        musicFragmentViewModel = ViewModelProviders.of(this)
            .get(MusicFragmentViewModel::class.java)


        //初始化一级分类列表recyclerview
        val LinearlayoutManager = LinearLayoutManager(activity)
        mRecyclerView1!!.layoutManager = LinearlayoutManager
        //添加一级Android自带的分割线
        //   mRecyclerView1.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
        //音乐分类一级列表
        val adapter1 = CategoryFatherAllAdapter()
        mRecyclerView1!!.adapter = adapter1

        //初始化音乐列表recyclerview
        val gridlayoutManager = GridLayoutManager(activity, 3)
        mRecyclerView!!.layoutManager = gridlayoutManager

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

        val lid = SharedPrefUtils.getString(activity,"language_lid")
        val mid = SharedPrefUtils.getString(activity,"RES_MUSIC")
        if (lid != "" && mid !="") {
            //获取一级列表
            lifecycleScope.launchWhenCreated {
                musicFragmentViewModel.getCategoryFatherAllByMid(mid.toLong(), lid.toLong())
                    .collectLatest { pagingData ->
                        adapter1.submitData(pagingData)
                    }
            }
            lifecycleScope.launchWhenCreated {
                musicFragmentViewModel.getMusicBymid(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()
                }
            }
        }
        //列表一级监听
        adapter1.setOnCategorySonClickListener(object : CategoryFatherAllAdapter.ICategorySonItemClickListener{
            override fun onItemClickListener(cid: Long,mid: Long) {
                //获取音乐二级列表
                lifecycleScope.launchWhenCreated {
                    musicFragmentViewModel.getMusicByCid(cid,mid,lid.toLong()).collectLatest { pagingData ->
                        adapter.submitData(pagingData)
                    }
                }
                isAll = false
            }
        })


        adapter.setOnMusicClickListener(object : MusicListAdapter.IMusicItemClickListener{

            override fun onItemClickListener(id:Int?,cid: Long?, mid: Long?) {
                //LogUtils.logD("cid$cid mid$mid lid$lid")
                val intent = Intent(context,MusicPlayActivity::class.java)
                intent.putExtra("id",id)
                intent.putExtra("cid",cid)
                intent.putExtra("mid",mid)
                intent.putExtra("lid",lid)
                startActivity(intent)
            }
        })
    }
    /**
     * 找到设备view控件
     */
    private fun showNullMusic() {
        sfl_device_loading!!.showEmpty()
    }

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

    override fun onDestroyView() {
        super.onDestroyView()
    }
}