package com.quansu.heifengwuliu.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.base.MBActivity
import com.quansu.heifengwuliu.config.ConfigRx
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.databinding.ActivityAddressaddBinding
import com.quansu.heifengwuliu.vmodel.AddressAddVModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.model.PositionBean
import com.ysnows.base.route.IGaode
import com.ysnows.base.utils.UiSwitch

/**
 * 添加收货/卸货地址
 *Created by shihuiyun
 *on 2020/9/11
 */
class AddressAddActivity : MBActivity<AddressAddVModel, ActivityAddressaddBinding>() {


    var type = "1"//1发货 2卸货
    var addr_id="0"

    override fun vmClass(): Class<AddressAddVModel> {
        return AddressAddVModel::class.java
    }

    override fun __before() {
        super.__before()
        var bundle = intent.extras
        if (null != bundle)
            type = bundle.getString("type","")
           addr_id=bundle.getString("addr_id","")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        if(!TextUtils.isEmpty(type)){
           vm.inData(type)
        }
        if(!TextUtils.isEmpty(addr_id)&&addr_id!="0"){
            Log.e("-shy-", "addr_id111=: "+addr_id )

            vm.getAddressInfo(addr_id)
            titleBar()?.setRightText("删除")
            titleBar()?.tvRight!!.setOnClickListener {
                //删除
                vm.setAddressDel(addr_id)
            }
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_TWO === requestCode && resultCode == Activity.RESULT_OK) {
            val bundle = data!!.extras
            if (null != bundle) {

                vm.setCityValue(bundle.getString("province_code"),bundle.getString("city_code"),
                        bundle.getString("area_code"),bundle.getString("name"))

            }


        }


    }


    override fun binding(): ActivityAddressaddBinding {
        return ActivityAddressaddBinding.inflate(layoutInflater)

    }

    override val isRxbus: Boolean
        get() = true

    override fun title(): String? {
        return if (type == "1") "添加发货地址" else "添加卸货地址"
    }

    @Subscribe(tags = [Tag(ConfigRx.ADD_POSITION)])
    fun getPosition(bean: PositionBean) {

//        var provincename = bean.provincename

        var name =bean.provincename+bean.cityname+bean.areaname

        vm.setCityMapValue(bean.areacode, name)

        vm.setMapValue(bean.latitude,bean.longitude,bean.position)


    }
}
