package com.yidianling.home.ui.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.ydl.ydlcommon.view.banner.GlideImageLoader
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeHeaderBean
import kotlinx.android.synthetic.xlzx.home_module_pager_banner_view.view.*


/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述:顶部ViewPager与分类模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
class HomePagerBannerView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) :
    RelativeLayout(mContext) {

    private val mImageViews = ArrayList<String>()
    private var homePagerBannerCategoryView: HomePagerBannerCategoryView? = null

    init {
        initView()
    }

    private fun initView() {
        var screenWidth = RxDeviceTool.getScreenWidth(mContext)
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        View.inflate(mContext, R.layout.home_module_pager_banner_view, this)

        initBannerLayout()
//        val addLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//        addLayoutParams.setMargins(0, (ScreenUtil.screenWidth * (356f / 750f)).toInt(),0,0)
//        homeModulePagerBannerViewAddLayout.layoutParams = addLayoutParams
//        if (null == homePagerBannerCategoryView) {
//            homePagerBannerCategoryView = HomePagerBannerCategoryView(mContext, homeEvent!!)
//            homeModulePagerBannerViewAddLayout.addView(homePagerBannerCategoryView)
//        }
    }

    private fun initBannerLayout() {
//        var screenWidth = RxDeviceTool.getScreenWidth(mContext)
//        var viewHeight = screenWidth * 508 / 750
//        val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, viewHeight)
//        homeModulePagerBannerViewBanner.layoutParams = params
        homeModulePagerBannerViewBanner.setIndicatorBottomPadding(50)
    }

    /**
     * 填充数据
     */
    fun initData(bean: HomeHeaderBean?) {
        initCategory(bean?.askCategoryData, bean?.homeSaleData)
        initBanner(bean?.focusList)
    }

    /**
     * 更新分类细目
     */
    private fun initCategory(
        categoryBean: List<HomeHeaderBean.AskCategoryDataBean>?,
        homeSaleData: List<HomeHeaderBean.HomeSaleDataBean>?
    ) {
        homePagerBannerCategoryView!!.initData(categoryBean, homeSaleData)
    }

    /**
     * 更新banner
     */
    private fun initBanner(focusListBean: List<HomeHeaderBean.FocusListBean>?) {
        //临时缓存集合
        val cacheList = ArrayList<HomeHeaderBean.FocusListBean>()
        if (null != focusListBean && focusListBean.isNotEmpty()) {
            mImageViews.clear()
            //产品说取前8个
            if (focusListBean.size > 8) {
                cacheList.addAll(focusListBean.subList(0, 8))
            } else {
                cacheList.addAll(focusListBean)
            }
            for (item in cacheList) {
                mImageViews.add(item.imageUrl!!)
            }
        }
        homeModulePagerBannerViewBanner.setImageLoader(GlideImageLoader(R.drawable.home_module_image_default_back))
            .setImages(mImageViews).start()

        homeModulePagerBannerViewBanner.setOnBannerListener {
            homeEvent?.bannerClick(cacheList!![it])
        }
    }

    fun startBanner() {
        if (homeModulePagerBannerViewBanner != null) {
            homeModulePagerBannerViewBanner.startAutoPlay()
        }
    }

    fun stopBanner() {
        if (homeModulePagerBannerViewBanner != null) {
            homeModulePagerBannerViewBanner.stopAutoPlay()
        }
    }
}