package com.quansu.heifengwuliu.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.google.gson.Gson
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.adapter.CollectionAdapter
import com.quansu.heifengwuliu.base.MBRActivity
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.databinding.ActivityCollectionBinding
import com.quansu.heifengwuliu.model.DriverBean
import com.quansu.heifengwuliu.model.SaveDriverBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.quansu.heifengwuliu.vmodel.CollectionVModel
import com.ysnows.base.net.IResponse
import com.ysnows.base.utils.UiSwitch
import io.reactivex.Observable

class CollectionActivity : MBRActivity<CollectionVModel, CollectionAdapter, ActivityCollectionBinding>() {

    var k:String=""
    var type="1"// 0 不显示选择按钮  1显示选择按钮


    override fun __before() {
        super.__before()
        type= intent?.extras?.getString("type", "1").toString()
    }

    override fun initView(view: View?) {
        super.initView(view)
        titleBar()?.setRightText(getString(R.string.confirm))
        titleBar()?.tvRight!!.setOnClickListener {
            //确定
            var list=adapter().data as ArrayList<DriverBean>
            if(null!=list&&list.size>0){
                var choseList=ArrayList<String>()
                for(item in list){
                    if(item.checked==1){
                        choseList.add(item.sj_uid)
                    }
                }
                if(choseList.size==0){
                    toast("请选择司机")
                    return@setOnClickListener
                }

//                var driver= Gson().toJson(choseList)

                val intent = Intent()
                var bundle=Bundle()
                bundle.putSerializable("driver",choseList)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

            }

        }
    }


    override fun initCreate(savedInstanceState: Bundle?) {
        super.initCreate(savedInstanceState)
        search()
    }

    override fun listeners() {
        super.listeners()
        binding.butSure.setOnClickListener{

            UiSwitch.singleRes(this, AllCarActivity::class.java,
                    MIntentAction.REQUEST_CODE_ONE)
        }
    }


    private fun search(){
        binding.search.getIvDelete()!!.setOnClickListener {
            if (!TextUtils.isEmpty(binding.search.getEtName()!!.text.toString().trim { it <= ' ' })) {
                binding.search.getEtName()!!.setText(null)
                onRefresh()
            } else {
                toast( "已经删除搜索条件！")
            }
        }

        binding.search.getEtName()!!.setOnEditorActionListener(TextView.OnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                k = binding.search.getEtName()!!.text.toString().trim()
                onRefresh()
                return@OnEditorActionListener true
            }
            false
        })

        binding.search.getTvSearch()!!.setOnClickListener { t ->
            k = binding.search.getEtName()!!.text.toString().trim()
            onRefresh()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_ONE == requestCode && resultCode == Activity.RESULT_OK) {
            //刷新列表
            onRefresh()
        }
    }

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


    override fun initAdapter(): CollectionAdapter {
        return CollectionAdapter(type)
    }

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

    override fun title(): String? {
        return getString(R.string.my_collection)
    }
    override fun vmClass(): Class<CollectionVModel> {
        return CollectionVModel::class.java
    }
}
