package com.sybotan.android.demo.activities.poc

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.ListView
import com.sybotan.android.demo.R
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.adapter.DeviceTypeAdapter
import com.sybotan.android.demo.entitiies.DeviceType
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.viewmodel.DeviceVM

class DeviceTypeActivity : BaseActivity() {
    private lateinit var typeLv: ListView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_device_type)
        typeLv = findViewById(R.id.typeLv)

        val backIv = findViewById<ImageView>(R.id.backIv)
        backIv.setOnClickListener {
            finish()
        }

        val saveBtn = findViewById<Button>(R.id.saveBtn)
        saveBtn.setOnClickListener {
            val intent = Intent()
            intent.putExtra("type", mType)
            setResult(DeviceActivity.RES_CODE_DEVICE, intent)
            finish()
        }
        initViewModel()
    }


    var mType: DeviceType? = null

    private fun initViewModel() {
        val deviceVM = DeviceVM(BaseViewModelInterface { name, params ->
            val types = params as List<DeviceType>
            val adapter = DeviceTypeAdapter(types, this@DeviceTypeActivity) {
                mType = it
            }
            typeLv.adapter = adapter
        }, this)

        deviceVM.getDeviceType()
    }
}