package com.casic.br.fragment.shop

import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.casic.br.R
import com.casic.br.adapter.FavoriteAdapter
import com.casic.br.adapter.FeaturedGoodsAdapter
import com.casic.br.extensions.combineImagePath
import com.casic.br.model.BannerImageModel
import com.casic.br.model.FavoriteModel
import com.casic.br.model.FeaturedGoodsModel
import com.casic.br.utils.GridItemDecoration
import com.casic.br.utils.LocaleConstant
import com.casic.br.view.WebsiteActivity
import com.casic.br.vm.BannerViewModel
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.*
import kotlinx.android.synthetic.main.fragment_favorite.*
import java.util.*

class FavoriteFragment : KotlinBaseFragment() {

    private lateinit var bannerViewModel: BannerViewModel

    override fun initLayoutView(): Int = R.layout.fragment_favorite

    override fun setupTopBarLayout() {

    }

    override fun initData() {
        bannerViewModel = ViewModelProvider(this)[BannerViewModel::class.java]
        //初始化轮播图
        initBanner()

        //TODO 现为模拟数据，待后期调整
        val goodsModels = ArrayList<FeaturedGoodsModel>()
        LocaleConstant.TEST_IMAGE.forEachIndexed { index, image ->
            val model = FeaturedGoodsModel()
            model.deviceImage = image
            model.deviceName = LocaleConstant.TEST_ARRAY[index]

            goodsModels.add(model)
        }
        val featuredGoodsAdapter = FeaturedGoodsAdapter(requireContext(), goodsModels)
        goodsRecyclerView.addItemDecoration(GridItemDecoration(requireContext()))
        goodsRecyclerView.adapter = featuredGoodsAdapter
        featuredGoodsAdapter.setOnGridItemClickListener(object :
            FeaturedGoodsAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {
                "此功能待后续完善".show(requireContext())
            }
        })

        //TODO 现为模拟数据，待后期调整
        val favoriteModels = ArrayList<FavoriteModel>()
        LocaleConstant.TEST_ARRAY.forEach {
            val model = FavoriteModel()
            model.deviceName = it

            favoriteModels.add(model)
        }
        val favoriteAdapter = FavoriteAdapter(requireContext(), favoriteModels)
        favoriteGridView.addItemDecoration(GridItemDecoration(requireContext()))
        favoriteGridView.adapter = favoriteAdapter
        favoriteAdapter.setOnGridItemClickListener(object :
            FavoriteAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {
                "此功能待后续完善".show(requireContext())
            }
        })
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {

    }

    private fun initBanner() {
        bannerViewModel.obtainBanner("", "0")
        bannerViewModel.listModel.observe(this, {
            if (it.code == 200) {
                val banner = shopBannerView as Banner<BannerImageModel.DataModel.RowsModel,
                        BannerImageAdapter<BannerImageModel.DataModel.RowsModel>>
                banner.apply {
                    setAdapter(object :
                        BannerImageAdapter<BannerImageModel.DataModel.RowsModel>(it.data.rows) {
                        override fun onBindView(
                            holder: BannerImageHolder,
                            data: BannerImageModel.DataModel.RowsModel,
                            position: Int,
                            size: Int
                        ) {
                            Glide.with(holder.itemView)
                                .load(data.bannerPicture.combineImagePath())
                                .placeholder(R.mipmap.load_image_error)
                                .into(holder.imageView)
                        }
                    })
                    indicator = CircleIndicator(requireContext())
                    addPageTransformer(AlphaPageTransformer())
                    addBannerLifecycleObserver(this@FavoriteFragment)

                    //Banner点击事件
                    setOnBannerListener { imageModel, _ ->
                        requireContext().navigatePageTo<WebsiteActivity>(imageModel.skipUrl)
                    }
                }
            }
        })
    }
}