package com.ray650128.gstreamer_demo_app.ui.mainScreen

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.ray650128.gstreamer_demo_app.PreferenceUtil
import com.ray650128.gstreamer_demo_app.model.Device

class MainViewModel: ViewModel() {

    private val uriList: List<Device> by lazy {
        listOf(
            Device(
                deviceName = "192.168.0.77",
                ip = "192.168.0.77",
                rtspPort = "554",
                account = "admin",
                password = "admin",
                stream1 = "/media/video1",
                stream2 = "/media/video2",
            ),
            /*Device(
                deviceName = "192.168.0.73",
                ip = "192.168.0.73",
                rtspPort = "554",
                account = "admin",
                password = "hs22601576",
                stream1 = "/media/video1",
                stream2 = "/media/video2",
            ),*/
            /*Device(
                deviceName = "192.168.0.79",
                ip = "192.168.0.79",
                rtspPort = "554",
                account = "admin",
                password = "1q2w3e4r!",
                stream1 = "/media/video1",
                stream2 = "/media/video2",
            ),*/
            Device(
                deviceName = "192.168.0.88",
                ip = "211.23.78.226",
                rtspPort = "8588",
                account = "admin",
                password = "1q2w3e4r~",
                stream1 = "/media/video1",
                stream2 = "/media/video2",
            ),
            Device(
                deviceName = "192.168.0.74",
                ip = "211.23.78.226",
                rtspPort = "8574",
                account = "admin",
                password = "admin",
                stream1 = "/v01",
                stream2 = "/v02",
            ),
            Device(
                deviceName = "192.168.0.75",
                ip = "211.23.78.226",
                rtspPort = "8575",
                account = "admin",
                password = "admin",
                stream1 = "/v01",
                stream2 = "/v02",
            ),
            /*Device(
                deviceName = "192.168.0.76",
                ip = "211.23.78.226",
                rtspPort = "8576",
                account = "admin",
                password = "123456",
                stream1 = "/profile1",
                stream2 = "/profile2",
            ),*/
            Device(
                deviceName = "192.168.0.82",
                ip = "192.168.0.82",
                rtspPort = "554",
                account = "admin",
                password = "123456",
                stream1 = "/profile1",
                stream2 = "/profile2",
            ),
            Device(
                deviceName = "192.168.0.84",
                ip = "192.168.0.84",
                rtspPort = "554",
                account = "admin",
                password = "123456",
                stream1 = "/profile1",
                stream2 = "/profile2",
            ),
            /*Device(
                deviceName = "192.168.0.95",
                ip = "192.168.0.95",
                rtspPort = "554",
                account = "admin",
                password = "123456",
                stream1 = "/profile1",
                stream2 = "/profile2",
            )*/
        )
    }

    val splitMode: MutableLiveData<Int> by lazy { MutableLiveData<Int>() }

    val cameraList: MediatorLiveData<List<List<Device>>> by lazy {
        MediatorLiveData<List<List<Device>>>().apply {
            addSource(splitMode) {
                postValue(updateCameraList(uriList))
            }
        }
    }

    init {
        splitMode.postValue(PreferenceUtil.lastSplitMode)
    }

    fun setSplitMode(mode: Int) {
        PreferenceUtil.lastSplitMode = mode
        splitMode.postValue(PreferenceUtil.lastSplitMode)
    }

    private fun updateCameraList(dbData: List<Device>?): List<List<Device>>? {
        if (dbData.isNullOrEmpty()) return null
        val tmpData = ArrayList<List<Device>>()
        for (index in uriList.indices step (PreferenceUtil.lastSplitMode)) {
            if (index == uriList.size) break
            val tmpSubData = ArrayList<Device>()
            for (subIndex in 0 until (PreferenceUtil.lastSplitMode)) {
                val dataIndex = index + subIndex
                if (dataIndex !in uriList.indices) break
                tmpSubData.add(uriList[dataIndex])
            }
            tmpData.add(tmpSubData)
        }
        return tmpData
    }
}