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

import android.content.Intent
import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.WrStandardData
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.base.home.contract.emptyView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

/**
 * 现货品种适配器
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @property itemSize IntArray
 * status 0 停用 1正常
 * @constructor
 */
class CommodityViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel,
    private val status: String
) : BaseViewHolder<WrStandardData>(activity) {

    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    override fun _FrameLayout.createContentView() {
        verticalLayout {

            data.bindOptional(context) {
                if (it?.indexSelect == 0) {
                    backgroundResource = R.color.white
                } else if (it?.indexSelect == 1) {
                    backgroundResource = R.color.rma_list_select_color
                }
            }

            onThrottleFirstClick {
                viewModel.resetwrStandardDataList(dataIndex, status)
            }

            linearLayout {
                linearLayout {
                    textView {
                        data.bindOptional(context) {
                            text = it?.wrstandardname
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 31
                        typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(35)
                        topMargin = autoSize(25)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_down
                        data.bindOptional(context) {
                            if (it?.indexSelect == 0) {
                                imageResource = R.mipmap.rma_down
                            } else if (it?.indexSelect == 1) {
                                imageResource = R.mipmap.rma_up
                            }
                        }
                    }.lparams(autoSize(36), autoSize(36)) {
                        marginEnd = autoSize(25)
                        topMargin = autoSize(25)
                    }
                }.lparams(autoSize(300), wrapContent)

                linearLayout {
                    textView {
                        data.bindOptional(context) {
                            text = it?.wrstandardcode
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 34
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }
                }.lparams(autoSize(200), matchParent)

                linearLayout {
                    textView {
                        data.bindOptional(context) {
                            text = it?.enumdicname
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 34
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }
                }.lparams(autoSize(200), matchParent)

                linearLayout {
                    emptyView()
                    textView {
                        data.bindOptional(context) {
                            text = if (it?.isvalid == "0") {
                                "停用"
                            } else {
                                "正常"
                            }
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 34
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                        marginEnd = autoSize(34)
                    }
                }.lparams(autoSize(200), matchParent)
            }.lparams(matchParent, autoSize(116))
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                emptyView()
                data.bindOptional(context) {
                    if (it?.indexSelect == 0) {
                        visibility = View.GONE
                    } else if (it?.indexSelect == 1) {
                        visibility = View.VISIBLE
                    }
                }
                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("type","2")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,AddSpotGoodsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    visibility = if (status == "1") {
                        View.VISIBLE
                    } else {
                        View.GONE
                    }
                    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(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("type","3")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,AddSpotGoodsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    if (status == "1") {
                        visibility = View.VISIBLE
                    } else {
                        visibility = View.GONE
                    }
                    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(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("type","5")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,AddSpotGoodsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    if (status == "0") {
                        visibility = View.VISIBLE
                    } else {
                        visibility = View.GONE
                    }
                    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(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("data",data.value)
                        intent.setClass(context,SpotVarietiesDetailsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    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(24)
                }
            }.lparams(matchParent, autoSize(76))
        }
    }

}