package com.zxbw.modulemain.fragment

import android.util.Log
import android.view.View
import com.zxbw.modulemain.R
import com.zxhl.cms.common.FragmentAdapter
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.TabEntity
import kotlinx.android.synthetic.main.fragment_layout_home.*


/**
 * Created by gaoleichao on 2021/10/25
 */
class HomeFragment : BaseFragment() {

    override fun layoutID(): Int {

        return R.layout.fragment_layout_home
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {

        }

    }

    override fun init(view: View?) {
        getTabList()
    }

    companion object {
        fun newInstance() = HomeFragment()
    }


    private fun getTabList() {
        ApiClient.homeApi.getBoxTab().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<TabEntity>>() {
                override fun onSuccess(result: List<TabEntity>?) {
                    if (result != null) {
                        initViewPager(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("wxw", "ERROR" + code + errorMsg)
                    val createTitles = ArrayList<TabEntity>().apply {
                        add(TabEntity())
                        add(TabEntity())
                        add(TabEntity())
                        add(TabEntity())
                        add(TabEntity())
                    }
                    initViewPager(createTitles)
                }
            })
    }

   private fun initViewPager(result: List<TabEntity>) {
       Log.d("wxw", "result" +result.size)
        val adapter = FragmentAdapter(childFragmentManager)
       for (i in 0 until (result?.size ?: 0)) {
           val title = result[i].name ?: ""
           adapter.addFrag(RecommendFragment.newInstance(title), title)
       }
        viewPager?.adapter = adapter
        id_fragment_home_tab?.setViewPager(viewPager)
       id_fragment_home_tab?.overScrollMode = View.OVER_SCROLL_NEVER
    }


}