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

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.BusinessData
import cn.muchinfo.rma.global.isShowTimeString
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.home.contract.emptyView
import com.blankj.utilcode.util.ActivityUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

class BusinessManagementViewholder(private val activity: AppCompatActivity, private val viewModel: BusinessManagementViewModel,private val type : String) : BaseViewHolder<BusinessData>(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?.applytime?.isShowTimeString("MM/dd HH:mm")
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 29
                            typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                        }.lparams(wrapContent, wrapContent) {
                            marginStart = autoSize(35)
                            topMargin = autoSize(25)
                        }

                        emptyView()

                        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){
                            text = it?.audittime?.isShowTimeString("MM/dd HH:mm")
                        }
                        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 = ViewEnumUtils.getContractType(it?.contracttype ?: "1")
                        }
                        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?.contractno
                        }
                        textSizeAuto = 22
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent){
                        topMargin = autoSize(15)
                    }

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

                /** 点价价格/结算类型 **/
                verticalLayout {
                    textView {
                        data.bindOptional(context) {
                            if (type == "1"){
                                text = it?.pricedPrice
                            }else{
                                text = it?.reckontype
                            }
                        }
                        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){
                            if (type == "1"){
                                text = it?.amount
                            }else{
                                text = it?.reckonvalue
                            }
                        }
                        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){
                            text = ViewEnumUtils.getBusinessManagementStatus(it?.applystatus ?: "")
                        }
                        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
                    }

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

            }.lparams(matchParent, autoSize(120))

            linearLayout {
                data.bindOptional(context) {
                    if (it?.indexSelect == 0) {
                        visibility = View.GONE
                    } else if (it?.indexSelect == 1) {
                        visibility = View.VISIBLE
                    }
                }
                gravity = Gravity.CENTER_VERTICAL

                emptyView()

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("data",data.value)
                        intent.putExtra("type","1")
                        if (type == "1"){
                            intent.setClass(context,MidpointsAuditActivity::class.java)
                        }else{
                            intent.setClass(context,SettlementAuditActivity::class.java)
                        }
                        ActivityUtils.startActivity(intent)
                    }
                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    data.bindOptional(context){
                        if (it?.applystatus == "1"){
                            visibility = View.VISIBLE
                        }else{
                            visibility = View.GONE
                        }
                    }
                    text = if (type == "1"){
                        "点价审核"
                    }else{
                        "结算审核"
                    }
                    textColorInt = R.color.rma_item_click_color
                    textSizeAuto = 26
                }.lparams(autoSize(168), autoSize(48)) {
                    marginEnd = autoSize(24)
                }

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("data",data.value)
                        intent.putExtra("type","2")
                        if (type == "1"){
                            intent.setClass(context,MidpointsAuditActivity::class.java)
                        }else{
                            intent.setClass(context,SettlementAuditActivity::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))
        }
    }

}