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

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AvaildGoodsGroupData
import cn.muchinfo.rma.global.data.GGConvertConfigData
import cn.muchinfo.rma.global.data.MiddleGoodsData
import cn.muchinfo.rma.global.isBlankString
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.commodity.viewholder.HedgeFutureVarietyViewholder
import cn.muchinfo.rma.view.base.home.contract.baseInformationView
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.adapter.BaseAdapter
import mtp.polymer.com.autowidget.dialog.creatHedgeBottomSheetDialog
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

class AddHedgeGoodsActivity : BaseActivity<CommodityInformationViewModel>() {

    /**
     * 页面的操作类型 1 新增 2修改 3停用 4详情 5恢复
     */
    val type by lazy {
        intent.getStringExtra("type")
    }

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

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

    /** 套保比例 **/
    lateinit var hedge_ratio_edtittext: EditText

    //品牌适配
    private val hedgeFutureAdapter: BaseAdapter<GGConvertConfigData, HedgeFutureVarietyViewholder> =
        BaseAdapter { _, _ -> HedgeFutureVarietyViewholder(this, viewModel) }

    /**
     * 选择的套保品种
     */
    val selectAvalidGoodsData: MutableLiveData<AvaildGoodsGroupData> = MutableLiveData()

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

    //用于修改，停用，恢复，详情的初始化数据
    fun init() {
        if (type == "2" || type == "3" || type == "5") {
            viewModel.queryMiddleGoodsDetail(data.middlegoodsid ?: "") {
                selectAvalidGoodsData.postValue(this)
                viewModel.queryGGConvertConfig(this.goodsgroupid ?: "")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            init()
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            viewModel.queryAvalidGoods()
            background = resources.getDrawable(R.color.segtabment_bg_color)
            //头部标题
            topBar {
                commonLeftButton()
                commonTitle {
                    when (type) {
                        "1" -> {
                            text = "新增套保品种"
                        }
                        "2" -> {
                            text = "修改套保品种"
                        }
                        "3" -> {
                            text = "停用套保品种"
                        }
                        "5" -> {
                            text = "恢复套保品种"
                        }
                    }
                }
            }

            frameLayout {
                nestedScrollView {
                    verticalLayout {
                        baseInformationView("基本信息")
                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                if (type == "2" || type == "3" || type == "5") {
                                    return@onThrottleFirstClick
                                }
                                if (viewModel.avalidGoodsList.value.isNullOrEmpty()) {
                                    ToastUtils.showLong("套保品种请求失败")
                                    return@onThrottleFirstClick
                                }
                                creatHedgeBottomSheetDialog(
                                    "请选择套保品种名称",
                                    viewModel.avalidGoodsList.value ?: arrayListOf()
                                ) {
                                    selectAvalidGoodsData.postValue(this)
                                    viewModel.queryGGConvertConfig(this.goodsgroupid ?: "")
                                }
                            }
                            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 {
                                selectAvalidGoodsData.bindOptional(context){
                                    text = it?.goodsgroupname
                                }
                                text = "请选择套保品种名称"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(30)
                            }

                            emptyView()

                            imageView {
                                if (type == "2" || type == "3" || type == "5") {
                                    visibility = View.GONE
                                }
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "套保品种代码"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(60)
                            }

                            textView {
                                selectAvalidGoodsData.bindOptional(context) {
                                    text = it?.outergroupcode
                                }
                                text = "请选择套保品种自动填入"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(30)
                            }

                            emptyView()

                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "单位"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(60)
                            }

                            textView {
                                selectAvalidGoodsData.bindOptional(context) {
                                    text = it?.goodunitname?.isBlankString()
                                }
                                text = "请选择套保品种自动填入"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(140)
                            }

                            emptyView()

                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            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 {
                                if (type == "2" || type == "3" || type == "5") {
                                    setText(data.needhedgeratio)
                                }
                                hedge_ratio_edtittext = this
                                hint = "请输入套保比例"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                setDecimalInputType()
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(132)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(132))

                        baseInformationView("期货品种信息")

                        //期货品种信息
                        recyclerView {
                            background = resources.getDrawable(R.color.white)
                            adapter = hedgeFutureAdapter
                        }.lparams(matchParent, wrapContent)

                        viewModel.ggConvertConfigDataList.bindOptional(context) {
                            hedgeFutureAdapter.setNewData(it)
                        }

                        baseInformationView("其他信息")

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

                            editText {
                                remask_edittext = 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 {
                        onThrottleFirstClick {
                            if (checkUpdate().not()) {
                                return@onThrottleFirstClick
                            }
                            val data1 = selectAvalidGoodsData.value
                            if (type == "1") {
                                viewModel.requestErmsMiddelGoodsReq(
                                    middlegoodsname = data1?.goodsgroupname ?: "",
                                    middlegoodscode = data1?.outergroupcode ?: "",
                                    goodsunitid = data1?.goodunitid?.toInt() ?: 0,
                                    goodsgroupid = data1?.goodsgroupid?.toLong() ?: 0,
                                    remark = remask_edittext.text.toString(),
                                    areauserid = GlobalDataCollection.instance?.loginRsp?.userID?.toLong() ?: 0,
                                    needhedgeratio = hedge_ratio_edtittext.text.toString()
                                        .toDouble()
                                ) {
                                    finish()
                                }
                            } else if (type == "2") {
                                viewModel.requestErmsMiddelGoodsEditReq(
                                    middlegoodsid = data.middlegoodsid?.toLong() ?: 0,
                                    needhedgeratio = hedge_ratio_edtittext.text.toString()
                                        .toDouble() ?: 0.0,
                                    goodsgroupid = data.goodsgroupid?.toLong() ?: 0,
                                    areauserid = GlobalDataCollection.instance?.loginRsp?.userID?.toLong() ?: 0,
                                    remark = remask_edittext.text.toString()
                                ) {
                                    finish()
                                }
                            } else if (type == "3") {
                                viewModel.requestErmsMiddelGoodsStopReq(middlegoodsid = data.middlegoodsid?.toLong() ?: 0,isvalid = 0){
                                    finish()
                                }
                            } else if (type == "5") {
                                viewModel.requestErmsMiddelGoodsStopReq(middlegoodsid = data.middlegoodsid?.toLong() ?: 0,isvalid = 1){
                                    finish()
                                }
                            }

                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        if (type == "1") {
                            text = "完成"
                        } else if (type == "2") {
                            text = "修改"
                        } else if (type == "3") {
                            text = "停用"
                        } else if (type == "5") {
                            text = "恢复"
                        }
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(matchParent, autoSize(119)) {
                        marginStart = autoSize(60)
                        marginEnd = autoSize(60)
                    }
                }.lparams(matchParent, autoSize(144)) {
                    gravity = Gravity.BOTTOM
                }
            }.lparams(matchParent, matchParent)
        }
    }


    private fun checkUpdate(): Boolean {
        if (selectAvalidGoodsData.value?.goodsgroupname.isNullOrEmpty()) {
            ToastUtils.showLong("请选择套保品种")
            return false
        }

        if (hedge_ratio_edtittext.text.toString().isNullOrEmpty()) {
            ToastUtils.showLong("请输入套保比例")
            return false
        }
        return true
    }
}