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

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.R
import cn.muchinfo.rma.business.hedge.HedgePlanInfo
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.MTPEnums
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.HedgePlanData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.home.contract.emptyView
import cn.muchinfo.rma.view.base.home.contract.viewItemUI
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.dialog.SelectData
import mtp.polymer.com.autowidget.dialog.creatBottomSheetDialog
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.TaskUiModel
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

/**
 * 新增套保计划
 */
class AddHedgePlanActivity : BaseActivity<HedgePlanViewModel>() {

    /**
     * type 为1是新增  为2是修改
     */
    val type by lazy {
        intent.getStringExtra("type")
    }

    val data by lazy {
        intent.getParcelableExtra<HedgePlanData>("data") as HedgePlanData
    }

    /**
     * 输入的计划名称
     */
    lateinit var planNameEdittext: EditText

    /**
     * 标仓系数输入值
     */
    lateinit var coefficientWarehouse: TextView

    /**
     * 计划量输入值
     */
    lateinit var amountPlanning: EditText

    /**
     * 备注
     */
    lateinit var ramask: EditText


    /**
     * 套保计划类型选择列表
     */
    val selectCotractTyleList by lazy {
        arrayListOf(
            SelectData(MTPEnums.CONTRACT_TYPE_BUY.toString(), "采购"),
            SelectData(MTPEnums.CONTRACT_TYPE_SELL.toString(), "销售")
        )
    }

    /** 选择的套保计划类型 **/
    val selectContractType: MutableLiveData<SelectData> = MutableLiveData()

    /** 选择的现货商品类型 */
    val selectWrStandardData: MutableLiveData<SelectData> = MutableLiveData()

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

    fun initData() {
        if (type == "2") {
            selectContractType.value = SelectData(
                id = data.contracttype ?: "",
                value = ViewEnumUtils.getContractType(data.contracttype ?: "")
            )
            // TODO 列表内缺少现货品种单位字段enumdicname
            selectWrStandardData.value = SelectData(
                id = data.wrstandardid ?: "",
                value = data.wrstandardname ?: "",
                deliverygoodsid = data.deliverygoodsid ?: "",
                areauserid = data.areauserid ?: "",
                enumdicname = ""
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        verticalLayout {
            viewModel.queryWrStandardList()//需要查询一次现货商品的正常数据列表
            initData()
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            background = resources.getDrawable(R.color.segtabment_bg_color)
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "新增套保计划"
                }
            }

            frameLayout {
                scrollView {
                    verticalLayout {
                        background = resources.getDrawable(R.color.white)
                        linearLayout {
                            onThrottleFirstClick {
                                creatBottomSheetDialog("请选择计划类型", selectCotractTyleList) {
                                    selectContractType.postValue(this)
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "计划类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectContractType.bindOptional(context) {
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择计划类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {

                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "计划名称"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            editText {
                                planNameEdittext = this
                                hint = "请输入计划名称"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                creatBottomSheetDialog(
                                    "请选择现货品种",
                                    viewModel.selectSpotVarietiesList(
                                        viewModel.wrStandardDataList.value ?: arrayListOf()
                                    )
                                ) {
                                    selectWrStandardData.postValue(this)

                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "现货品种"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectWrStandardData.bindOptional(context) {
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择商品名称"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))


                        linearLayout {
                            visibility = View.GONE
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "产品类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                text = "请选择现货类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {

                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "标仓系数"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

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


                        linearLayout {
                            visibility = View.GONE
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "商品型号"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                hint = "请输入商品型号"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {

                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "计  划  量"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                amountPlanning = this
                                hint = "请输入计划量"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                setDecimalInputType()
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                selectWrStandardData.bindOptional(context) {
                                    text = it?.enumdicname
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }

                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

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

                            editText {
                                ramask = this
                                hint = "请输入备注"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(260)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(136)) {
                            bottomMargin = autoSize(200)
                        }
                    }
                }

                linearLayout {
                    background = resources.getDrawable(R.color.white)
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_save_info_bg
                        text = "保存草稿"
                        textSizeAuto = 38
                        textColorInt = R.color.rma_star_color
                    }.lparams(autoSize(419), autoSize(119)) {
                        marginStart = autoSize(24)
                    }

                    textView {
                        onThrottleFirstClick {
                            if (checkUpdate().not()) {
                                return@onThrottleFirstClick
                            }
                            viewModel.orderHedge(
                                hedgePlanData = getHedgeInfo(),
                                operateType = 2,
                                remark = ramask.text.toString()
                            ) {
                                finish()
                            }
                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        text = "提交审核"
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(autoSize(419), autoSize(119)) {
                        marginStart = autoSize(13)
                    }
                }.lparams(matchParent, autoSize(144)) {
                    gravity = Gravity.BOTTOM
                }
            }.lparams(matchParent, matchParent)
        }
    }

    private fun getHedgeInfo(): HedgePlanInfo {
        val hedgePlanInfo = HedgePlanInfo()
        hedgePlanInfo.ContractType = selectContractType.value?.id?.toInt()!!//计划类型
        if (planNameEdittext.text.toString().isNotEmpty()) {
            hedgePlanInfo.HedgePlanNo = planNameEdittext.text.toString()
        }

        hedgePlanInfo.DeliveryGoodsID = selectWrStandardData.value?.deliverygoodsid?.toInt()!!
        hedgePlanInfo.WrStandardID = selectWrStandardData.value?.id?.toInt()!!
        hedgePlanInfo.AreaUserID = GlobalDataCollection.instance?.loginRsp?.userID ?: 0
        hedgePlanInfo.ProductType = 1//写死为1产品类型
        hedgePlanInfo.SpotGoodsDesc = "咳咳咳"

        if (coefficientWarehouse.text.toString().isNotEmpty()) {
            hedgePlanInfo.ConvertFactor = coefficientWarehouse.text.toString().toDouble()
        }

        hedgePlanInfo.PlanQty = amountPlanning.text.toString().toDouble()

        return hedgePlanInfo
    }

    private fun checkUpdate(): Boolean {
        if (selectContractType.value?.id.isNullOrEmpty()) {
            ToastUtils.showLong("请选择计划类型")
            return false
        }

        if (selectWrStandardData.value?.id.isNullOrEmpty()) {
            ToastUtils.showLong("请选择现货品种")
            return false
        }

        if (amountPlanning.text.toString().isNullOrEmpty()) {
            ToastUtils.showLong("请输入计划量")
            return false
        }

        return true
    }

}