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

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
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 com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.textChangedListener
import org.json.JSONObject

/**
 * 点价登记
 */
class MidpointsRegistrationActivity : BaseActivity<RegistrationViewModel>() {

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

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

    /**
     * 输入的点价价格
     */
    lateinit var price_edittext : EditText

    /** 输入的点价数量 ***/
    lateinit var qty_edittext : EditText

    /** 点价金额 **/
    lateinit var amountText : TextView

    /** 商品价格 **/
    lateinit var goodsPrice : TextView

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

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            val margingPadding = autoSize(47)
            background = resources.getDrawable(R.color.main_hit_bg_color)
            dialog.bindTaskStatus(context,viewModel.loadingDialogStatus)
            //页面标题
            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.qty
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = data.enumdicname
                                            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(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 + "元/" + data.enumdicname
                                            textSizeAuto = 29
                                            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 = 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
                                            maxLines = 1
                                            maxEms = 10
                                            ellipsize = TextUtils.TruncateAt.END
                                        }.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.goodscode
                                            textSizeAuto = 29
                                            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.deliverygoodsname
                                            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 + data.enumdicname
                                            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.pricedavg + "元/" + data.enumdicname
                                            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(25)

                                bottomMargin = autoSize(40)
                            }
                        }.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 = "未定价量=合同量-已定价量",v = this)
                                    }
                                    imageResource = R.mipmap.rma_zhushi
                                }.lparams(autoSize(36), autoSize(36)){
                                    marginStart = autoSize(29)
                                }
                            }.lparams(matchParent, autoSize(60)){
                                topMargin = autoSize(36)
                            }

                            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 {
                                        price_edittext = this
                                        textChangedListener {
                                            afterTextChanged {
                                                if (it.toString().isNotEmpty()){
                                                    goodsPrice.text = it.toString().toDouble().plus(data.pricemove?.toDouble() ?: 0.0).toString()
                                                    if (qty_edittext.text.toString().isNotEmpty()){
                                                        amountText.text = goodsPrice.text.toString().toDouble().times(qty_edittext.text.toString().toDouble()).toString() + "元"
                                                    }
                                                }
                                            }
                                        }
                                        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 = "元/" + data.enumdicname
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }
                            }.lparams(matchParent, autoSize(80)){
                                topMargin = autoSize(20)
                            }

                            linearLayout {
                                textView {
                                    text = "升贴水："
                                    textColorInt = R.color.text_hit_color
                                    textSizeAuto = 29
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(240)
                                }

                                textView {
                                    text = data.pricemove
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_blue_color
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(15)
                                }

                                textView {
                                    text = "商品价格："
                                    textColorInt = R.color.text_hit_color
                                    textSizeAuto = 29
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(36)
                                }

                                textView {
                                    goodsPrice = this
                                    text = data.pricemove
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_star_color
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(15)
                                }
                            }.lparams(matchParent, wrapContent){
                                topMargin = autoSize(10)
                            }

                            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 {
                                        qty_edittext = this
                                        textChangedListener {
                                            afterTextChanged {
                                                if (it.toString().isNotEmpty()){
                                                    if (price_edittext.text.toString().isNotEmpty()){
                                                        amountText.text = goodsPrice.text.toString().toDouble().times(qty_edittext.text.toString().toDouble()).toString() + "元"
                                                    }
                                                }
                                            }
                                        }
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "0.00"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

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

                                textView {
                                    text = data.enumdicname
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }
                            }.lparams(matchParent, autoSize(80)){
                                topMargin = autoSize(20)
                            }

                            linearLayout {
                                textView {
                                    text = "未定价量："
                                    textColorInt = R.color.text_hit_color
                                    textSizeAuto = 29
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(240)
                                }

                                textView {
                                    text = data.unpricedqty
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_blue_color
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(15)
                                }

                                emptyView()

                                textView {
                                    text = "全部登记"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_green_color
                                }.lparams(wrapContent, wrapContent){
                                    marginEnd = autoSize(180)
                                }
                            }.lparams(matchParent, wrapContent){
                                topMargin = autoSize(10)
                            }

                            linearLayout {
                                textView {
                                    text = "点价金额"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(36)
                                }

                                textView {
                                    amountText = this
                                    text = "0元"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(84)
                                }
                            }.lparams(matchParent, wrapContent){
                                topMargin = autoSize(30)
                                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

                        onThrottleFirstClick {
                            if (checkUpdate().not()) {
                                return@onThrottleFirstClick
                            }
                            viewModel.orderPerationContract(info = getPerationContractInfo(price_edittext.text.toString(),qty_edittext.text.toString()),operateType = 1){
                                finish()
                            }
                        }
                        textView {
                            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
     * @param price String 价格
     * @param qty String 数量   价格和数量都要用double类型，否则会报错
     * @return ErmcpMI1.ContractOperateApplyInfo
     */
    private fun getPerationContractInfo(price : String,qty : String) : ErmcpMI1.ContractOperateApplyInfo {
        val json = JSONObject()
        json.put("PricedPrice",price.toDouble())
        json.put("PricedQty",qty.toDouble())
        val contractInfobuilder = ErmcpMI1.ContractOperateApplyInfo.newBuilder()
        contractInfobuilder.relatedID = data.spotcontractid?.toLong() ?: 0
        contractInfobuilder.operateApplyType = 1
        contractInfobuilder.detailJson = ByteString.copyFrom(json.toString().toByteArray())

        return contractInfobuilder.build()
    }

    /**
     * 提交数据
     * @return Boolean
     */
    private fun checkUpdate(): Boolean {
        if (price_edittext.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入点价价格")
            return false
        }

        if (qty_edittext.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入点价数量")
            return false
        }

        return true
    }
}