package cn.muchinfo.rma.view.base.exposure

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.MotionEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.RealExposureData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.app.Constant
import cn.muchinfo.rma.view.base.procurement.TopTabAdpater
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import org.jetbrains.anko.*

/**
 * 实时敞口的现货期货明细
 */
class RealTimeExposureDetailsActivity : BaseActivity<ExposureViewModel>() , ExposureDetailsContentAdapter.OnContentScrollListener{

    /**
     * type 1为实时敞口-现货明细 2 实时敞口-期货明细
     */
    private val type by lazy {
        intent.getStringExtra("type")
    }

    private val data by lazy {
        intent.getParcelableExtra<RealExposureData>("data") as RealExposureData
    }

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    lateinit var horScrollview: CustomHorizontalScrollView
    lateinit var recyclerView: RecyclerView

    val topAdapter by lazy {
        TopTabAdpater(this)
    }

    val exposureContentAdapter by lazy {
        ExposureDetailsContentAdapter(this,type)
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            topBar {
                commonLeftButton()
                commonTitle {
                    if (type == "1"){
                        text = "实时敞口-现货明细"
                    }else if (type == "2"){
                        text = "实时敞口-期货明细"
                    }
                }
            }
            background = resources.getDrawable(R.color.white)
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                backgroundColorResource = R.color.rma_blue_color
                textView {
                    text = "套保品种 : " + data.ENUMDICNAME + "(" + data.MiddleGoodsCode + ")"
                    textSizeAuto = 32
                    textColorInt = R.color.white
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(36)
                }
            }.lparams(matchParent, autoSize(70))

            verticalLayout {
                val titleString = if (type == "1") Constant.table_mobile_exposure_spot_detail else Constant.table_mobile_exposure_futures_detail
                //打开页面时请求的表头数据
                viewModel.queryRealDetailsTitle(tableKey = titleString,type = type ?: "1",middleGoodsId = data.MiddleGoodsID ?: "")
                exposureContentAdapter.setOnContentScrollListener(this@RealTimeExposureDetailsActivity)
                linearLayout {
                    linearLayout {
                        gravity = Gravity.LEFT or Gravity.CENTER_VERTICAL
                        textView {//头部右侧数据
                            viewModel.exposureRealTimeDetailsTitle.bindOptional(context) {
                                text = it?.get(0)?.columntitle?.replace("\\n", "\n") ?: ""
                            }
                            textColorStr = "#AAAAAA"
                            textSizeAuto = 24
                        }.lparams(wrapContent, wrapContent) {
                            marginStart = autoSize(30)
                        }
                    }.lparams(autoSize(SizeUtils.dp2px(135f)), autoSize(100))

                    inflateLayout<CustomHorizontalScrollView>(R.layout.scrollview) {
                        horScrollview = this
                        /**
                         * 用来同步内容列表的item左右滑动
                         */
                        setOnCustomScrollChangeListener { listener, scrollX, scrollY, oldScrollX, oldScrollY ->
                            exposureContentAdapter.offestX = scrollX
                            val viewHolderCacheList: List<ExposureDetailsContentAdapter.ItemViewHolder> =
                                exposureContentAdapter.viewHolderCacheList
                            if (null != viewHolderCacheList) {
                                val size = viewHolderCacheList.size
                                for (i in 0 until size) {
                                    viewHolderCacheList[i].exposure_horItemScrollview.scrollTo(
                                        scrollX,
                                        0
                                    )
                                }
                            }
                        }
                        relativeLayout {
                            recyclerView {//头部右侧数据
                                val linearLayoutManager = LinearLayoutManager(context)
                                linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
                                layoutManager = linearLayoutManager
                                background = resources.getDrawable(R.color.white)
                                adapter = topAdapter
                            }
                            viewModel.exposureRealTimeDetailsTitle.bindOptional(context) {
                                val titleStringList = arrayListOf<String>()
                                it?.subList(1, it.size)?.forEach { data ->
                                    titleStringList.add(data.columntitle?.replace("\\n", "\n") ?: "")
                                }
                                topAdapter.setDatas(titleStringList)
                            }
                        }
                    }.lparams(matchParent, matchParent)
                }.lparams(matchParent, autoSize(100))

                statusLayout(contentBlock = {
                    statusLayout = this
//                bindTaskStatus(viewModule.status)
                    swipeToLoadLayout {
                        swipeToLayout = this
                        setEnableRefresh(true)
                        setEnableLoadMore(false)
                        setOnRefreshListener {
                            /**
                             * 下拉刷新
                             */
                            if (type == "1"){
                                viewModel.queryExposureDetail(data.MiddleGoodsID ?: "")
                            }else{
                                viewModel.queryRealtimeExposurePosition(data.MiddleGoodsID ?: "")
                            }

                        }
                        setEnableScrollContentWhenLoaded(false)
                        setEnableLoadMoreWhenContentNotFull(false)

                        recyclerView {
                            background = resources.getDrawable(R.color.white)
                            adapter = exposureContentAdapter
                            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                                    super.onScrolled(recyclerView, dx, dy)
                                    val viewHolderCacheList: List<ExposureDetailsContentAdapter.ItemViewHolder> =
                                        exposureContentAdapter.viewHolderCacheList
                                    if (null != viewHolderCacheList) {
                                        val size = viewHolderCacheList.size
                                        for (i in 0 until size) {
                                            viewHolderCacheList[i].exposure_horItemScrollview.scrollTo(
                                                exposureContentAdapter.getOffestX(),
                                                0
                                            )
                                        }
                                    }
                                }
                            })
                        }
                    }
                }, emptyBlock = {
                    emptyView(hint = resources.getString(R.string.now_no_data))
                }).lparams(matchParent, matchParent)

                viewModel.exposureRealTimeDetailsDataList.bindOptional(context) {
                    LogUtils.eTag("dajsdhajkhsdsa",it?.size)
                    if (it?.isEmpty() == true || it?.size == 0) {
                        statusLayout.showEmpty()
                    } else {
                        if (swipeToLayout.getIsRefreshing()) {
                            swipeToLayout.finishRefresh()
//                        mHeadHeaderHSView.scrollTo(scrollXView,scrollYView)
                        }
                        statusLayout.showSuccess()
                        exposureContentAdapter.setDatas(it)
                    }
                }
            }.lparams(matchParent, matchParent)

        }
    }

    override fun onScroll(event: MotionEvent?) {
        //处理单个item滚动时,顶部tab需要联动
        if (null != horScrollview) horScrollview.onTouchEvent(event)
    }
}
