package com.quansu.heifengwuliu.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.adapter.AreaSelectAdapter
import com.quansu.heifengwuliu.base.MBRActivity
import com.quansu.heifengwuliu.databinding.ActivityAreaselectBinding
import com.quansu.heifengwuliu.model.AreaBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.quansu.heifengwuliu.vmodel.AreaSelectVModel
import com.ysnows.base.net.IResponse
import io.reactivex.Observable

//城市选择
class AreaSelectActivity : MBRActivity<AreaSelectVModel, AreaSelectAdapter, ActivityAreaselectBinding>() {

    var code="0"//默认市
    var type=1
    var name=""
    var province_code=""//省的编码
    var city_code=""//市的编码
    var area_code=""//区的编码


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

        adapter().setOnItemClickListener { adapter, view, position ->
            var bean=adapter.getItem(position) as AreaBean

            if(type==1){
                code=bean.code
                province_code=bean.code
                name=bean.name
                type++
                onRefresh()
                return@setOnItemClickListener
            }
            if(type==2){
                code=bean.code
                city_code=bean.code
                name += bean.name
                type++

                onRefresh()
                return@setOnItemClickListener
            }
            if(type==3){
                area_code=bean.code
                name += bean.name

                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("name", name)
                bundle.putString("province_code", province_code)
                bundle.putString("city_code", city_code)
                bundle.putString("area_code", area_code)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

                return@setOnItemClickListener
            }

        }
    }


    override fun title(): String {
        return getStr(R.string.chose_city)!!
    }

    override fun initAdapter(): AreaSelectAdapter = AreaSelectAdapter(vm)

    override fun api(): Observable<out IResponse<*>> {
        return NetEngine.service.getArea(code, vm?.repository()?.autoPage()!!)
    }

    override fun binding(): ActivityAreaselectBinding {
        return ActivityAreaselectBinding.inflate(layoutInflater)
    }

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

    override fun needLoadMore(): Boolean {
        return false
    }

    override fun onListReceive(res: IResponse<*>) {
        super.onListReceive(res)
        if(res.empty()&&type>1){
            val intent = Intent()
            val bundle = Bundle()
            bundle.putString("name", name)
            bundle.putString("province_code", province_code)
            bundle.putString("city_code", city_code)
            bundle.putString("area_code", area_code)
            intent.putExtras(bundle)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }





}
