package com.hongyegroup.cpt_parttime.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBLoadingFragment
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.checkEmpty
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.utils.log.YYLogUtils
import com.guadou.lib_baselib.view.gloading.GLoadingTitleStatus
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingPlaceHolderlAdapter
import com.hongyegroup.cpt_parttime.BR
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.FragmentParttimeJobDetailBinding
import com.hongyegroup.cpt_parttime.databinding.IncludePartTimeJobDetailFragmentBinding
import com.hongyegroup.cpt_parttime.mvvm.PartJobDetailChildViewModel
import com.hongyegroup.cpt_parttime.ui.activity.PartTimeJobDetailActivity

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

    private var mRootBinding: IncludePartTimeJobDetailFragmentBinding? = null
    private val mClickProxy by lazy { ClickProxy() }

    //重新生成GLoading对象-跳动动画
    override fun generateGLoading(view: View): Gloading.Holder {
        return Gloading.from(GloadingPlaceHolderlAdapter(R.layout.layout_placeholder_part_time_detail_child))
            .wrap(view, GLoadingTitleStatus(false, true, false))
            .withRetry { onGoadingRetry() }
    }

    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()
        initListener()
    }

    override fun onResume() {
        super.onResume()
        initDataAndRootView()
    }

    private fun initDataAndRootView() {

        if (!mBinding.viewStubRoot.isInflate()) {
            val start1 = System.currentTimeMillis()
            mBinding.viewStubRoot.inflateAsync { view ->
                val end1 = System.currentTimeMillis()
                YYLogUtils.w("添加2布局耗时：" + (end1 - start1))
                mRootBinding = DataBindingUtil.bind<IncludePartTimeJobDetailFragmentBinding>(view)?.apply {
                    viewModel = mViewModel
                    click = mClickProxy
                }
                popupDetailData()
                checkView2Showed()
            }
        }

        requestRecommendJob()

    }

    //这里请求网络数据完成
    private fun requestRecommendJob() {
        if (mViewModel.mDatas.checkEmpty()) {
            mViewModel.getRecommendJobs().observe(this) {
                checkView2Showed()
            }
        }
    }

    //查询异步加载的布局和异步的远端数据是否已经准备就绪
    private fun checkView2Showed() {
        if (mViewModel.isLoadSuccessed && mRootBinding != null) {
            showStateSuccess()
        }
    }

    //父Activity传递过来的详情数据展示出来
    private fun popupDetailData() {
        //部分使用DataBinding 部分手动赋值
    }

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

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

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

    override fun startObserve() {
    }

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

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

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

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

    }
}