package com.rdiot.yx485.ui.ctrl

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.gyf.immersionbar.ktx.immersionBar
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseActivity
import com.rdiot.yx485.bean.RoomData
import com.rdiot.yx485.databinding.ActRoomCtrlMainBinding
import com.rdiot.yx485.ui.ctrl.model.RoomCtrlViewModel
import com.rdiot.yx485.ui.web.WebActivity
import com.rdiot.yx485.util.RoomUtils
import com.rdiot.yx485.util.clearGoTo

/**
 * 房间控制 容器
 * @author mR2hao
 * @date 2023/2/2
 */
class RoomCtrlMainActivity : BaseActivity<ActRoomCtrlMainBinding>() {
    override fun getLayoutId() = R.layout.act_room_ctrl_main
    lateinit var roomCtrlViewModel: RoomCtrlViewModel
    private lateinit var navController: NavController

    companion object {
        const val ROOM_DATA = "roomData"
    }

    override fun initView(savedInstanceState: Bundle?) {

        immersionBar {
            statusBarView(binding.v)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.main_bg_color)
            statusBarDarkFont(true)
        }
        navController =
            (supportFragmentManager.findFragmentById(R.id.fcv) as NavHostFragment).navController

        roomCtrlViewModel = ViewModelProvider(this)[RoomCtrlViewModel::class.java]
        val tempRoomData = intent.getSerializableExtra(ROOM_DATA) ?: finish()
        roomCtrlViewModel.roomData.postValue(tempRoomData as RoomData)
        val isMaster = RoomUtils

        if (RoomUtils.isInfrared(tempRoomData.code)) {
            finish()
            clearGoTo(
                WebActivity.getNewWebIntent(
                    this,
                    "https://app.yongxulvjian.com/#/deviceManage/${tempRoomData.recordId}/${tempRoomData.code}",
                    ""
                )
            )
        }

        if (RoomUtils.isDRAUGHT(tempRoomData.code)) {
            finish()
            clearGoTo(
                WebActivity.getNewWebIntent(
                    this,
                    "https://app.yongxulvjian.com/#/airDistributionBox/${tempRoomData.recordId}",
                    ""
                )
            )
        }

        if (RoomUtils.isWaterSystem(tempRoomData.code)) {
            finish()
            clearGoTo(
                WebActivity.getNewWebIntent(
                    this,
                    "https://app.yongxulvjian.com/#/waterSystem/${tempRoomData.recordId}",
                    ""
                )
            )
        }

        if (!RoomUtils.isMaster(tempRoomData.code)) {
            navController.navigate(R.id.subRoomCtrlFragment)
        }

    }

    override fun onBackPressed() {
        if (!navController.currentDestination.toString()
                .contains(RoomSettingFragment::class.java.name)
        ) {
            finish()
        } else {
            navController.popBackStack()
        }
    }
}