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

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.FinancialManageData
import cn.muchinfo.rma.global.isShowTimeString
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.*

/**
 * 适配财务列变的viewholder
 */
class FinancialViewholder(private val activity: AppCompatActivity, private val viewModel: FinancialViewModel,private val type : String) : BaseViewHolder<FinancialManageData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    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)
                        }

                        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){
                            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"){
                                if (it?.kxtype == "1"){
                                    text = "付款"
                                }else if (it?.kxtype == "2"){
                                    text = "退款"
                                }else if (it?.kxtype == "3"){
                                    text = "付款/退款"
                                }
                            }else{
                                if (it?.contracttype == "1"){
                                    text = "收票"
                                }else if (it?.contracttype == "2"){
                                    text = "开票"
                                }
                            }

                        }
                        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"){
                                if (it?.kxtype == "1"){
                                    text = it.payamount
                                }else if (it?.kxtype == "2"){
                                    text = it.deductamount
                                }else if (it?.kxtype == "3"){
                                    text = it.payamount +  "/" + it.deductamount
                                }
                            }else{
                                text = it?.invoiceAmount
                            }

                        }
                        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?.contractstatus ?: "")
                        }
                        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
                view {

                }.lparams(0, autoSize(1), 1f)

                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("data",data.value)
                        intent.putExtra("type",type)
                        intent.setClass(context, PaymentReviewActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    backgroundResource = R.drawable.rma_item_click_bg
                    gravity = Gravity.CENTER
                    data.bindOptional(context){
                        if (it?.contractstatus == "2"){
                            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 {
                    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))
        }
    }

}