package com.quansu.heifengwuliu.vmodel

import android.os.Bundle
import android.text.TextUtils
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.quansu.heifengwuliu.activity.AddressActivity
import com.quansu.heifengwuliu.activity.AreaSelectActivity
import com.quansu.heifengwuliu.activity.SourceDetailsActivity
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.fragment.OwnerMoreFragment
import com.quansu.heifengwuliu.inte.ChoiceData
import com.quansu.heifengwuliu.model.DataInfoBean
import com.quansu.heifengwuliu.model.InfoEditBean
import com.quansu.heifengwuliu.model.bean.ReceiveBean
import com.quansu.heifengwuliu.utils.PopupUtils
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.route.IGaode
import com.ysnows.base.utils.B
import com.ysnows.base.utils.UiSwitch


open class OwnerMoreVModel : BViewModel<BRepository>() {

    var unit: MutableLiveData<String> = MutableLiveData()

    var insurance: MutableLiveData<String> = MutableLiveData()

    var isPush: MutableLiveData<Int> = MutableLiveData(isPushValue)
    var isAgree: MutableLiveData<Int> = MutableLiveData(isPushValue)

    val info: MutableLiveData<DataInfoBean> = MutableLiveData<DataInfoBean>()

    //1:/装货地址 2卸货地址
    var type = 1
    var fragment : OwnerMoreFragment? =null
    var position=0



    fun initData(ss : OwnerMoreFragment){
        this.fragment=ss
    }

    companion object {

        const val isPushValue = 1

    }


    fun getDataInfo(info_id:String){

        repository().lreq(NetEngine.service.infoGet(info_id))
                .doOnNext() {
                    if (it.ok()) {
                        info.value=it.data() as DataInfoBean
                    }
                }
                .subscribe()


    }



    fun setReleaseSource(info_id:String,type_id:String,total:String,driver:String,
                         nums:String,is_public:String,is_insurance:String,
                         type:String,data:String,send:String,receive:String
    ){
        //todo:发布货源

        repository().lreq(NetEngine.service.infoEdit(info_id,type_id,total,driver,nums,is_public,is_insurance,type,
        data,send,receive))
                .doOnNext() {
                    if (it.ok()) {
                        var  bean=it.data() as InfoEditBean
                        UiSwitch.bundle(repository().context, SourceDetailsActivity::class.java, Bundle().apply {
                            putString("info_id", bean.info_id)
                            putString("type","1")
                        })

                    }
                }
                .subscribe()

    }
    //卸货列表的操作



    fun choseCity(position:Int){
        //选择城市
        this.position=position
        type=2
        UiSwitch.singleRes(fragment , AreaSelectActivity::class.java,
                MIntentAction.REQUEST_CODE_TWO)

    }
    fun chosePeoPle(position:Int){
        //地址薄
        this.position=position
        type=2
        UiSwitch.bundleRes(fragment, AddressActivity::class.java,
                B.with().putString("type", "2").ok(), MIntentAction.REQUEST_CODE_ONE)

    }

    fun choseMap(position:Int){
        this.position=position
        type=2
        goChoseMap()


    }




    fun goChoseMap(){
        AndPermission.with(repository().context)
                .runtime()
                .permission(Permission.ACCESS_COARSE_LOCATION,
                        Permission.ACCESS_FINE_LOCATION,
                        Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE,
                        Permission.READ_PHONE_STATE
                )
                .onGranted { data ->
                    CCRetrofit.instance()
                            .with(repository().context)
                            .create(IGaode::class.java)
                            .selectLocation()
                            .subscribe()

                }
                .start()

    }


    fun getContext(text:String):String{

        return if(TextUtils.isEmpty(text)) "" else text
    }



}
