module NotificationModule
    use SimulationVariables
    implicit none
contains
    subroutine Set_UnlatchLed(v)
        use CDrillingConsoleVariables
        use SimulationVariables
        !use CLatchLedNotification
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%UnlatchLed == v) return
        #endif
        data%State%notifications%UnlatchLed = v
        
        if(data%State%notifications%UnlatchLed) then
            data%EquipmentControl%DrillingConsole%UnlatchPipeLED = 1
            !call Set_LatchLed(.false.)
        else 
            data%EquipmentControl%DrillingConsole%UnlatchPipeLED = 0
        endif
        
        !**call data%State%notifications%OnUnlatchLedChange%RunAll()
    end subroutine
    
    logical function Get_UnlatchLed()
        implicit none
        Get_UnlatchLed = data%State%notifications%UnlatchLed
    end function

    subroutine Set_TongNotification(v)
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%TongNotification == v) return
        #endif
        data%State%notifications%TongNotification = v
        ! if(associated(data%State%notifications%TongNotificationPtr)) !**call data%State%notifications%TongNotificationPtr(data%State%notifications%TongNotification)
        #ifdef deb
                print*, 'data%State%notifications%TongNotification=', data%State%notifications%TongNotification
        #endif
        !**call data%State%notifications%OnTongNotificationChange%RunAll()
    end subroutine
    
    logical function Get_TongNotification()
        implicit none
        Get_TongNotification = data%State%notifications%TongNotification
    end function

    subroutine Set_TorqueWrenchLed(v)
        use CTopDrivePanelVariables
    use SimulationVariables!, only: data%EquipmentControl%TopDrivePanel%TopDriveTorqueWrenchLed
        implicit none
        integer , intent(in) :: v
        
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%TorqueWrenchLed == v) return
        #endif
        data%State%notifications%TorqueWrenchLed = v
        data%EquipmentControl%TopDrivePanel%TopDriveTorqueWrenchLed = v
        !**call data%State%notifications%OnTorqueWrenchLedChange%RunAll()
    end subroutine
    
    logical function Get_TorqueWrenchLed()
        implicit none
        Get_TorqueWrenchLed = data%State%notifications%TorqueWrenchLed
    end function

    subroutine Set_PowerLed(v)
        use CTopDrivePanelVariables
    use SimulationVariables!, only: data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerLed
        !use CLatchLedNotification
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%PowerLed == v) return
        #endif
        data%State%notifications%PowerLed = v
        
        if(data%State%notifications%PowerLed) then
            data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerLed = 1
            !call Set_LatchLed(.false.)
        else 
            data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerLed = 0
        endif
        
        !**call data%State%notifications%OnPowerLedChange%RunAll()
    end subroutine
    
    logical function Get_PowerLed()
        implicit none
        Get_PowerLed = data%State%notifications%PowerLed
    end function

    subroutine Set_IbopLed(v)
        use CTopDrivePanelVariables
    use SimulationVariables!, only: data%EquipmentControl%TopDrivePanel%TopDriveIbopLed
        use CManifolds, Only: OpenTopDriveIBop, CloseTopDriveIBop
        !use CLatchLedNotification
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%IbopLed == v) return
        #endif
        data%State%notifications%IbopLed = v
        
        if(data%State%notifications%IbopLed) then
            data%EquipmentControl%TopDrivePanel%TopDriveIbopLed = 1
            call CloseTopDriveIBop()
        else 
            data%EquipmentControl%TopDrivePanel%TopDriveIbopLed = 0
            call OpenTopDriveIBop()
        endif
        
        !**call data%State%notifications%OnIbopLedChange%RunAll()
    end subroutine
    
    logical function Get_IbopLed()
        implicit none
        Get_IbopLed = data%State%notifications%IbopLed
    end function

    subroutine Set_SwingLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: SwingLedHw => SwingLed
        implicit none
        logical , intent(in) :: v
        
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%SwingLed == v) return
        #endif
        data%State%notifications%SwingLed = v
        if(data%State%notifications%SwingLed) then
            data%EquipmentControl%DrillingConsole%SwingLed = 1
        else 
            data%EquipmentControl%DrillingConsole%SwingLed = 0
        endif
        !**call data%State%notifications%OnSwingLedChange%RunAll()
    end subroutine
    
    logical function Get_SwingLed()
        implicit none
        Get_SwingLed = data%State%notifications%SwingLed
    end function
    
    subroutine Set_SlipsNotification(v)
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%SlipsNotification == v) return
        #endif
        data%State%notifications%SlipsNotification = v
        ! if(associated(data%State%notifications%SlipsNotificationPtr)) !**call data%State%notifications%SlipsNotificationPtr(data%State%notifications%SlipsNotification)
        #ifdef deb
                print*, 'data%State%notifications%SlipsNotification=', data%State%notifications%SlipsNotification
        #endif
        !**call data%State%notifications%OnSlipsNotificationChange%RunAll()
    end subroutine
    
    logical function Get_SlipsNotification()
        implicit none
        Get_SlipsNotification = data%State%notifications%SlipsNotification
    end function

    subroutine Set_OpenSafetyValveLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: OpenSafetyValveLedHw => OpenSafetyValveLed
        use CManifolds, only: OpenSafetyValve_TopDrive, OpenSafetyValve_KellyMode, OpenSafetyValve_TripMode
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, TopDrive_DriveType, Kelly_DriveType
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%OpenSafetyValveLed == v) return
        #endif
        data%State%notifications%OpenSafetyValveLed = v
        
        if(data%State%notifications%OpenSafetyValveLed) then
            !!call OpenSafetyValve()
            if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) call OpenSafetyValve_TopDrive()            
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_OpenSafetyValveLed == 0) call OpenSafetyValve_KellyMode()
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_OpenSafetyValveLed == 1) call OpenSafetyValve_TripMode()
        endif 
        
        !**call data%State%notifications%OnOpenSafetyValveLedChange%RunAll()
    end subroutine
    
    logical function Get_OpenSafetyValveLed()
        implicit none
        Get_OpenSafetyValveLed = data%State%notifications%OpenSafetyValveLed
    end function

    subroutine Set_OpenKellyCockLed(v)
        ! use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: OpenKellyCockLedHw => OpenKellyCockLed
        use CManifolds, only: OpenKellyCock
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%OpenKellyCockLed == v) return
        #endif
        data%State%notifications%OpenKellyCockLed = v
        
        if(data%State%notifications%OpenKellyCockLed) then
            call OpenKellyCock()
        endif  
        
        ! HAS BEEN IMPLEMENTED IN CMANIFOLD
        
        !if(OpenKellyCockLed) then
        !    OpenKellyCockLedHw = 1
        !else 
        !    OpenKellyCockLedHw = 0
        !endif
        
        !**call data%State%notifications%OnOpenKellyCockLedChange%RunAll()
    end subroutine
    
    logical function Get_OpenKellyCockLed()
        implicit none
        Get_OpenKellyCockLed = data%State%notifications%OpenKellyCockLed
    end function

    subroutine Set_LatchLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: data%EquipmentControl%DrillingConsole%LatchPipeLED
        !use CUnlatchLedNotification
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%LatchLed == v) return
        #endif
        data%State%notifications%LatchLed = v
        if(data%State%notifications%LatchLed) then
            data%EquipmentControl%DrillingConsole%LatchPipeLED = 1
            !call Set_UnlatchLed(.false.)
        else 
            data%EquipmentControl%DrillingConsole%LatchPipeLED = 0
        endif
        !**call data%State%notifications%OnLatchLedChange%RunAll()
    end subroutine
    
    logical function Get_LatchLed()
        implicit none
        Get_LatchLed = data%State%notifications%LatchLed
    end function

    subroutine Set_IrSafetyValveLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: data%EquipmentControl%DrillingConsole%CloseKellyCockLed => IRSafetyValveLed
        use CManifolds, only: &
            InstallSafetyValve_TopDrive, &
            InstallSafetyValve_KellyMode, &
            InstallSafetyValve_TripMode, &
            RemoveSafetyValve_TopDrive, &
            RemoveSafetyValve_KellyMode, &
            RemoveSafetyValve_TripMode 
        use UnitySignalVariables
    use UnitySignalsModule, only: Set_SafetyValve_Install, Set_SafetyValve_Remove
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, TopDrive_DriveType, Kelly_DriveType
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%IrSafetyValveLed == v) return
        #endif
        data%State%notifications%IrSafetyValveLed = v
        
        if(data%State%notifications%IrSafetyValveLed) then
            data%EquipmentControl%DrillingConsole%IRSafetyValveLed = 1
            
            if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) call InstallSafetyValve_TopDrive()            
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_IrSafetyValveLed == 0) call InstallSafetyValve_KellyMode()
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_IrSafetyValveLed == 1) call InstallSafetyValve_TripMode()
            
            call Set_SafetyValve_Install()
        else 
            data%EquipmentControl%DrillingConsole%IRSafetyValveLed = 0
            
            if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) call RemoveSafetyValve_TopDrive()            
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_IrSafetyValveLed == 0) call RemoveSafetyValve_KellyMode()
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_IrSafetyValveLed == 1) call RemoveSafetyValve_TripMode()
                        
            call Set_SafetyValve_Remove()
        endif
        
        !**call data%State%notifications%OnIrSafetyValveLedChange%RunAll()
    end subroutine
    
    logical function Get_IrSafetyValveLed()
        implicit none
        Get_IrSafetyValveLed = data%State%notifications%IrSafetyValveLed
    end function
    
    subroutine Set_IrIBopLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: IRIBopLedHw => IRIBopLed
        use CManifolds, only: InstallIBop, RemoveIBop
        use UnitySignalVariables
    use UnitySignalsModule, only: Set_Ibop_Install, Set_Ibop_Remove
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%IrIBopLed == v) return
        #endif
        data%State%notifications%IrIBopLed = v
        if(data%State%notifications%IrIBopLed) then
            data%EquipmentControl%DrillingConsole%IRIBopLed = 1
            call InstallIBop()
            call Set_Ibop_Install()
        else 
            data%EquipmentControl%DrillingConsole%IRIBopLed = 0
            call RemoveIBop()
            call Set_Ibop_Remove()
        endif
        !**call data%State%notifications%OnIrIBopLedChange%RunAll()
    end subroutine
    
    logical function Get_IrIBopLed()
        implicit none
        Get_IrIBopLed = data%State%notifications%IrIBopLed
    end function

    subroutine Set_FillMouseHoleLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: FillMouseHoleLedHw => FillMouseHoleLed
        ! use CMouseHoleEnumVariables
        use UnitySignalVariables
    use UnitySignalsModule
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%FillMouseHoleLed == v) return
        #endif
        data%State%notifications%FillMouseHoleLed = v
        if(data%State%notifications%FillMouseHoleLed) then
            data%EquipmentControl%DrillingConsole%FillMouseHoleLed = 1
            !call Set_MouseHole(MOUSE_HOLE_FILL)
        else 
            data%EquipmentControl%DrillingConsole%FillMouseHoleLed = 0
            !call Set_MouseHole(MOUSE_HOLE_EMPTY)
        endif
        !**call data%State%notifications%OnFillMouseHoleLedChange%RunAll()
    end subroutine
    
    logical function Get_FillMouseHoleLed()
        implicit none
        Get_FillMouseHoleLed = data%State%notifications%FillMouseHoleLed
    end function

    subroutine Set_CloseKellyCockLed(v)
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: CloseKellyCockLedHw => CloseKellyCockLed
        use CManifolds, only: CloseKellyCock
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%CloseKellyCockLed == v) return
        #endif
        data%State%notifications%CloseKellyCockLed = v        
        if(data%State%notifications%CloseKellyCockLed) then
            call CloseKellyCock()
        endif 
        !**call data%State%notifications%OnCloseKellyCockLedChange%RunAll()
    end subroutine
    
    logical function Get_CloseKellyCockLed()
        implicit none
        Get_CloseKellyCockLed = data%State%notifications%CloseKellyCockLed
    end function

    subroutine Set_CloseSafetyValveLed(v)
        ! use CDrillingConsoleVariables
    use SimulationVariables
    !@use ConfigurationVariables, only: CloseSafetyValveLedHw => CloseSafetyValveLed
        use CManifolds, only: CloseSafetyValve_TopDrive, CloseSafetyValve_KellyMode, CloseSafetyValve_TripMode
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, TopDrive_DriveType, Kelly_DriveType
        implicit none
        logical , intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%State%notifications%CloseSafetyValveLed == v) return
        #endif
        data%State%notifications%CloseSafetyValveLed = v
        if(data%State%notifications%CloseSafetyValveLed) then
            !!call CloseSafetyValve()
            if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) call CloseSafetyValve_TopDrive()            
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_CloseSafetyValveLed == 0) call CloseSafetyValve_KellyMode()
            if(data%Configuration%Hoisting%DriveType == Kelly_DriveType .and. data%State%notifications%operation_CloseSafetyValveLed == 1) call CloseSafetyValve_TripMode()
        endif
        !**call data%State%notifications%OnCloseSafetyValveLedChange%RunAll()
    end subroutine
    
    logical function Get_CloseSafetyValveLed()
        implicit none
        Get_CloseSafetyValveLed = data%State%notifications%CloseSafetyValveLed
    end function
        
end module NotificationModule

