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

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Typeface
import android.text.TextUtils
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.data.ContractData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import com.blankj.utilcode.util.ActivityUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

class ContractDataViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: ContractViewModel,
    private val type: String
) : BaseViewHolder<ContractData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    @SuppressLint("SetTextI18n")
    override fun _FrameLayout.createContentView() {
        verticalLayout {
            onThrottleShortFirstClick {
                viewModel.resetDataList(dataIndex, type)
            }
            data.bindOptional(context) {
                if (it?.indexSelect == 0) {
                    backgroundResource = R.color.white
                } else if (it?.indexSelect == 1) {
                    backgroundResource = R.color.rma_list_select_color
                }
            }
            linearLayout {
                verticalLayout {
                    linearLayout {
                        /** 合同编号 **/
                        textView {
                            data.bindOptional(context) {
                                text = it?.contractno
                            }
                            maxLines = 1
                            maxEms = 7
                            ellipsize = TextUtils.TruncateAt.END
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 29
                            typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                        }.lparams(wrapContent, wrapContent) {
                            marginStart = autoSize(35)
                            topMargin = autoSize(25)
                        }

                        view {

                        }.lparams(0, autoSize(1), 1f)
                        imageView {
                            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(matchParent, wrapContent)

                    textView {
                        data.bindOptional(context) {
                            val typeString = if (it?.contracttype == "1") {
                                "采购"
                            } else {
                                "销售"
                            }
                            text = "$typeString/" + ViewEnumUtils.getContractStatus(
                                it?.contracctstatus ?: ""
                            )
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(35)
                        topMargin = autoSize(15)
                    }

                }.lparams(autoSize(335), autoSize(120))

                verticalLayout {
                    textView {
                        data.bindOptional(context) {
                            text = if (it?.contracttype == "1") {
                                it.buyusername
                            } else {
                                it?.sellusername
                            }
                        }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                        maxLines = 1
                        ellipsize = TextUtils.TruncateAt.END
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }

                    textView {
                        data.bindOptional(context) {
                            text = it?.qty + it?.enumdicname
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(15)
                    }

                }.lparams(autoSize(150), autoSize(120))

                verticalLayout {
                    textView {
                        data.bindOptional(context) {
                            text = it?.deliverygoodsname
                        }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                        maxLines = 1
                        ellipsize = TextUtils.TruncateAt.END
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                        gravity = Gravity.RIGHT
                    }
                    textView {
                        data.bindOptional(context) {
                            text = it?.spotgoodsdesc
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(15)
                        gravity = Gravity.RIGHT
                    }
                }.lparams(autoSize(145), autoSize(120))

                verticalLayout {

                    textView {
                        data.bindOptional(context) {
                            when (it?.producttype) {
                                "1" -> {
                                    text = "标准"
                                }
                                "2" -> {
                                    text = "等标"
                                }
                                "3" -> {
                                    text = "非标"
                                }
                            }
                        }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                        maxLines = 1
                        ellipsize = TextUtils.TruncateAt.END
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                        marginEnd = autoSize(35)
                        gravity = Gravity.RIGHT
                    }
                    textView {
                        data.bindOptional(context) {
                            text = it?.convertfactor
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(15)
                        marginEnd = autoSize(35)
                        gravity = Gravity.RIGHT
                    }
                }.lparams(autoSize(250), autoSize(120))

            }.lparams(matchParent, autoSize(120))

            linearLayout {
                onThrottleFirstClick {
                    val intent = Intent()
                    intent.putExtra("type","3")
                    intent.putExtra("data",data.value)
                    intent.setClass(context,ContractDetailsActivity::class.java)
                    ActivityUtils.startActivity(intent)
                }
                data.bindOptional(context) {
                    if (it?.indexSelect == 0) {
                        visibility = View.GONE
                    } else if (it?.indexSelect == 1) {
                        visibility = View.VISIBLE
                    }
                }
                gravity = Gravity.CENTER_VERTICAL

                emptyView()

                textView {
                    data.bindOptional(context) {
                        visibility = if (type == "2") {
                            if (it?.contracctstatus == "1") {
                                View.VISIBLE
                            } else {
                                View.GONE
                            }
                        } 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","2")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,ContractDetailsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    data.bindOptional(context) {
                        visibility = if (type == "2") {
                            if (it?.contracctstatus == "1") {
                                View.VISIBLE
                            } else {
                                View.GONE
                            }
                        } 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("operationtype","2")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,AddContractActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    visibility = if (type == "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","4")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,ContractDetailsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    visibility = if (type == "3") {
                        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(168), autoSize(48)) {
                    marginEnd = autoSize(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("operationtype","2")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,AddContractActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    data.bindOptional(context) {
                        visibility = if (type == "2") {
                            if (it?.contracctstatus == "4" || it?.contracctstatus == "6") {
                                View.VISIBLE
                            } else {
                                View.GONE
                            }
                        } else {
                            View.GONE
                        }
                    }
                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    textResource = R.string.commit_again
                    textColorInt = R.color.rma_item_click_color
                    textSizeAuto = 26
                }.lparams(autoSize(168), autoSize(48)) {
                    marginEnd = autoSize(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("type","1")
                        intent.putExtra("data",data.value)
                        intent.setClass(context,ContractDetailsActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }

                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    textResource = R.string.details
                    textColorInt = R.color.rma_item_click_color
                    textSizeAuto = 26
                }.lparams(autoSize(120), autoSize(48)) {
                    marginEnd = autoSize(36)
                }
            }.lparams(matchParent, autoSize(72))
        }
    }

}