"""
* Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
"""
__all__ = ["LiveModel"]

import omni.ui as ui
import omni.kit.usd.layers as layers
import omni.kit.notification_manager as nm


class LiveModel(ui.AbstractValueModel):
    def __init__(self, stage_item):
        super().__init__()
        self._stage_item = stage_item

    def destroy(self):
        self._stage_item = None

    def get_value_as_bool(self) -> bool:
        """Reimplemented get bool"""
        return self._stage_item.in_session

    def set_value(self, value: bool):
        """Reimplemented set bool"""
        live_syncing = layers.get_live_syncing(self._stage_item.usd_context)
        if self._stage_item.in_session:
            live_syncing.stop_live_session(prim_path=self._stage_item.path)
        elif self._stage_item.payrefs:
            if len(self._stage_item.payrefs) > 1:
                nm.post_notification(
                    f"Cannot join default session for prim {self._stage_item.path} as it"
                    " includes multiple references or payloads. Please join from Property"
                    " Window instead."
                )
            else:
                payref = self._stage_item.payrefs[0]
                default_session = live_syncing.find_live_session_by_name(payref, "Default")
                if not default_session:
                    default_session = live_syncing.create_live_session("Default", payref)

                if default_session:
                    live_syncing.join_live_session(default_session, self._stage_item.path)
