class TelemetryPanel(Window):
    __guid__ = 'form.TelemetryPanel'
    default_caption = 'Telemetry Panel'

    def ApplyAttributes(self, attributes):
        Window.ApplyAttributes(self, attributes)
        if hasattr(self, 'SetTopparentHeight'):
            self.SetTopparentHeight(0)
            self.container = Container(parent=self.sr.main,
                                       align=uiconst.TOALL)
        else:
            self.container = Container(parent=self.sr.content,
                                       align=uiconst.TOALL)
        self.optionsContainer = Container(parent=self.container,
                                          align=uiconst.TOTOP,
                                          height=32)
        self.cppCaptureChk = Checkbox(
            parent=self.optionsContainer,
            text='C++ capture',
            checked=blue.statistics.isCppCaptureEnabled,
            callback=self._OnCppCaptureChk,
            align=uiconst.TOTOP)
        from carbonui.primitives.gridcontainer import GridContainer
        self.buttonContainer = GridContainer(parent=self.container,
                                             align=uiconst.TOALL,
                                             columns=2,
                                             rows=2)
        self.startBtn = Button(parent=self.buttonContainer,
                               align=uiconst.TOALL,
                               label='Start',
                               func=self._Start)
        self.stopBtn = Button(parent=self.buttonContainer,
                              align=uiconst.TOALL,
                              label='Stop',
                              func=self._Stop)
        self.pauseBtn = Button(parent=self.buttonContainer,
                               align=uiconst.TOALL,
                               label='Pause',
                               func=self._Pause)
        self.resumeBtn = Button(parent=self.buttonContainer,
                                align=uiconst.TOALL,
                                label='Resume',
                                func=self._Resume)
        uthread.new(self._CheckStatus)

    def _OnCppCaptureChk(self, checkbox):
        blue.statistics.isCppCaptureEnabled = checkbox.GetValue()

    def _Start(self, args):
        print 'Starting Telemetry'
        blue.statistics.StartTelemetry('localhost')

    def _Stop(self, args):
        print 'Stopping Telemetry'
        blue.statistics.StopTelemetry()

    def _Pause(self, args):
        print 'Pausing Telemetry'
        blue.statistics.PauseTelemetry()

    def _Resume(self, args):
        print 'Resuming Telemetry'
        blue.statistics.ResumeTelemetry()

    def _CheckStatus(self):
        while not self.destroyed:
            self.cppCaptureChk.SetChecked(blue.statistics.isCppCaptureEnabled,
                                          report=False)
            if blue.statistics.isTelemetryConnected:
                self.startBtn.Disable()
                self.stopBtn.Enable()
                if blue.statistics.isTelemetryPaused:
                    self.pauseBtn.Disable()
                    self.resumeBtn.Enable()
                else:
                    self.pauseBtn.Enable()
                    self.resumeBtn.Disable()
            else:
                self.startBtn.Enable()
                self.stopBtn.Disable()
                self.pauseBtn.Disable()
                self.resumeBtn.Disable()
            blue.synchro.SleepWallclock(500)
