package com.hongyegroup.cpt_parttime.ui

import android.os.Bundle
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBFragment
import com.guadou.cs_cptservices.interfaces.IFragmentRefresh
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.vertical
import com.hongyegroup.cpt_parttime.BR
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.FragmentParttimeJobDetailBinding
import com.hongyegroup.cpt_parttime.mvvm.PartJobDetailChildViewModel

/**
 * 工作详情 子页面 展示工作详细信息
 */
class PartJobDetailFragment : YYBaseVDBFragment<PartJobDetailChildViewModel, FragmentParttimeJobDetailBinding>(), IFragmentRefresh {

    private val mClickProxy by lazy { ClickProxy() }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_parttime_job_detail, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, mClickProxy)
    }

    override fun init(savedInstanceState: Bundle?) {

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        mViewModel.getRecommendJobs()
    }

    private fun initRV() {
        mBinding.rvRecommend.vertical().apply {
            adapter = mViewModel.mAdapter
        }
    }

    private fun initListener() {
        //Adapter的滑动监听，监听加载更多
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false

        //Item点击
        mViewModel.mAdapter.addChildClickViewIds(R.id.ll_job_root)
        mViewModel.mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mViewModel.mDatas[position]
            mClickProxy.gotoJobDetailPage()
        }
    }

    override fun startObserve() {
    }

    override fun scrollTopRefresh() {
        if (mViewModel.mDatas.size > 1) {
            mBinding.rvRecommend.scrollToPosition(0)
        }
        initData()
    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        //执行导航
        fun doDirection() {
            toast("执行导航")
        }

        //跳转详情
        fun gotoJobDetailPage() {
            PartTimeJobDetailActivity.startInstance()
        }

    }
}