package cn.muchinfo.rma.view.base.home.commodity

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.GGConvertConfigData
import cn.muchinfo.rma.global.data.WrStandardData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import com.blankj.utilcode.util.ConvertUtils
import com.qmuiteam.qmui.widget.tab.QMUITabSegment
import mtp.polymer.com.autowidget.adapter.BaseAdapter
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.viewPager


/**
 * 期货品种
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @property root _FrameLayout
 * @constructor
 */
class FuturesVarietiesUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {


    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val spotVarietiesNormalAdapter: BaseAdapter<GGConvertConfigData, CommodityFutureViewHolder> =
        BaseAdapter { _, _ -> CommodityFutureViewHolder(activity, viewModel) }

    init {
        viewModel.queryGGConvertConfig()
    }

    /**
     * 外层主动刷新
     */
    fun refrashData(){
        viewModel.queryGGConvertConfig()
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            linearLayout {
                verticalLayout {
                    gravity = Gravity.LEFT
                    textView {
                        text = "源期货交易所/品种/"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(20)
                        marginStart = autoSize(36)
                    }
                    textView {
                        text = "代码/单位"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(36)
                    }
                }.lparams(autoSize(300), autoSize(97))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        text = "期货品种系数"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(300), autoSize(97))

                verticalLayout {
                    gravity = Gravity.RIGHT
                    textView {
                        text = "目标期货交易所/品种/"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(20)
                        marginStart = autoSize(36)
                    }
                    textView {
                        text = "代码/单位"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        marginEnd = autoSize(36)
                    }
                }.lparams(autoSize(300), autoSize(97))
            }.lparams(matchParent, autoSize(97))

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        viewModel.queryGGConvertConfig()
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = spotVarietiesNormalAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            viewModel.showGgConvertConfigDataList.bindOptional(context) {
                if (it?.isEmpty() == true || it?.size == 0) {
                    statusLayout.showEmpty()
                } else {
                    if (swipeToLayout.getIsRefreshing()) {
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    spotVarietiesNormalAdapter.setNewData(it)
                }
            }
        }
    }
}

/**
 * 期货品种（正常）
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @constructor
 */
class FuturesVarietiesNormalUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val spotVarietiesNormalAdapter: BaseAdapter<WrStandardData, CommodityViewHolder> =
        BaseAdapter { _, _ -> CommodityViewHolder(activity, viewModel, status = "") }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            creatSpotVarietiesHeadUI(text1 = "现货品种", text2 = "代码", text3 = "单位", text4 = "状态")

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = spotVarietiesNormalAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)
        }
    }
}

/**
 * 期货品种（停用）
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @property swipeToLayout SwipeToLoadLayout
 * @property statusLayout StatusLayout
 * @property spotVarietiesNormalAdapter BaseAdapter<WrStandardData, CommodityViewHolder>
 * @property root _FrameLayout
 * @constructor
 */
class FuturesVarietiesStopUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val spotVarietiesNormalAdapter: BaseAdapter<WrStandardData, CommodityViewHolder> =
        BaseAdapter { _, _ -> CommodityViewHolder(activity, viewModel, "") }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            creatSpotVarietiesHeadUI(text1 = "现货品种", text2 = "代码", text3 = "单位", text4 = "状态")

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = spotVarietiesNormalAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)
        }
    }
}

