package com.swago.user.task

import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.mine.TaskModel
import com.swago.user.databinding.FragmentDailyBinding
import com.swago.user.databinding.FragmentNewerBinding
import com.swago.user.vm.TaskVm

/**
 *@date 2021/11/25 21:21
 *description:
 */
class NewerFragment: BaseListFragment<FragmentDailyBinding, TaskModel>() {

    private val taskVm by viewModels<TaskVm>()

    override val smartRecyclerView: SwagoRecyclerView<TaskModel> by lazy {
        binding.swagoRv as SwagoRecyclerView<TaskModel>
    }
    override val adapter: BaseQuickAdapter<TaskModel, *> by lazy {
        TaskAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
        taskVm.getTaskList(2)
    }

    override fun initViewData() {
        adapter.setOnItemClickListener { _, _, position ->
            if (adapter.data.size>position && adapter.data[position].task_status == 1){
                taskVm.receiveReward(adapter.data[position].id,position)
            }
        }
    }


    override fun initLiveData() {
        taskVm.taskListLiveData.observe(this){
            if (it == null){
                smartRecyclerView.onFetchDataError()
            }else{
                smartRecyclerView.onFetchDataFinish(it,true)
            }
        }

        taskVm.receiveLiveData.observe(this){
            adapter.data[it].task_status = 2
            adapter.notifyItemChanged(it)
        }
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }
}