package com.zxbw.modulemain.fragment

import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.HomeBannerAdapter
import com.zxbw.modulemain.contract.HomeContract
import com.zxbw.modulemain.presenter.HomePresenter
import com.zxbw.modulemain.view.DialogUtils
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.FragmentAdapter
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.down.UpdateVersionManager
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.TabEntity
import com.zxhl.cms.net.model.qy.CouponsEntity
import com.zxhl.cms.net.model.qy.HomeBannerEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.UpdateCallback
import com.zxhl.cms.utils.Utils
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, UpdateCallback {
    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?) {
        val updateVersionManager = this?.let { it1 -> UpdateVersionManager(mActivity, this) }
        updateVersionManager?.checkUpdate(true)
        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 {
            JumpUtils.SearchJump()
        }
        id_img_kefu.setOnClickListener {
//            JumpUtils.webJump("客服", NetConfig.H5.WEB_URL_CUSTOMER_SERVICE)
//            JumpUtils.UDesk()
            JumpUtils.UserQuestion()
        }

        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) {
                    setHomeTabColor(result[position].bannerType)
                }

                override fun onPageScrollStateChanged(state: Int) {
                }
            })
            setHomeTabColor(result[0].bannerType)
        }
        indicator_view.setupWithViewPager(id_home_top_banner)
    }

    private fun setHomeTabColor(bannerType: String) {
        if (bannerType.equals("blindBox")) {
            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("#8B3AFE"))
            id_tv_search_btn.setBackgroundResource(R.drawable.shape_8d41fd_r15)
        } else if (bannerType.equals("H5")) {
            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("#628E43"))
            id_tv_search_btn.setBackgroundResource(R.drawable.shape_689349_r15)
        } 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"))
            id_tv_search_btn.setBackgroundResource(R.drawable.shape_eb3625_r15)
        }
    }

    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(result[i]), 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

    }

    override fun setCouponsList(result: List<CouponsEntity>?) {
        if(!result.isNullOrEmpty()){
            if(TextUtils.equals(SettingPreference.getifShowCoups(),"0")){
                DialogUtils.showifshowCoup(mActivity, result, View.OnClickListener {
                    JumpUtils.RechargeJump()
                })
            }

        }

    }

    override fun CheckUpate(boolean: Boolean) {
        Log.e("MXL", "更新：" + boolean)
        if (Constant.Switch.isOpenVip) {
            if (!boolean) {
                mPresenter?.getFetchCard()
            }
        }

    }


}