class CameraDebugWindow(Window):
    """ Window for debug camera control and feedback """
    __guid__ = 'uicls.CameraDebugWindow'
    default_windowID = 'CameraDebugWindow'
    default_width = 500
    default_height = 200

    def ApplyAttributes(self, attributes):
        super(CameraDebugWindow, self).ApplyAttributes(attributes)
        self.SetMinSize([self.default_width, self.default_height])
        self.SetCaption('Camera Debug Window')
        self.sr.content.padding = (5, 16, 5, 5)
        self.debugSelectionClient = sm.GetService('debugSelectionClient')
        self.cameraDebugClient = sm.GetService('cameraDebugClient')
        self.cameraClient = sm.GetService('cameraClient')
        self.cameraStack = Label(parent=self.sr.content, align=uiconst.TOPLEFT, text=self.PrintCameraStack())
        buttonContainer = Container(parent=self.sr.content, align=uiconst.TOBOTTOM, height=70, padding=(150, 0, 150, 0))
        self.toggleCamera = Button(parent=buttonContainer, align=uiconst.TOBOTTOM, label='Activate Normal Camera', func=self.OnToggleDebugCamera)
        self.toggleCamUpdate = Button(parent=buttonContainer, align=uiconst.TOBOTTOM, label='Toggle Camera Update', func=self.OnToggleDebugCameraUpdate, hint='Toggle between updating the debug, and normal camera.')
        self.showCamCheckBox = Checkbox(parent=buttonContainer, aligh=uiconst.TOBOTTOM, text='Show Normal Camera', checked=False, callback=self.OnSnowNormalCameraCheckbox)
        import cameras
        self.cameraClient.AddSharedCamera('Debug Camera', cameras.DebugCamera())
        if type(self.cameraClient.GetActiveCamera()) is cameras.DebugCamera:
            self.DisableDebugCamera()
        self.EnableDebugCamera()
        sm.GetService('navigation').hasControl = False

    def OnSnowNormalCameraCheckbox(self, checkBox):
        debugCam = self.cameraClient.GetSharedCamera('Debug Camera')
        debugCam.SetShowNormalCamera(checkBox.GetValue())

    def OnToggleDebugCamera(self, *args):
        activeCam = self.cameraClient.GetActiveCamera()
        debugCam = self.cameraClient.GetSharedCamera('Debug Camera')
        if activeCam is debugCam:
            self.DisableDebugCamera()
        else:
            self.EnableDebugCamera()

    def EnableDebugCamera(self):
        debugCamera = self.cameraClient.GetSharedCamera('Debug Camera')
        entity = self.debugSelectionClient.GetSelectedEntity()
        if entity is None:
            self.debugSelectionClient.SelectPlayer()
            entity = self.debugSelectionClient.GetSelectedEntity()
        if entity is not None:
            entPos = entity.position.position
            position = (entPos[0], entPos[1] + DEFAULT_Y_OFFSET, entPos[2])
            debugCamera.SetPointOfInterest(position)
            debugCamera.distance = 0.0
            if debugCamera not in self.cameraClient.GetCameraStack():
                self.cameraClient.PushActiveCamera(debugCamera)
        self.cameraStack.SetText(self.PrintCameraStack())
        self.toggleCamera.SetLabel('Activate Normal Camera')
        self.toggleCamUpdate.Enable()
        self.showCamCheckBox.Show()
        sm.GetService('navigation').hasControl = debugCamera.IsControlEnabled()
        debugCamera.SetShowNormalCamera(self.showCamCheckBox.GetValue())

    def DisableDebugCamera(self):
        self.cameraClient.PopActiveCamera()
        self.cameraStack.SetText(self.PrintCameraStack())
        self.toggleCamera.SetLabel('Activate Debug Camera')
        self.toggleCamUpdate.Disable()
        self.showCamCheckBox.Hide()
        sm.GetService('navigation').hasControl = True
        debugCam = self.cameraClient.GetSharedCamera('Debug Camera')
        debugCam.SetShowNormalCamera(False)

    def OnToggleDebugCameraUpdate(self, *args):
        self.cameraDebugClient.ToggleDebugCameraUpdate()

    def PrintCameraStack(self):
        """
        Print out each camera in the stack along with all of their behaviors.
        """
        stack = self.cameraClient.GetCameraStack()
        stackStr = ''
        for i, cam in enumerate(stack):
            stackStr += str(i) + ': ' + str(cam) + '\n'
            for behavior in cam.cameraBehaviors:
                stackStr += '\t' + str(behavior) + '\n'

        stackStr = stackStr.replace('<', '').replace('>', '')
        return stackStr

    def Close(self, *args, **kwds):
        import cameras
        if type(self.cameraClient.GetActiveCamera()) is cameras.DebugCamera:
            self.DisableDebugCamera()
        Window.Close(self, *args, **kwds)