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

import android.os.Bundle
import android.view.Gravity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.FinancialManageData
import cn.muchinfo.rma.global.data.NumberSpotData
import cn.muchinfo.rma.global.data.NumberSpotDetailsData
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.home.financial.FinancialViewholder
import mtp.polymer.com.autowidget.adapter.BaseAdapter
import org.jetbrains.anko.*

/**
 * 现货头寸-现货明细
 * 期货头寸-期货明细  详情列表页
 * @property type String?
 */
class NumberExposureDetailsActivity : BaseActivity<ExposureViewModel>(){

    /**
     * type 1 现货头寸（数量）明细 2 期货头寸（数量）明细
     */
    private val type by lazy {
        intent.getStringExtra("type")
    }

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

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

    private val invoiceAdapter: BaseAdapter<NumberSpotDetailsData, ExposurePositionDetailsViewholder> = BaseAdapter { _, _ -> ExposurePositionDetailsViewholder(this, viewModel,type ?: "1") }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            if (type == "1"){
                viewModel.queryExposureSpotDetail(data.wrstandardid ?: "")
            }else{
                viewModel.queryExposureHedgePositionDetail(data.goodsid ?: "")
            }
            topBar {
                commonLeftButton()
                commonTitle {
                    if (type == "1"){
                        text = "现货头寸-现货明细"
                    }else if (type == "2"){
                        text = "期货头寸-期货明细"
                    }
                }
            }
            background = resources.getDrawable(R.color.white)

            if (type == "1"){
                SpotTitle()
            }else{
                FutureTitle()
            }

            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 = invoiceAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

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

}

/**
 * 现货头寸-现货明细-头寸
 * @receiver _LinearLayout
 */
fun _LinearLayout.SpotTitle(){
    linearLayout {
        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "时间"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
            textView {
                text = "编号"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "类型"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "现货品种"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)

        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.RIGHT
            textView {
                text = "数量"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent){
                marginEnd = autoSize(36)
                topMargin = autoSize(30)
            }
        }.lparams(autoSize(225), autoSize(92))
    }.lparams(matchParent, autoSize(92))
}

/**
 * 期货头寸-期货明细-头
 * @receiver _LinearLayout
 */
fun _LinearLayout.FutureTitle(){
    linearLayout {
        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "时间"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
            textView {
                text = "期货合约"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "方向"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "开平"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent)

        }.lparams(autoSize(225), autoSize(92))

        verticalLayout {
            gravity = Gravity.RIGHT
            textView {
                text = "数量（手）"
                textColorInt = R.color.hint_text_color
                textSizeAuto = 24
            }.lparams(wrapContent, wrapContent){
                marginEnd = autoSize(36)
                topMargin = autoSize(30)
            }
        }.lparams(autoSize(225), autoSize(92))
    }.lparams(matchParent, autoSize(92))
}