package com.yidian.yac.newscontent.ui

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import cc.shinichi.library.ImagePreview
import com.yidian.common.base.BaseFragment
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import com.yidian.yac.newscontent.R
import com.yidian.yac.newscontent.adapter.BannerAdapter
import com.yidian.yac.newscontent.bean.xbrid.BannderBean
import com.yidian.yac.newscontent.utils.addLifecycle
import kotlinx.android.synthetic.main.fragment_group.*

/**
 * A simple [Fragment] subclass.
 * Use the [GoupFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class GroupFragment : BaseFragment() {
    companion object {
        fun newInstance(): GroupFragment {
            val fragment = GroupFragment()
            val args = bundleOf()
            fragment.arguments = args
            return fragment
        }
    }

    private val data: List<BannderBean>
        get() {
            val list: List<BannderBean> = mutableListOf<BannderBean>(
                BannderBean(
                    "0",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile2.renrendoc.com%2Ffileroot_temp3%2F2021-3%2F22%2F6d6d4388-7021-4580-9c52-5676233033d2%2F6d6d4388-7021-4580-9c52-5676233033d23.gif&refer=http%3A%2F%2Ffile2.renrendoc.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=d52c96296fd7ab32c4088c2d7999aed8"
                ),
                BannderBean(
                    "1",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0133e75674ca4332f8759f043f3536.gif&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622195236&t=a7a65502e63bf4bb2d7092852d33fcbc"
                ),
                BannderBean(
                    "2",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190508%2F18%2F1557310449-TxUZvFrqsd.jpg&refer=http%3A%2F%2Fimage.biaobaiju.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=926685f8fb93df270897c87a9de4c6de"
                ),
                BannderBean(
                    "3",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190508%2F15%2F1557300412-vboKAJtlMk.jpg&refer=http%3A%2F%2Fimage.biaobaiju.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=2435eae3839836377b2d560b70a77bcc"
                )
            )
            return list
        }

    override val contentViewId: Int
        get() = R.layout.fragment_group

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        initView()
        initListenner()
    }

    private fun initView() {
        //添加轮播图
        xbanner.setPointsIsVisible(true)
        xbanner.setBannerData(data)
        //加载广告图片支持gif
        xbanner.loadImage(context?.let { BannerAdapter(it) })
        xbanner.addLifecycle(lifecycle)
        xbanner.setOnItemClickListener { banner, model, view, position ->
            //处理点击事件
            startImagePreview(position)
        }
    }

    private fun initListenner() {
        btSelectCity.setOnClickListener {
            XEventManager.post(XBaseEvent("selectCity",""))
        }
    }


    /**
     * 添加图片预览
     */
    private fun startImagePreview(index: Int) {
        context?.let {
            val imagePreview = ImagePreview.getInstance().setImageList(
                arrayListOf(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile2.renrendoc.com%2Ffileroot_temp3%2F2021-3%2F22%2F6d6d4388-7021-4580-9c52-5676233033d2%2F6d6d4388-7021-4580-9c52-5676233033d23.gif&refer=http%3A%2F%2Ffile2.renrendoc.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=d52c96296fd7ab32c4088c2d7999aed8",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0133e75674ca4332f8759f043f3536.gif&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622195236&t=a7a65502e63bf4bb2d7092852d33fcbc",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190508%2F18%2F1557310449-TxUZvFrqsd.jpg&refer=http%3A%2F%2Fimage.biaobaiju.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=926685f8fb93df270897c87a9de4c6de",
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190508%2F15%2F1557300412-vboKAJtlMk.jpg&refer=http%3A%2F%2Fimage.biaobaiju.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622194124&t=2435eae3839836377b2d560b70a77bcc"
                )
            )
            imagePreview?.setContext(it)?.setIndex(index)?.start()
        }

    }
}