package com.zxbw.modulemain.fragment

import android.graphics.Color
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import com.flyco.tablayout.listener.OnTabSelectListener
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.DetailBannerAdapter
import com.zxbw.modulemain.adapter.HomeBannerAdapter
import com.zxhl.cms.common.Constant
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.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.AppInEntity
import com.zxhl.cms.net.model.box.TabEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import kotlinx.android.synthetic.main.activity_layout_tb_goods_detail.*
import kotlinx.android.synthetic.main.fragment_layout_home.*
import me.jessyan.autosize.utils.ScreenUtils


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

    private var mBannerAdapter: HomeBannerAdapter? = null//Banner
    override fun layoutID(): Int {

        return R.layout.fragment_layout_home
    }

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

        }

    }

    override fun init(view: View?) {
        var statusBarHeight = ScreenUtils.getStatusBarHeight()
        if (statusBarHeight <= 0) {
            statusBarHeight = 40;
        }
        id_title_sys_bar_view.layoutParams.height = statusBarHeight
        var mList = listOf<String>("","","")
        if (activity!=null){
            mBannerAdapter = HomeBannerAdapter(mList,activity!!)
            id_home_top_banner.adapter = mBannerAdapter
        }
        indicator_view.setupWithViewPager(id_home_top_banner)

        getTabList()
        id_rl_search_view.setOnClickListener {
            showToast("搜索")
        }
    }

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


    private fun getTabList() {
        ApiClient.homeApi.getAppInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AppInEntity>() {
                override fun onSuccess(result: AppInEntity?) {
                    SettingPreference.saveConfig(result)
                    if (result != null && !result.tabs.isNullOrEmpty()) {
                        initViewPager(result.tabs!!)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    val config = SettingPreference.getConfig();
                    if (config != null && !config.tabs.isNullOrEmpty()) {
                        initViewPager(config.tabs!!)
                    }
                }
            })
    }

    private fun initViewPager(result: List<TabEntity>) {
        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
        id_fragment_home_tab?.notifyDataSetChanged()

    }


}