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

import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.channelinnerorderstatus
import cn.muchinfo.rma.global.data.futureOrders.DealOrderData
import cn.muchinfo.rma.global.data.futureOrders.FutureEntrustData
import cn.muchinfo.rma.global.isBlankString
import cn.muchinfo.rma.global.isShowTimeString
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.autoWidget.onThrottleFirstClick
import cn.muchinfo.rma.view.autoWidget.textColorInt
import cn.muchinfo.rma.view.autoWidget.textSizeAuto
import cn.muchinfo.rma.view.autoWidget.utils.NumberUtils
import cn.muchinfo.rma.view.base.home.contract.emptyView
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

/**
 * 交易查询历史的viewholder
 * @property activity AppCompatActivity
 * @property viewModel TradingQueryViewModel
 * @property itemSize IntArray
 * type 1是历史委托 2是历史成交
 * @constructor
 */
class EntrustHistoryViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: TradingQueryViewModel
) : BaseViewHolder<FutureEntrustData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    override fun _FrameLayout.createContentView() {
        verticalLayout {
            onThrottleFirstClick {
                viewModel.clickItemEntrust(dataIndex)
            }
            data.bindOptional(context) {
                if (it?.selected == false) {
                    backgroundResource = R.color.white
                } else if (it?.selected == true) {
                    backgroundResource = R.color.rma_list_select_color
                }
            }
            linearLayout {
                verticalLayout {
                    gravity = Gravity.LEFT
                    textView {
                        data.bindOptional(context){
                            text = it?.goodsname
                        }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                    }.lparams(wrapContent, wrapContent){
                        topMargin = autoSize(20)
                        marginStart = autoSize(36)
                    }
                    textView {
                        data.bindOptional(context){
                            text = ViewEnumUtils.getDealOrderType(it?.buyorsell.toString() ?: "",it?.channelbuildtype.toString() ?: "")
                            textColorInt = if (it?.buyorsell == 0){
                                R.color.rma_red_color
                            }else{
                                R.color.rma_green_color
                            }
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.rma_star_color
                    }.lparams(wrapContent, wrapContent){
                        marginStart = autoSize(36)
                    }
                }.lparams(autoSize(300), autoSize(100))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        data.bindOptional(context){
                            text = it?.orderqty.toString()
                        }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                    }.lparams(wrapContent, wrapContent)
                    textView {
                        data.bindOptional(context){
                            text = NumberUtils.roundNum(it?.orderprice.toString(),2)
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(300), autoSize(100))

                verticalLayout {
                    gravity = Gravity.RIGHT
                    /** 状态 **/
                    textView {
                        data.bindOptional(context){
                            text = it?.channelinnerorderstatus.toString().channelinnerorderstatus()
                        }
                        text= ""
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent){
                        topMargin = autoSize(20)
                        marginEnd = autoSize(36)
                    }
                    /** 时间 **/
                    textView {
                        data.bindOptional(context){
                            text = it?.ordertime?.isShowTimeString("yyyy-MM-dd mm:HH:ss")
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent){
                        marginEnd = autoSize(36)
                    }
                }.lparams(autoSize(300), autoSize(100))
            }.lparams(matchParent, autoSize(100))

            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                data.bindOptional(context) {
                    if (it?.selected == true) {
                        visibility = View.VISIBLE
                    } else if (it?.selected == false) {
                        visibility = View.GONE
                    }
                }
                emptyView()

                textView {
                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    text = "图表"
                    textColorInt = R.color.rma_item_click_color
                    textSizeAuto = 26
                }.lparams(autoSize(120), autoSize(48)) {
                    marginEnd = autoSize(36)
                }

                textView {
                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    text = "详情"
                    textColorInt = R.color.rma_item_click_color
                    textSizeAuto = 26
                }.lparams(autoSize(120), autoSize(48)) {
                    marginEnd = autoSize(36)
                }
            }.lparams(matchParent, autoSize(90))
        }.lparams(matchParent, wrapContent)
    }

}