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

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.global.isBlankString
import cn.muchinfo.rma.global.isShowTimeString
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.autoWidget.onThrottleFirstClick
import cn.muchinfo.rma.view.autoWidget.textColorInt
import cn.muchinfo.rma.view.autoWidget.textSizeAuto
import com.blankj.utilcode.util.ToastUtils
import org.jetbrains.anko.*

fun _LinearLayout.viewItemUI() {
    view {
        background = resources.getDrawable(R.color.rma_hint_line_color_eee)
    }.lparams(matchParent, autoSize(1)) {
        marginStart = autoSize(60)
    }
}


fun _LinearLayout.baseInformationView(textString: String) {
    linearLayout {
        background = resources.getDrawable(R.color.main_hit_bg_color)
        gravity = Gravity.CENTER_VERTICAL
        textView {
            text = textString
            textSizeAuto = 31
            textColorInt = R.color.rma_hint_title_text_color
        }.lparams(wrapContent, wrapContent) {
            marginStart = autoSize(59)
        }
    }.lparams(matchParent, autoSize(60))
}

fun _LinearLayout.emptyView() {
    view { }.lparams(0, autoSize(1), 1f)
}

fun _LinearLayout.contractDetailOneView(data: ContractData) {
    verticalLayout {
        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "合同编号"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.contractno
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "合同类型"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = ViewEnumUtils.getContractType(data.contracttype ?: "") + "合同"
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "采购方"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.buyusername
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(121)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "销售方"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.sellusername
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(121)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "合同附件"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                onThrottleFirstClick {
                    ToastUtils.showLong("查看附件")
                }
                text = "查看附件"
                textColorInt = R.color.rma_yellow_text_color
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))
    }
}

fun _LinearLayout.contractDetailTwoView(data: ContractData) {
    verticalLayout {
        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "现货品种"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.deliverygoodsname
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "产品类型"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = ViewEnumUtils.getProductType(data.producttype ?: "")
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "标仓系数"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.convertfactor
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "型        号"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.modelname?.isBlankString()
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "品        牌"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.brandname?.isBlankString()
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "商品规格"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.spotgoodsdesc
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
                maxLines = 2
                ellipsize = TextUtils.TruncateAt.END
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
                marginEnd = autoSize(59)
            }
        }.lparams(matchParent, 220)
    }
}

@SuppressLint("SetTextI18n")
fun _LinearLayout.contractDetailThreeView(data: ContractData) {
    verticalLayout {
        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "定价类型"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = ViewEnumUtils.getPriceType(data.pricetype ?: "")
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "数        量"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.qty + data.enumdicname
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        linearLayout {
            //只有在一口价时才显示
            visibility = if (data.pricetype == "1") { View.VISIBLE } else { View.GONE }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "价        格"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.price + "元"
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            visibility = if (data.pricetype == "1") {
                View.GONE
            } else {
                View.VISIBLE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "点价合约"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.goodsname
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            if (data.pricetype == "1") {
                visibility = View.GONE
            } else {
                visibility = View.VISIBLE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "升  贴  水"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = "100元"
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            visibility = View.GONE
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "点  贴  水"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = "100元"
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            if (data.startdate?.isNotEmpty() == true) {
                visibility = View.VISIBLE
            } else {
                visibility = View.GONE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "点  价  期"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.startdate?.isShowTimeString("yyyy-MM-dd") + " ~ " + data.enddate?.isShowTimeString("yyyy-MM-dd")
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            if (data.deliveryenddate?.isNotEmpty() == true) {
                visibility = View.VISIBLE
            } else {
                visibility = View.GONE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "交  收  期"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.deliverystartdate?.isShowTimeString("yyyy-MM-dd") + " ~ " + data.deliveryenddate?.isShowTimeString("yyyy-MM-dd")
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))
    }
}

fun _LinearLayout.contractDetailFourView(data: ContractData) {
    verticalLayout {
        linearLayout {
            if (data.contractmargin.isNullOrEmpty()) {
                visibility = View.GONE
            } else {
                visibility = View.VISIBLE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "保  证  金"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.contractmargin
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
            }
        }.lparams(matchParent, autoSize(132))

        viewItemUI()

        linearLayout {
            if (data.remark.isNullOrEmpty()) {
                visibility = View.GONE
            } else {
                visibility = View.VISIBLE
            }
            gravity = Gravity.CENTER_VERTICAL
            textView {
                text = "备        注"
                textSizeAuto = 31
                textColorInt = R.color.p_normal_summary_txt_color
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(59)
            }

            textView {
                text = data.remark
                textColorInt = R.color.rma_black_33
                textSizeAuto = 31
                maxLines = 2
                ellipsize = TextUtils.TruncateAt.END
            }.lparams(wrapContent, wrapContent) {
                marginStart = autoSize(80)
                marginEnd = autoSize(59)
            }
        }.lparams(matchParent, 220)
    }
}
