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 androidx.viewpager.widget.ViewPager
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.zxbw.modulemain.contract.HomeContract
import com.zxbw.modulemain.presenter.HomePresenter
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.net.model.qy.HomeBannerEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
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(), HomeContract.View {
    private var mPresenter: HomePresenter? = null
    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

        mPresenter = HomePresenter(this)

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

        val config = SettingPreference.getConfig();
        if (config != null && !config.tabs.isNullOrEmpty()) {
            setTabList(config.tabs!!)
        } else {
            mPresenter?.getTabList()
        }

        mPresenter?.getBannerList()

    }

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

    override fun setBannerList(result: List<HomeBannerEntity>?) {
        if (result.isNullOrEmpty()) {
            id_home_top_banner_view.visibility = View.GONE
            return
        }
        if (activity != null) {
            mBannerAdapter = HomeBannerAdapter(result, activity!!)
            id_home_top_banner.adapter = mBannerAdapter
            id_home_top_banner.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    if (result[position].bannerType.equals("blindBox")) {
                        id_fragment_home_tab.textSelectColor = Color.parseColor("#333333")
                        id_fragment_home_tab.textUnselectColor = Color.parseColor("#FC333333")
                        id_fragment_home_tab.indicatorColor = Color.parseColor("#333333")
                        toolbar_layout.setContentScrimColor(Color.parseColor("#8B3AFE"))

                    } else if (result[position].bannerType.equals("H5")) {
                        id_fragment_home_tab.textSelectColor = Color.parseColor("#333333")
                        id_fragment_home_tab.textUnselectColor = Color.parseColor("#FC333333")
                        id_fragment_home_tab.indicatorColor = Color.parseColor("#333333")
                        toolbar_layout.setContentScrimColor(Color.parseColor("#628E43"))
                    } else {
                        id_fragment_home_tab.textSelectColor = Color.parseColor("#FFFFFF")
                        id_fragment_home_tab.textUnselectColor = Color.parseColor("#FCFFFFFF")
                        id_fragment_home_tab.indicatorColor = Color.parseColor("#FFFFFF")
                        toolbar_layout.setContentScrimColor(Color.parseColor("#FFED2E28"))
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
            if (result[0].bannerType.equals("blindBox")) {
                id_fragment_home_tab.textSelectColor = Color.parseColor("#333333")
                id_fragment_home_tab.textUnselectColor = Color.parseColor("#66597B")
                id_fragment_home_tab.indicatorColor = Color.parseColor("#333333")
                toolbar_layout.setContentScrimColor(Color.parseColor("#8B3AFE"))
            } else if (result[0].bannerType.equals("H5")) {
                id_fragment_home_tab.textSelectColor = Color.parseColor("#333333")
                id_fragment_home_tab.textUnselectColor = Color.parseColor("#5C6C52")
                id_fragment_home_tab.indicatorColor = Color.parseColor("#333333")
                toolbar_layout.setContentScrimColor(Color.parseColor("#628E43"))
            } else {
                id_fragment_home_tab.textSelectColor = Color.parseColor("#FFFFFF")
                id_fragment_home_tab.textUnselectColor = Color.parseColor("#FFDBDB")
                id_fragment_home_tab.indicatorColor = Color.parseColor("#FFFFFF")
                toolbar_layout.setContentScrimColor(Color.parseColor("#FFED2E28"))
            }
        }
        indicator_view.setupWithViewPager(id_home_top_banner)
    }

    override fun setTabList(result: List<TabEntity>?) {
        if (result.isNullOrEmpty()) {
            showToast("加载失败，请重试")
            return
        }
        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?.setTextSelectSize(Utils.dip2px(20).toFloat())
        id_fragment_home_tab?.setTextUnselectSize(Utils.dip2px(18).toFloat())
        id_fragment_home_tab?.setViewPager(viewPager)
        id_fragment_home_tab?.overScrollMode = View.OVER_SCROLL_NEVER

    }


}