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

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.data.UserInfoData
import cn.muchinfo.rma.global.isBlankString
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.*

/**
 *
 * @property activity AppCompatActivity
 * @property viewModel CustomerDataViewModel
 * @property type String 1 是未提交 2是待审核 3是正常 4停用
 * @property itemSize IntArray
 * @constructor
 */
class CustomerDataViewHolder(
    private val activity: AppCompatActivity,
    private val viewModel: CustomerDataViewModel,
    private val type: String
) : BaseViewHolder<UserInfoData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    override fun _FrameLayout.createContentView() {
        verticalLayout {
            // 展开与收缩
            onThrottleShortFirstClick {
                viewModel.resetDataList(dataIndex, type)
            }
            // 双向绑定 UserInfoData
            data.bindOptional(context) {
                if (it?.indexSelect == 0) {
                    backgroundResource = R.color.white
                } else if (it?.indexSelect == 1) {
                    backgroundResource = R.color.rma_list_select_color
                }
            }
            linearLayout {
                linearLayout {

                    // 客户类型
                    textView {
                        data.bindOptional(context) {
                            text = if (it?.userinfotype.toString() == "2") "企业" else "个人"
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 29
                    }.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(70)
                        topMargin = autoSize(25)
                    }

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

                // 企业名称
                linearLayout {
                    textView {
                        data.bindOptional(context) { text = it?.customername }
                        textSizeAuto = 29
                        textColorInt = R.color.rma_black_33
                        maxLines = 2
                        ellipsize = TextUtils.TruncateAt.END
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }

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

                // 联系人手机
                linearLayout {
                    textView {
                        data.bindOptional(context) { text = it?.mobile?.isBlankString() }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 29
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }
                }.lparams(autoSize(220), autoSize(120))

                // 状态
                linearLayout {
                    textView {
                        data.bindOptional(context) { text = it?.statusdesc }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 29
                    }.lparams(wrapContent, wrapContent) {
                        topMargin = autoSize(25)
                    }
                }.lparams(autoSize(139), 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("status","4")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    data.bindOptional(context){
                        if (type == "2"){
                            if (it?.status == 2) {
                                visibility = View.VISIBLE
                            }else{
                                visibility = View.GONE
                            }
                        }else{
                            visibility = 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("status","3")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    data.bindOptional(context){
                        if (type == "2"){
                            if (it?.status == 2) {
                                visibility = View.VISIBLE
                            }else{
                                visibility = View.GONE
                            }
                        }else{
                            visibility = 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("status","2")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    data.bindOptional(context){
                        if (type == "2"){
                            if (it?.status == 5 || it?.status == 7) {
                                visibility = View.VISIBLE
                            }else{
                                visibility = View.GONE
                            }
                        }else{
                            visibility = 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("status","2")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    if (type == "1"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = 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("status","7")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    if (type == "4"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = 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("status","5")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    if (type == "3"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = 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("status","6")
                        intent.putExtra("userInfoData",data.value)
                        intent.setClass(context,AddCustomerDataActivity::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))
        }
    }

}