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

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.inputmethod.EditorInfo
import android.widget.CheckBox
import android.widget.EditText
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.protobuf.protoclasses.ErmcpMI1
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.home.contract.emptyView
import com.blankj.utilcode.util.ToastUtils
import com.google.protobuf.ByteString
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*
import org.json.JSONObject

/**
 * 款项登记页面
 */
class PaymentRegistrationActivity : BaseActivity<RegistrationViewModel>() {

    /**     付款申请选择框 **/
    lateinit var pay_checkbox : CheckBox

    /** 退款申请选择框 **/
    lateinit var refund_checkbox : CheckBox

    /** 登记金额输入框 **/
    lateinit var registration_amount_edittext : EditText

    /** 备注信息 **/
    lateinit var ramask_edittext: EditText

    //type 为1是采购 2是销售
    private val type by lazy {
        intent.getStringExtra("type")
    }

    //传进来的数据
    private val data by lazy {
        intent.getParcelableExtra<ContractData>("data") as ContractData
    }

    private val dialog by lazy { createLoadingDialog(hintStr = "请求中...") }

    @SuppressLint("SetTextI18n", "RtlHardcoded")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            dialog.bindTaskStatus(context,viewModel.loadingDialogStatus)
            val margingPadding = autoSize(47)
            background = resources.getDrawable(R.color.main_hit_bg_color)
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "款项登记"
                }
            }

            relativeLayout {
                scrollView {
                    verticalLayout {

                        verticalLayout {
                            background = resources.getDrawable(R.color.white)
                            textView {
                                text = "合同基本信息"
                                textColorInt = R.color.rma_blue_color
                                textSizeAuto = 34
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = margingPadding
                                topMargin = autoSize(25)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "合同编号"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = ViewEnumUtils.getContractType(data.contracttype ?: "") + "/"
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = data.spotcontractid
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)
                                linearLayout {
                                    textView {
                                        text = "现货品种"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.deliverygoodsname
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = margingPadding
                                        }
                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60)) {
                                topMargin = autoSize(30)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "定价类型"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = ViewEnumUtils.getPriceType(data.pricetype ?: "")
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)
                                linearLayout {
                                    textView {
                                        text = "暂定价"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.price + "元"
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = margingPadding
                                        }
                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60))

                            textView {
                                text = "贷款总额"
                                textSizeAuto = 24
                                textColorInt = R.color.p_normal_summary_txt_color
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(478)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = if (type == "1") {
                                            "销售方"
                                        } else {
                                            "采购方"
                                        }
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.accountname
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)

                            }.lparams(matchParent, autoSize(60))

                            view {
                                background = resources.getDrawable(R.color.rma_hint_line_color_eee)
                            }.lparams(matchParent, autoSize(1)) {
                                marginStart = autoSize(36)
                                marginEnd = autoSize(36)
                                topMargin = autoSize(30)
                            }

                            textView {
                                text = "已登记点价信息"
                                textColorInt = R.color.rma_blue_color
                                textSizeAuto = 34
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = margingPadding
                                topMargin = autoSize(25)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "已定价量"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.pricedqty
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)
                                linearLayout {
                                    textView {
                                        text = "其他费用"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.reckonotheramount
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = margingPadding
                                        }

                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60)) {
                                topMargin = autoSize(25)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "调整金额"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.reckonadjustamount
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)
                                linearLayout {
                                    textView {
                                        text = "总计金额"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.totalamount
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = margingPadding
                                        }
                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60))

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "贷款总额"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.loanamount
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)
                                linearLayout {
                                    textView {
                                        text = "已支付款"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.payamount
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = margingPadding
                                        }
                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60))

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "保证金"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.margin
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)

                            }.lparams(matchParent, autoSize(60)) {
                                bottomMargin = autoSize(45)
                            }

                        }.lparams(matchParent, wrapContent)

                        verticalLayout {
                            backgroundDrawable = createRoundRectDrawable("#ffffff", 14)

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                textView {
                                    text = "本次款项信息"
                                    textColorInt = R.color.rma_blue_color
                                    textSizeAuto = 34
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(36)
                                }

                                imageView {
                                    onThrottleFirstClick {
                                        showPopups(context = context,textString = "货款总额=已定价额+调整金额\n合计总额=货款总额+其他费用+保证金",v = this)
                                    }
                                    imageResource = R.mipmap.rma_zhushi
                                }.lparams(autoSize(36), autoSize(36)){
                                    marginStart = autoSize(29)
                                }
                            }.lparams(matchParent, autoSize(60)){
                                topMargin = autoSize(36)
                            }

                            linearLayout {
                                textView {
                                    text = "待支付款"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(36)
                                }

                                textView {
                                    text = data.unpayamount + "元"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(82)
                                }
                            }.lparams(matchParent, wrapContent){
                                topMargin = autoSize(40)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                textView {
                                    text = "款项类型"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(36)
                                }

                                checkBox {
                                    pay_checkbox = this
                                    buttonDrawable = null
                                    background = resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(80)
                                }

                                textView {
                                    text = "付款申请"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                                checkBox {
                                    refund_checkbox = this
                                    buttonDrawable = null
                                    background = resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(100)
                                }

                                textView {
                                    text = "退款通知"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)

                                }
                            }.lparams(matchParent, autoSize(60)){
                                topMargin = autoSize(80)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                textView {
                                    text = "登记金额"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(36)
                                }


                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)

                                    editText {
                                        registration_amount_edittext = this
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "0.00"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                                        setDecimalInputType()
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(72)) {
                                    marginStart = autoSize(80)
                                }

                                textView {
                                    text = "元"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }
                            }.lparams(matchParent, autoSize(80)){
                                topMargin = autoSize(50)
                            }

                            linearLayout {
                                textView {
                                    text = "备注"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(36)
                                }

                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)
                                    editText {
                                        ramask_edittext = this
                                        setPadding(0, 10, 0, 0)
                                        hint = "请输入备注信息"
                                        gravity = Gravity.LEFT or Gravity.TOP
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_TEXT
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(220)) {
                                    marginStart = autoSize(138)
                                }
                            }.lparams(matchParent, autoSize(220)){
                                topMargin = autoSize(50)
                                bottomMargin = autoSize(40)
                            }

                        }.lparams(matchParent, wrapContent) {
                            marginStart = autoSize(24)
                            marginEnd = autoSize(24)
                            topMargin = autoSize(24)
                            bottomMargin = autoSize(200)
                        }


                    }.lparams(autoSize(900), matchParent)
                }

                verticalLayout {
                    view { }.lparams(autoSize(1), 0, 1f)
                    linearLayout {
                        background = resources.getDrawable(R.color.white)
                        gravity = Gravity.CENTER_VERTICAL

                        textView {
                            onThrottleFirstClick {
                                if (checkUpdate().not()){
                                    return@onThrottleFirstClick
                                }
                                viewModel.orderPerationContract(info = getPerationContractInfo(),operateType = 1,remark = ramask_edittext.text.toString()){
                                    finish()
                                }
                            }
                            gravity = Gravity.CENTER
                            backgroundResource = R.mipmap.rma_submit_bg
                            text = "款项登记"
                            textColorInt = R.color.white
                            textSizeAuto = 38
                        }.lparams(autoSize(780), autoSize(119)) {
                            marginStart = autoSize(59)
                        }

                    }.lparams(matchParent, autoSize(144)) {
                        gravity = Gravity.BOTTOM
                    }
                }.lparams(matchParent, matchParent)
            }
        }
    }

    /**
     * 获取合同操作info
     * @return ErmcpMI1.ContractOperateApplyInfo
     */
    private fun getPerationContractInfo(): ErmcpMI1.ContractOperateApplyInfo {
        val json = JSONObject()
        if (pay_checkbox.isChecked){
            json.put("PayAmount",registration_amount_edittext.text.toString().toDouble())
        }else if (refund_checkbox.isChecked){
            json.put("DeductAmount",registration_amount_edittext.text.toString().toDouble())
        }
        val contractInfobuilder = ErmcpMI1.ContractOperateApplyInfo.newBuilder()

        contractInfobuilder.relatedID = data.spotcontractid?.toLong() ?: 0// 现货合同ID
        contractInfobuilder.operateApplyType = 3//款项
        contractInfobuilder.detailJson = ByteString.copyFrom(json.toString().toByteArray())

        return contractInfobuilder.build()
    }

    /**
     * 提交信息前的效验
     * @return Boolean
     */
    private fun checkUpdate(): Boolean{
        if (pay_checkbox.isChecked.not() && refund_checkbox.isChecked.not()){
            ToastUtils.showLong("请选择款项类型!")
            return false
        }

        if (registration_amount_edittext.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入登记金额!")
            return false
        }

        return true
    }

}