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

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.WrStandardData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import com.blankj.utilcode.util.ConvertUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator
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

/**
 * 现货品种 与套保和期货uI分开写，不晓得是不是同样的页面并防止他们改单独的页面
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @constructor
 */
class SpotVarietiesUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {

    lateinit var segment: QMUITabSegment
    var selectedTabIndex: Int = 0
    private lateinit var viewPager: ViewPager

    private val tabsArray: List<String> by lazy {
        arrayListOf(
            "正常","停用"
        )
    }
    private val spotVarietiesNormalUI by lazy { SpotVarietiesNormalUI(activity,viewModel) }
    val spotVarietiesStopUI by lazy { SpotVarietiesStopUI(activity,viewModel) }

    private val pagerAdapter: PagerAdapter by lazy {
        object : PagerAdapter() {

            override fun getPageTitle(position: Int): CharSequence? = tabsArray[position]

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                when (position) {
                    0 -> {
                        return spotVarietiesNormalUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    1 -> {
                        return spotVarietiesStopUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    else -> return super.instantiateItem(container, position)
                }
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                (`object` as? View)?.let { container.removeView(it) }
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

            override fun getCount(): Int = tabsArray.size

        }
    }


    fun refrashData(){
        if (selectedTabIndex == 0){//刷新正常liebiao
            viewModel.queryWrStandard(status = "1")
        }else{//刷新停用列表
            viewModel.queryWrStandard(status = "0")
        }
    }


    val root = _FrameLayout(activity).apply {
        verticalLayout {
            tabSegment {
                segment = this
                background = resources.getDrawable(R.color.white)
                addOnTabSelectedListener(object :
                    QMUITabSegment.OnTabSelectedListener {
                    override fun onDoubleTap(index: Int) {}
                    override fun onTabReselected(index: Int) {}
                    override fun onTabUnselected(index: Int) {}
                    override fun onTabSelected(index: Int) {
                        selectedTabIndex = index
                    }
                })
                setIndicator(
                    QMUITabIndicator(
                        QMUIDisplayHelper.dp2px(context, 2),
                        false,
                        true
                    )
                )
                setDefaultTextSize(ConvertUtils.sp2px(16f), ConvertUtils.sp2px(16f))
            }.lparams(matchParent, autoSize(80))

            view {
                background = resources.getDrawable(R.color.main_hit_bg_color)
            }.lparams(matchParent, autoSize(1)) {
                marginStart = autoSize(32)
                marginEnd = autoSize(32)
            }
            viewPager {
                background = resources.getDrawable(R.color.white)
                viewPager = this
                adapter = pagerAdapter
            }.lparams(matchParent, matchParent)

            segment.setupWithViewPager(viewPager)
            segment.selectTab(selectedTabIndex)
        }

    }
}

/**
 * 现货品种(正常)
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @constructor
 */
class SpotVarietiesNormalUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {

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

    init {
        viewModel.queryWrStandard(status = "1")
    }

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

    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 {
                        viewModel.queryWrStandard(status = "1")
                    }
                    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.wrStandardnormalList.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 SpotVarietiesStopUI(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel
) {

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

    init {
        viewModel.queryWrStandard(status = "0")
    }

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

    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 {
                        viewModel.queryWrStandard(status = "0")
                    }
                    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.wrStandardDisableList.bindOptional(context){
                if (it?.isEmpty() == true || it?.size == 0){
                    statusLayout.showEmpty()
                }else {
                    if (swipeToLayout.getIsRefreshing()){
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    spotVarietiesNormalAdapter.setNewData(it)
                }
            }
        }
    }
}

