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

import android.view.Gravity
import android.view.inputmethod.EditorInfo
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.MTPEnums
import cn.muchinfo.rma.global.data.MiddleGoodsData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.home.commodity.CommodityInformationViewModel
import cn.muchinfo.rma.view.base.home.contract.emptyView
import cn.muchinfo.rma.view.base.home.contract.viewItemUI
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import mtp.polymer.com.autowidget.dialog.SelectData
import mtp.polymer.com.autowidget.dialog.creatBottomSheetDialog
import mtp.polymer.com.autowidget.dialog.creatGoodsHedgeBottomSheetDialog
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.textChangedListener

/**
 * 现货品种的套保品种选择
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @property operationType String  1 是新增，2是修改，3是停用，4详情 5恢复
 * @property itemSize IntArray
 * @constructor
 */
class InsuredVarietyViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel,
    private val operationType : String = ""
) : BaseViewHolder<SpotInfoData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    override fun _FrameLayout.createContentView() {
        verticalLayout {
            linearLayout {
                background = resources.getDrawable(R.color.white)
                gravity = Gravity.CENTER_VERTICAL
                onThrottleFirstClick {
                    if (operationType == "4" || operationType == "3" || operationType == "5"){
                        return@onThrottleFirstClick
                    }

                    activity.creatGoodsHedgeBottomSheetDialog("请选择套保品种", viewModel.middleGoodsNormalList.value ?: arrayListOf()) {
                        viewModel.changeMiddleGoodsList(type = "3",id = dataIndex.plus(1).toString(),ano = "",middlegoodsId = this.middlegoodsid ?: "",enumdicname = this.enumdicname ?: "",middleGoodsName = this.middlegoodsname ?: "")
                    }
                }
                textView {
                    text = "*"
                    textColorInt = R.color.rma_star_color
                    textSizeAuto = 31
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(37)
                }

                textView {
                    text = "套保品种"
                    textSizeAuto = 31
                    textColorInt = R.color.rma_black_33
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(10)
                }

                textView {
                    data.bindOptional(context){
                        text = it?.middleGoodsName
                        textColorInt = R.color.rma_black_33
                    }
                    text = "请选择套保品种"
                    textSizeAuto = 31
                    textColorInt = R.color.rma_hint_text_color_ccc
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(80)
                }

                emptyView()

                imageView {
                    imageResource = R.mipmap.rma_more
                }.lparams(autoSize(36), autoSize(36)) {
                    marginEnd = autoSize(25)
                }
            }.lparams(matchParent, autoSize(132))

            viewItemUI()

            linearLayout {
                background = resources.getDrawable(R.color.white)
                gravity = Gravity.CENTER_VERTICAL

                textView {
                    text = "单位"
                    textSizeAuto = 31
                    textColorInt = R.color.rma_black_33
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(60)
                }

                textView {
                    data.bindOptional(context){
                        if (it?.enumdicname.isNullOrEmpty()){
                            text = ""
                        }else{
                            text = it?.enumdicname
                            textColorInt = R.color.rma_black_33
                        }
                    }
                    text = "请选择套保品种后自动填入"
                    textSizeAuto = 31
                    textColorInt = R.color.rma_hint_text_color_ccc
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(140)
                }

                emptyView()

            }.lparams(matchParent, autoSize(132))

            viewItemUI()

            linearLayout {
                background = resources.getDrawable(R.color.white)
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    text = "*"
                    textColorInt = R.color.rma_star_color
                    textSizeAuto = 31
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(37)
                }

                textView {
                    data.bindOptional(context) {
                        text = "套保系数" + dataIndex.plus(1)
                    }
                    textSizeAuto = 31
                    textColorInt = R.color.rma_black_33
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(10)
                }

                editText {
                    isEnabled = !(operationType == "4" || operationType == "3" || operationType == "5")
                    data.bindOptional(context){
                        if (it?.value.isNullOrEmpty().not()){
                            setText(it?.value)
                            textColorInt = R.color.rma_black_33
                        }
                    }
                    hint = "请输入套保系数"
                    textChangedListener {
                        afterTextChanged {
                            if (it.toString().isNotEmpty()) {
                                viewModel.changeMiddleGoodsList(
                                    "3",
                                    dataIndex.plus(1).toString(),
                                    ano = it.toString()
                                )
                            }
                        }
                    }
                    background = null
                    inputType = EditorInfo.TYPE_CLASS_TEXT
                    hintColorStr = "#CCCCCC"
                    textSizeAuto = 31
                    textColorStr = "#333333"
                }.lparams(autoSize(300), autoSize(132)) {
                    marginStart = autoSize(40)
                }

                emptyView()

                imageView {
                    isEnabled = !(operationType == "4" || operationType == "3" || operationType == "5")
                    onThrottleFirstClick {
                        if (viewModel.middlegoodList.value?.size.toString() == "1") {
                            ToastUtils.showLong("最少输入一个型号")
                            return@onThrottleFirstClick
                        }
                        viewModel.changeMiddleGoodsList(
                            "2",
                            dataIndex.plus(1).toString(),
                            middlegoodsId = ""
                        )

                    }
                    imageResource = R.mipmap.rma_delete
                }.lparams(autoSize(30), autoSize(30)) {
                    marginEnd = autoSize(31)
                }
            }.lparams(matchParent, autoSize(132))
        }
    }

}