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

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.inputmethod.EditorInfo
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
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 com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.textChangedListener

/**
 * 输入型号和品牌的适配器
 * @property activity AppCompatActivity
 * @property viewModel CommodityInformationViewModel
 * @property type String 1是型号输入，2是品牌输入
 * operationType 1 是新增，2是修改，3是停用，4详情 5恢复
 * @property itemSize IntArray
 * @constructor
 */
class SpotModelViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: CommodityInformationViewModel,
    private val type : String,
    private val operationType : String = ""
): BaseViewHolder<SpotInfoData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    @SuppressLint("SetTextI18n")
    override fun _FrameLayout.createContentView() {
        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){
                    if (type == "1"){
                        text = "型号" + dataIndex.plus(1)
                    }else{
                        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")
                if (type == "1"){
                    hint = "请输入型号"
                }else{
                    hint = "请输入品牌"
                }
                textChangedListener {
                    afterTextChanged {
                        if (it.toString().isNotEmpty()){
                            if (type == "1"){
                                viewModel.changeSpotModelList("3",dataIndex.plus(1).toString(),it.toString())
                            }else{
                                viewModel.changeBrandModelList("3",dataIndex.plus(1).toString(),it.toString())
                            }
                        }
                    }
                }
                data.bindOptional(context){
                    setText(it?.value)
                }
                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 (type == "1"){
                        if (viewModel.spotModelList.value?.size.toString() == "1"){
                            ToastUtils.showLong("最少输入一个型号")
                            return@onThrottleFirstClick
                        }
                        viewModel.changeSpotModelList("2",dataIndex.plus(1).toString(),value = "")
                    }else if (type == "2"){
                        if (viewModel.brandModelList.value?.size.toString() == "1"){
                            ToastUtils.showLong("最少输入一个品牌")
                            return@onThrottleFirstClick
                        }
                        viewModel.changeBrandModelList("2",dataIndex.plus(1).toString(),value = "")
                    }

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

}