package com.zxhl.main.page.fragment

import android.view.View
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.other.IsWeChatLoginEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.RxBus
import com.zxhl.cms.utils.WeChatLoginSuccessCallBall
import com.zxhl.cms.utils.WeChatUtils
import com.zxhl.cms.widget.IsLoginCallBack
import com.zxhl.main.R
import com.zxhl.main.page.view.BaseViewPagerFragment
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_warehouse.*

class WareHouseFragment : BaseViewPagerFragment(), WeChatLoginSuccessCallBall {
    private var mSwitchFragmentObservable: Observable<Int>? = null
    private var wechatutil: WeChatUtils? = null
    override val createTitles = ArrayList<String>().apply {
        add(AppContext.get().getString(R.string.to_be_recycled))
//        add("待收货")
//        add("已完成")
        add(AppContext.get().getString(R.string.recycled))
    }

    override val createFragments: Array<BaseFragment> = arrayOf(
        RecycledFragment.newInstance(),
//        ReceivingFragment.newInstance(),
//        FinishedFragment.newInstance(),
        RecoveryFragment.newInstance()
    )

    //private var mAdapter: HomeAdapter? = null
    override fun layoutID(): Int {

        return R.layout.fragment_warehouse
    }

    override fun init(view: View?) {
        //id_lottie_mining_machine?.imageAssetsFolder = "xingyunkuangji/images/"
//        id_lottie_mining_machine?.imageAssetsFolder = "guangying/images/"
//        id_lottie_mining_machine?.setAnimation("guangying/xiaoguo.json")
//        id_lottie_mining_machine?.repeatCount = -1
//        id_lottie_mining_machine?.playAnimation()
        //  mPresenter?.clearPage()
        //   mPresenter?.getHomeBoxList("")
        viewPager?.currentItem = 1
        wechatutil= WeChatUtils(mActivity,this)
        mSwitchFragmentObservable =
            RxBus.get().register(Constant.swith_fragment, Int::class.java)
        mSwitchFragmentObservable?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(Consumer<Int?> { s ->
                viewPager?.currentItem = s
            }, Consumer<Throwable?> { })
        id_tv_ware_house?.setOnClickListener {

            JumpUtils.WareHouseJump()
        }
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isVisible && isInitial) {
            wechatutil?.isWechatLogin(object : IsLoginCallBack<IsWeChatLoginEntity> {
                override fun onResult(result: IsWeChatLoginEntity?) {
                    if(result?.status.equals("0")){
                        JumpUtils.ThirdPardLoginJump()
                    }else{
                        createFragments[viewPager?.currentItem ?: 0].userVisibleHint = true
                    }
                }

                override fun onError(code: String?, errorMsg: String?) {

                }

            })


        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mSwitchFragmentObservable != null) {
            RxBus.get().unregister(Constant.swith_fragment, mSwitchFragmentObservable!!)
        }

    }

    companion object {

        fun newInstance() = WareHouseFragment()
    }

    override fun loginSuccess(lottery: String?) {

    }
}