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

import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
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.global.data.HedgePlanData
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.home.contract.viewItemUI
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

/**
 * 套保
 * @property type String?
 * @property data HedgePlanData
 * @property audit_opinion_edittext EditText
 * @property dialog Dialog
 */
class HedgeDetailsActivity : BaseActivity<HedgePlanViewModel>() {

    /**
     * 详情内的操作类型 1是单独显示详情 2 可以审核通过，审核拒绝  3是撤销
     */
    private val type by lazy {
        intent.getStringExtra("type")
    }

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

    lateinit var audit_opinion_edittext: EditText

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


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            background = resources.getDrawable(R.color.segtabment_bg_color)
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "审核"
                }
            }

            frameLayout {
                scrollView {
                    verticalLayout {
                        verticalLayout {
                            background = resources.getDrawable(R.color.white)
                            linearLayout {
                                if (type == "1") {
                                    visibility = View.GONE
                                } else {
                                    visibility = View.VISIBLE
                                }
                                gravity = Gravity.CENTER_VERTICAL
                                if (data.hedgeplanstatus == "3") {
                                    background =
                                        resources.getDrawable(R.color.rma_pass_status)//rma_pass_status已通过背景色
                                } else {
                                    background =
                                        resources.getDrawable(R.color.rma_details_status)//rma_pass_status已通过背景色
                                }
                                imageView {
                                    if (data.hedgeplanstatus == "3") {
                                        imageResource = R.mipmap.rma_pass_status
                                    } else {
                                        imageResource = R.mipmap.rma_wait_status
                                    }
                                }.lparams(autoSize(38), autoSize(38)) {
                                    marginStart = autoSize(37)
                                }
                                textView {
                                    text =
                                        ViewEnumUtils.getContractStatus(data.hedgeplanstatus ?: "")
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_yellow_text_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(20)
                                }
                            }.lparams(matchParent, autoSize(60))

                            creatHedgeItemView(
                                titleString = "计划类型",
                                content = "套保" + ViewEnumUtils.getContractType(
                                    data.contracttype ?: ""
                                ) + "计划"
                            )

                            viewItemUI()

                            creatHedgeItemView(
                                titleString = "计划名称",
                                content = data.wrstandardname + "#套保" + ViewEnumUtils.getContractType(
                                    data.contracttype ?: ""
                                ) + "计划"
                            )

                            viewItemUI()

                            creatHedgeItemView(
                                titleString = "现货品种",
                                content = data.wrstandardname ?: ""
                            )


                            viewItemUI()

                            creatHedgeItemView(titleString = "标仓系数", content = "1")

                            viewItemUI()

                            // TODO 缺一个计划量单位
                            creatHedgeItemView(
                                titleString = "计  划  量",
                                content = data.planqty ?: ""
                            )

                            viewItemUI()

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

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

                        linearLayout {
                            if (type == "2") {
                                visibility = View.VISIBLE
                            } else {
                                visibility = View.GONE
                            }
                            background = resources.getDrawable(R.color.white)

                            textView {
                                text = "审核意见"
                                textColorInt = R.color.rma_black_33
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            editText {
                                gravity = Gravity.TOP
                                audit_opinion_edittext = this
                                hint = "请输入审核意见"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(260)) {
                                marginStart = autoSize(59)
                            }
                        }.lparams(matchParent, autoSize(260)) {
                            topMargin = autoSize(20)
                            bottomMargin = autoSize(200)
                        }
                    }
                }

                linearLayout {
                    if (type == "1") {
                        visibility = View.GONE
                    } else {
                        visibility = View.VISIBLE
                    }
                    background = resources.getDrawable(R.color.white)
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        onThrottleFirstClick {
                            viewModel.orderHedge(
                                hedgePlanID = data.hedgeplanid?.toLong() ?: 0,
                                operateType = 4,
                                remark = audit_opinion_edittext.text.toString()
                            ) {
                                finish()
                            }
                        }
                        if (type == "2") {
                            visibility = View.VISIBLE
                        } else {
                            visibility = View.GONE
                        }
                        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 {
                            viewModel.orderHedge(
                                hedgePlanID = data.hedgeplanid?.toLong() ?: 0,
                                operateType = 3,
                                remark = audit_opinion_edittext.text.toString()
                            ) {
                                finish()
                            }
                        }
                        if (type == "2") {
                            visibility = View.VISIBLE
                        } else {
                            visibility = View.GONE
                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        text = "审核通过"
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(autoSize(419), autoSize(119)) {
                        marginStart = autoSize(13)
                    }

                    textView {

                        onThrottleFirstClick {
                            viewModel.orderHedge(
                                hedgePlanID = data.hedgeplanid?.toLong() ?: 0,
                                operateType = 5,
                                remark = audit_opinion_edittext.text.toString()
                            ) {
                                finish()
                            }
                        }

                        visibility = if (type == "3") {
                            View.VISIBLE
                        } else {
                            View.GONE
                        }
                        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)
        }
    }

}

fun _LinearLayout.creatHedgeItemView(titleString: String, content: String) {
    linearLayout {
        gravity = Gravity.CENTER_VERTICAL
        textView {
            text = titleString
            textSizeAuto = 31
            textColorInt = R.color.p_normal_summary_txt_color
        }.lparams(wrapContent, wrapContent) {
            marginStart = autoSize(59)
        }

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