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

import android.content.Intent
import android.os.Bundle
import android.widget.ExpandableListView
import com.sybotan.android.demo.R
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.activities.GraphyActivity
import com.sybotan.android.demo.adapter.ExpandAdapter
import com.sybotan.android.demo.entitiies.Build
import com.sybotan.android.demo.entitiies.Floor
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.viewmodel.BuildVM
import kotlinx.android.synthetic.main.activity_scene.*

class SpaceActivity : BaseActivity() {
    private lateinit var expandLv: ExpandableListView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_space)

        expandLv = findViewById(R.id.expandLv)
        expandLv.setGroupIndicator(null)
        expandLv.divider = null

        expandLv.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
            val floor = mBuilds[groupPosition].floor[childPosition]
            val intent = Intent(this, GraphyActivity::class.java)
            val bundle = Bundle()
            bundle.putString("buildingId", floor.buildingId)
            bundle.putString("floorId", floor.id)
            bundle.putString("floorMap", floor.infos.floorMap)
            bundle.putString("from", "space")
            intent.putExtra("floor", bundle)
            startActivity(intent)
            true
        }
        backIv.setOnClickListener {
            finish()
        }

        initViewModel()
    }

    private lateinit var mBuilds: List<Build>
    private fun initViewModel() {
        val vm = BuildVM(BaseViewModelInterface { name, params ->
            mBuilds = params as List<Build>
            val floors = sep(mBuilds)
            val adapter = ExpandAdapter(this, mBuilds, floors)
            expandLv.setAdapter(adapter)

        }, this)
        vm.getBuilds()
    }

    private fun sep(builds: List<Build>): List<List<Floor>> {
        val floors = mutableListOf<List<Floor>>()
        for (build in builds) {
            floors.add(build.floor)
        }
        return floors
    }


}


