package com.hongyegroup.cpt_parttime.ui.main

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.utils.CommUtils
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.GloadingGlobalAdapter
import com.hongyegroup.cpt_parttime.BR
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.FragmentParttimeJobProfileBinding
import com.hongyegroup.cpt_parttime.databinding.IncludePensonalTurnUpRateBinding
import com.hongyegroup.cpt_parttime.mvvm.PartJobProfileViewModel
import com.hongyegroup.cpt_parttime.ui.PartJobEditProfileInfoActivity
import com.hongyegroup.cpt_parttime.widget.UserUrgentAlertPopup
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 兼职个人信息页面
 */
class PartTimeProfileFragment : YYBaseVDBLoadingFragment<PartJobProfileViewModel, FragmentParttimeJobProfileBinding>(), IFragmentRefresh,
    OnRefreshListener {

    val clickProxy by lazy { ClickProxy() }
    var mUserProfile: String? = null
    var mRootBinding: IncludePensonalTurnUpRateBinding? = null

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

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

    override fun startObserve() {
    }

    override fun init(savedInstanceState: Bundle?) {
        initData()
        initListener()
    }

    private fun initData() {
        showStateLoading()

        YYLogUtils.w("开始异步加载真正的跟视图")
        if (!mBinding.viewStubRoot.isInflate()) {
            mBinding.viewStubRoot.inflateAsync { view ->
                mRootBinding = DataBindingUtil.bind<IncludePensonalTurnUpRateBinding>(view)?.apply {
                    click = clickProxy
                }

                popupData2View()
            }
        }


        YYLogUtils.w("开始请求用户详情数据")
        CommUtils.getHandler().postDelayed({
            mUserProfile = "xxx"
            showStateSuccess()
            popupData2View()
        }, 1200)
    }

    private fun popupData2View() {
        if (mUserProfile != null && mRootBinding != null) {
            mRootBinding!!.turnUpProgress.setValue("60", 100F)
        }
    }

    override fun onGoadingRetry() {

    }

    private fun initListener() {

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {

    }

    override fun scrollTopRefresh() {

    }

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

        //跳转到编辑个人信息界面
        fun editProfileInfo() {
            PartJobEditProfileInfoActivity.startInstance()
        }

        //审核个人信息加急
        fun userUrgent() {
            //弹窗展示以及申请成功
            XPopup.Builder(mActivity)
                .hasShadowBg(true)
                .asCustom(UserUrgentAlertPopup(mActivity))
                .show()
        }

    }

}