package cn.muchinfo.rma.view.base.future.costs

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AccountData
import cn.muchinfo.rma.global.data.AccountShowData
import cn.muchinfo.rma.global.toPercentage
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.autoWidget.textColorInt
import cn.muchinfo.rma.view.autoWidget.textSizeAuto
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.base.future.deals.DealData
import cn.muchinfo.rma.view.base.future.trade.itemView
import cn.muchinfo.rma.view.base.home.contract.emptyView
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.UI

/**
 * 资金信息
 */
class CostInfoFragment(type: String?) : BaseFragment<CostViewModel>() {
    private var type: String? = type

    companion object {
        fun getInstance(type: String?) = CostInfoFragment(type)
    }

    private var _view: View? = null
    private var idCostInfoList: RecyclerView? = null
    private var accountData : AccountData? = null
    var currencyName : TextView? = null

    val userAccountData : MutableLiveData<AccountShowData> = MutableLiveData()

    fun initData(){
        accountData = GlobalDataCollection.instance?.accountData
        val showAccountData = AccountShowData()
//        showAccountData.setNormalData()
        userAccountData.postValue(showAccountData)
        if (currencyName != null){
            //暂时写死人民币
//            currencyName?.text = accountData?.currencyid.toString()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return UI {
            initData()
            verticalLayout {
                scrollView {
                    verticalLayout {
                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "币种"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                currencyName = this
                                text = "RMB"
//                                text = accountData?.currencyid.toString()
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "当前权益"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = accountData?.currentbalance?.plus(it?.keep_watch_profit_and_loss?.toDouble() ?: 0.0).toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "可用金额"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = accountData?.currentbalance?.minus(accountData?.usedmargin ?: 0.0)?.minus(accountData?.freezecharge ?: 0.0)?.minus(accountData?.freezemargin ?: 0.0)?.minus(accountData?.otherfreezemargin ?: 0.0)?.toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "占用金额"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                /** 直接去占用保证金 **/
                                userAccountData.bindOptional(context){
                                    text = accountData?.usedmargin.toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()


                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "冻结金额"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = accountData?.freezemargin?.plus(accountData?.freezecharge ?: 0.0).toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()


                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "持仓盈亏"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                 userAccountData.bindOptional(context){
                                    text = it?.close_watch_profit_and_loss
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "平仓盈亏"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = it?.keep_watch_profit_and_loss
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

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

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = accountData?.inamount.toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

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

                            emptyView()

                            textView {
                                userAccountData.bindOptional(context){
                                    text = accountData?.outamount.toString()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "资金使用率"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(36)
                            }

                            emptyView()

                            textView {
                               userAccountData.bindOptional(context){
                                   val canuse = accountData?.currentbalance?.minus(accountData?.usedmargin ?: 0.0)?.minus(accountData?.freezecharge ?: 0.0)?.minus(accountData?.freezemargin ?: 0.0)?.minus(accountData?.otherfreezemargin ?: 0.0)
                                   val all = accountData?.currentbalance?.plus(it?.keep_watch_profit_and_loss?.toDouble() ?: 0.0)
                                   text = (canuse?.div(all ?: 1.0) ?: "").toString().toPercentage()
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent){
                                marginEnd = autoSize(36)
                            }
                        }.lparams(matchParent, autoSize(90))
                        itemView()

                    }
                }
            }
        }.view
    }
}