package llc.arma.ble.app.ui.screen.main

import androidx.compose.runtime.Composable
import androidx.compose.ui.window.DialogProperties
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.dialog
import androidx.navigation.compose.rememberNavController
import llc.arma.ble.app.ui.screen.ble.BleListContract
import llc.arma.ble.app.ui.screen.ble.BleListScreen
import llc.arma.ble.app.ui.screen.connection.ConnectionContract
import llc.arma.ble.app.ui.screen.connection.ConnectionScreen
import llc.arma.ble.app.ui.screen.password.ChangePasswordContract
import llc.arma.ble.app.ui.screen.password.ChangePasswordScreen
import llc.arma.ble.app.ui.screen.rotation.delete.RotationDeleteContract
import llc.arma.ble.app.ui.screen.rotation.delete.RotationDeleteScreen
import llc.arma.ble.app.ui.screen.rotation.statistic.RotationStatisticContract
import llc.arma.ble.app.ui.screen.rotation.statistic.RotationStatisticScreen

@Composable
fun MainScreen() {

    val controller = rememberNavController()

    NavHost(
        navController = controller,
        startDestination = "bleList",
        builder = {

            composable(
                route = "bleList",
                content = {

                    BleListScreen(
                        onNavigationEvent = {
                            when(it){
                                is BleListContract.Effect.Navigation.NavigateToBle -> controller.navigate("connection/${it.serial}")
                            }
                        }
                    )

                }
            )

            composable(
                route = "connection/{serial}",
                content = {

                    ConnectionScreen(
                        onNavigationEvent = {
                            when(it){
                                ConnectionContract.Effect.Navigation.NavigateUp -> controller.navigateUp()
                                is ConnectionContract.Effect.Navigation.NavigateToChangePassword ->
                                    controller.navigate("change_password/${it.serial}")
                                is ConnectionContract.Effect.Navigation.NavigateToRotationsStatistic ->
                                    controller.navigate("rotation_statistic/${it.serial}")
                            }
                        }
                    )

                }
            )

            composable(
                route = "rotation_statistic/{serial}",
                content = {

                    RotationStatisticScreen {
                        when(it){
                            is RotationStatisticContract.Effect.Navigation.NavigateToDelete -> {
                                controller.navigate("rotation_delete/${it.serial}")
                            }
                            RotationStatisticContract.Effect.Navigation.NavigateUp -> {
                                controller.navigateUp()
                            }
                        }
                    }

                }
            )

            dialog(
                route = "rotation_delete/{serial}",
                dialogProperties = DialogProperties(usePlatformDefaultWidth = false),
                content = {
                    RotationDeleteScreen {
                        when(it){
                            RotationDeleteContract.Effect.Navigation.NavigateUp -> controller.navigateUp()
                        }
                    }
                }
            )

            dialog(
                route = "change_password/{serial}",
                dialogProperties = DialogProperties(usePlatformDefaultWidth = false),
                content = {
                    ChangePasswordScreen {
                        when(it){
                            is ChangePasswordContract.Effect.Navigation.NavigateUp -> controller.navigateUp()
                        }
                    }
                }
            )

        }

    )

}