package com.quansu.heifengwuliu.vmodel


import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.quansu.heifengwuliu.BR
import com.quansu.heifengwuliu.activity.AddressAddActivity
import com.quansu.heifengwuliu.activity.AreaSelectActivity
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.model.AddressDetailsBean
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.UiSwitch

open class AddressAddVModel : BViewModel<BRepository>() {

    var checked: MutableLiveData<Int> = MutableLiveData()
    var cityname: MutableLiveData<String> = MutableLiveData()

    var lat :String= ""
    var lng :String= ""
    var province = ""
    var city = ""
    var area = ""
    var type = "1"//默认发货
    private var addr_id="0"////默认添加

    init {
        checked.value = 1//默认选中

    }
    fun inData(type: String){
        this.type=type
    }

    fun setMapValue(lat_data: String, lng_data: String, position: String) {
        lat = lat_data
        lng = lng_data
        address = position
    }

    fun setCityValue(province_code: String, city_code: String, area_code: String, name: String) {
        province = province_code
        city = city_code
        area = area_code
        cityname.value = name

    }

    fun setCityMapValue( area_code: String, name: String) {

        area = area_code
        cityname.value = name
        //371312区   370000省  371300市
        province = area.substring(0,2)+"0000"
        city = area.substring(0,4)+"00"


    }


    @Bindable
    open var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    open var phone: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }

    @Bindable
    open var address: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.address)
        }


    fun choseMap() {
        //地图选点
        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 choseCity() {
        //选择省市区
        UiSwitch.singleRes(repository().context as AddressAddActivity, AreaSelectActivity::class.java,
                MIntentAction.REQUEST_CODE_TWO)
    }


    fun setSave() {
        //保存数据
        Log.e("-shy-", "name=: " +name)
        Log.e("-shy-", "phone=: " +phone)


        if (name.isNullOrEmpty()) {
            toast("请输入联系人")
            return
        }
        if (phone.isNullOrEmpty()) {

            toast("请输入手机号码")
            return
        }
        if (TextUtils.isEmpty(province)) {
            toast("请选择地区")
            return
        }
        if (address.isNullOrEmpty()) {
            toast("请输入详细地址")
            return
        }

        repository().lreq(NetEngine.service.addressEdit(addr_id, lat, lng, province, city, area, address,
                name, phone, type, checked.value.toString()))
                .doOnNext() { res ->
                    if (res.ok(true)) {
                        val intent = Intent()
                        var con = repository().context as Activity
                        con.setResult(Activity.RESULT_OK, intent)
                        con.finish()
                    }
                }
                .subscribe()



    }


    fun choseDefault() {
        //选择默认
        if (checked.value == 1) checked.value = 0 else checked.value = 1

    }


    //获取地址详情
    fun getAddressInfo(addrid: String) {


        addr_id=addrid


        repository().lreq(NetEngine.service.getAddress(addr_id))
                .doOnNext() {
                    if (it.ok()) {
                        var info=it.data() as AddressDetailsBean
                        //设置值
                        name=info.name
                        phone=info.mobile
                        address=info.address
                        lat= info.lat!!
                        lng=info.lng!!
                        province=info.province!!.code!!
                        city=info.city!!.code!!
                        area=info.area!!.code!!
                        type=info.type!!
                        cityname.value=info.province!!.name+info.city!!.name!!+info.area!!.name!!
                        checked.value=info.is_default

                    }
                }
                .subscribe()
    }

    fun setAddressDel(addr_id: String) {
        //删除

        QMUIDialog.MessageDialogBuilder(repository().context)
                .setTitle("提示")
                .setMessage("确定要删除该地址么")
                .addAction("取消",QMUIDialogAction.ActionListener(){
                    dialog, index ->
                    dialog.dismiss()
                })
                .addAction("确定",QMUIDialogAction.ActionListener(){
                    dialog, index ->
                    setSureDel(addr_id)
                    dialog.dismiss()
                })
                .show()

    }


    fun setSureDel(addr_id: String){

        repository().lreq(NetEngine.service.setAddressDel(addr_id))
                .doOnNext() {
                    if (it.ok()) {
                        val intent = Intent()
                        var con = repository().context as Activity
                        con.setResult(Activity.RESULT_OK, intent)
                        con.finish()
                    }
                }
                .subscribe()

    }


}
