package com.zxbw.modulemain.activity

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.DetailBannerAdapter
import com.zxbw.modulemain.adapter.GoodsListAdapter
import com.zxbw.modulemain.contract.TbGoodsDetailContract
import com.zxbw.modulemain.presenter.TbGoodsDetailPresenter
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.qy.TbGoodsItemEntity
import com.zxhl.cms.net.model.shop.ShopListEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import kotlinx.android.synthetic.main.activity_layout_tb_goods_detail.*
import me.jessyan.autosize.utils.ScreenUtils

/**
 * @author (wangXuewei)
 * @datetime 2022-05-24 17:12 GMT+8
 * @detail : 淘宝商品详情
 */
class TbGoodsDetailActivity : BaseActivity(), TbGoodsDetailContract.View {

    private var mGoodsAdapter: GoodsListAdapter? = null;
    private var mPresenter: TbGoodsDetailPresenter? = null;
    private var goodsDetail: TbGoodsItemEntity? = null

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_tb_goods_detail
    }

    override fun init() {
        var statusBarHeight = ScreenUtils.getStatusBarHeight()
        if (statusBarHeight <= 0) {
            statusBarHeight = 40;
        }
        id_tb_goods_detail_title_sys_bar_view.layoutParams.height = statusBarHeight

        goodsDetail = intent.getSerializableExtra("goods") as TbGoodsItemEntity

        mPresenter = TbGoodsDetailPresenter(this)
        mGoodsAdapter = GoodsListAdapter(this,
            OnRecycleItemClickListener<TbGoodsItemEntity> { view, position, data ->
                JumpUtils.TbGoodsDetailJump()
            })
        id_rcl_recommend_goods_list?.layoutManager = LinearLayoutManager(this)
        id_rcl_recommend_goods_list.adapter = mGoodsAdapter

        id_img_close_btn?.setOnClickListener(this)

        initView()

        var link = ""
        if (!goodsDetail?.item_url.isNullOrEmpty()) {
            link = goodsDetail?.item_url!!
        } else if (!goodsDetail?.item_id.isNullOrEmpty()) {
            link = goodsDetail?.item_id!!
        }
        mPresenter?.getConvertLink(link)
        mPresenter?.getGoodsDetail(goodsDetail?.item_id ?: "")


    }

    private fun initView() {
        if (goodsDetail == null) {
            return
        }


        id_tv_goods_name?.text = goodsDetail?.title
        if (!goodsDetail?.small_images.isNullOrEmpty()) {
            var mPagerAdapter = DetailBannerAdapter(goodsDetail?.small_images!!)
            id_detail_top_banner.adapter = mPagerAdapter
            id_tv_vp_index.text = "${1}/${goodsDetail?.small_images?.size ?: 0 + 1}"
            id_detail_top_banner.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    id_tv_vp_index.text =
                        "${position + 1}/${goodsDetail?.small_images?.size ?: 0 + 1}"
                }

                override fun onPageScrollStateChanged(state: Int) {
                }
            })
        }

    }

    override fun onClick(v: View?) {
        when (v) {
            id_img_close_btn -> {
                finish()
            }
        }
    }

    override fun setConvertLink() {

    }

    override fun setGoodsDetail() {

    }

}