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

import android.os.Bundle
import android.view.Gravity
import android.view.View
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.WrStandardData
import cn.muchinfo.rma.global.toPercentage
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.viewItemUI
import org.jetbrains.anko.*

/**
 * 现货品种详情
 */
class SpotVarietiesDetailsActivity : BaseActivity<CommodityInformationViewModel>(){

    /**
     * 数据
     */
    val data: WrStandardData by lazy {
        intent.getParcelableExtra<WrStandardData>("data") as WrStandardData
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            viewModel.queryWrStandardDetails(data.wrstandardid ?: "")
            //头部标题
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "现货品种详情"
                }
            }

            scrollView {
                verticalLayout {
                    linearLayout {
                        gravity = Gravity.CENTER_VERTICAL
//                        rma_yellow_text_color
                        background = resources.getDrawable(R.color.rma_pass_status)//rma_pass_status已通过背景色

                        imageView {
                            if (data.isvalid == "1"){
                                imageResource = R.mipmap.rma_pass_status
                            }else{
                                imageResource = R.mipmap.rma_wait_status
                            }
                        }.lparams(autoSize(38), autoSize(38)) {
                            marginStart = autoSize(37)
                        }
                        textView {
                            if (data.isvalid == "1"){
                                text = "正常"
                            }else{
                                text = "无效"
                            }
                            textSizeAuto = 31
                            textColorInt = R.color.rma_yellow_text_color
                        }.lparams(wrapContent, wrapContent) {
                            marginStart = autoSize(20)
                        }
                    }.lparams(matchParent, autoSize(60))

                    contentItem(itemTile = "现货品种名称",itemContent = data.wrstandardname ?: "")

                    viewItemUI()

                    contentItem(itemTile = "现货品种代码",itemContent = data.wrstandardcode ?: "")

                    viewItemUI()

                    contentItem(itemTile = "单位",itemContent = data.enumdicname ?: "")

                    viewModel.wrStandardDetails.bindOptional(context){
                        it?.gtList?.forEach {
                            viewItemUI()
                            contentItem(itemTile = "型号" + viewModel.wrStandardDetails.value?.gtList?.indexOf(it)?.plus(1),itemContent = it.modelid)
                        }
                    }

                    viewModel.wrStandardDetails.bindOptional(context){
                        it?.gbList?.forEach {
                            viewItemUI()
                            contentItem(itemTile = "品牌" + viewModel.wrStandardDetails.value?.gbList?.indexOf(it)?.plus(1),itemContent = it.brandid)
                        }
                    }

                    viewModel.wrStandardDetails.bindOptional(context){
                        it?.wrsList?.forEach {
                            viewItemUI()
                            contentItem(itemTile = "套保品种" + viewModel.wrStandardDetails.value?.wrsList?.indexOf(it)?.plus(1),itemContent = it.middlegoodsid)
                            contentItem(itemTile = "套保系数" + viewModel.wrStandardDetails.value?.wrsList?.indexOf(it)?.plus(1),itemContent = it.convertratio.toPercentage())
                        }
                    }

                    if (data.remark?.isNotEmpty() == true){
                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            background = resources.getDrawable(R.color.white)
                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                textView {
                                    text = "备注"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_gray_color
                                }.lparams(wrapContent, wrapContent){
                                    marginStart = autoSize(35)
                                }
                            }.lparams(autoSize(250), autoSize(120))

                            textView {
                                text = data.remark
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(35)
                                marginEnd = autoSize(35)
                            }

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

    fun _LinearLayout.contentItem(itemTile : String,itemContent : String){
        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            background = resources.getDrawable(R.color.white)
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    text = itemTile
                    textSizeAuto = 31
                    textColorInt = R.color.rma_gray_color
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(35)
                }
            }.lparams(autoSize(250), autoSize(120))

            textView {
                text = itemContent
                textSizeAuto = 31
                textColorInt = R.color.rma_black_33
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(35)
            }

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

}