module COperationScenariosSettings
    implicit none
    public
    contains
    
    !subroutine Initialization()
    !    ! use CSimulationVariables
    !    use CUnityOutputs, only: SetupUnityOutputs => Setup
    !    
    !    use CBucketEnum
    !    use CTongEnum
    !    use CSwingEnum
    !    use CSlipsEnum
    !    use CSafetyValveEnum
    !    use UnitySignalsModule
    !    use CMouseHoleEnum
    !    use CKellyEnum
    !    use OperationScenariosModule
    !    use CIbopEnum
    !    use CHeadEnum
    !    use CElevatorEnum
    !    use CElevatorConnectionEnum
    !    
    !    use CInstallFillupHeadPermission
    !    use CInstallMudBucketPermission
    !    use CIrIbopPermission
    !    use CIrSafetyValvePermission
    !    use CRemoveFillupHeadPermission
    !    use CRemoveMudBucketPermission
    !     
    !    use CCloseKellyCockLedNotification
    !    use CCloseSafetyValveLedNotification
    !    use CFillMouseHoleLedNotification
    !    use CIrIBopLedNotification
    !    use CIrSafetyValveLedNotification
    !    use CLatchLedNotification
    !    use COpenKellyCockLedNotification
    !    use COpenSafetyValveLedNotification
    !    use CSlipsNotification
    !    use CSwingLedNotification
    !    use CTongNotification
    !    use CUnlatchLedNotification
    !    
    !    use CHookHeight
    !    use CIbopHeight
    !    use CNearFloorConnection
    !    use CSafetyValveHeight
    !    use CSlackOff
    !    use CStandRack
    !    use CStringPressure
    !    use CZeroStringSpeed
    !    
    !    use CStringUpdate
    !    
    !    use CFlowPipeDisconnectEnum
    !    use CFlowKellyDisconnectEnum
    !    
    !    use CFillupHeadPermission
    !    use CSwingDrillPermission
    !    use CSwingOffPermission
    !    use CSwingTiltPermission
    !    use CTdsStemJointHeight
    !    use UnitySignalsModule !for CTdsConnectionModesEnum
    !    use CTdsElevatorModesEnum
    !    use CTdsSpineEnum
    !    use CTdsSwingEnum
    !    use CTdsTongEnum
    !    use CTdsBackupClamp
    !    
    !    use CTdsIbopLedNotification
    !    use CTdsPowerLedNotification
    !    
    !    use CTdsTorqueWrenchLedNotification
    !    
    !    implicit none
    !    
    !    call SetupUnityOutputs()
    !    
    !    call Subscribe_Tong()
    !    call Subscribe_MudBucket()
    !    call Subscribe_ElevatorConnection()
    !    call Subscribe_Elevator()
    !    call Subscribe_FillupHead()
    !    call Subscribe_Ibop()
    !    call Subscribe_KellyConnection()
    !    call Subscribe_Kelly()
    !    call Subscribe_MouseHole()
    !    call Subscribe_OperationCondition()
    !    call Subscribe_SafetyValve()
    !    call Subscribe_Slips()
    !    call Subscribe_Swing()
    !    
    !    
    !    call Subscribe_InstallFillupHeadPermission()
    !    call Subscribe_InstallMudBucketPermission()
    !    call Subscribe_IrIbopPermission()
    !    call Subscribe_IrSafetyValvePermission()
    !    call Subscribe_RemoveFillupHeadPermission()
    !    call Subscribe_RemoveMudBucketPermission()
    !    
    !    call Subscribe_CloseKellyCockLed()
    !    call Subscribe_CloseSafetyValveLed()
    !    call Subscribe_FillMouseHoleLed()
    !    call Subscribe_IrIBopLed()
    !    call Subscribe_IrSafetyValveLed()
    !    call Subscribe_LatchLed()
    !    call Subscribe_OpenKellyCockLed()
    !    call Subscribe_OpenSafetyValveLed()
    !    call Subscribe_SlipsNotification()
    !    call Subscribe_SwingLed()
    !    call Subscribe_UnlatchLed()
    !    
    !    call Subscribe_HookHeight()
    !    call Subscribe_IbopHeight()
    !    call Subscribe_NearFloorConnection()
    !    call Subscribe_SafetyValveHeight()
    !    call Subscribe_SlackOff()
    !    call Subscribe_StringPressure()
    !    call Subscribe_ZeroStringSpeed()
    !    call Subscribe_StandRack()
    !    
    !    call Subscribe_StringUpdate()
    !    
    !    call Subscribe_TongNotification()
    !    
    !    call Subscribe_FlowKellyDisconnect()
    !    call Subscribe_FlowPipeDisconnect()
    !    
    !    
    !    
    !    
    !    
    !    !top drive
    !    call Subscribe_TdsConnectionModes()
    !    call Subscribe_TdsElevatorModes()
    !    call Subscribe_FillupHeadPermission()
    !    call Subscribe_SwingDrillPermission()
    !    call Subscribe_SwingOffPermission()
    !    call Subscribe_SwingTiltPermission()
    !    call Subscribe_TdsStemJointHeight()
    !    call Subscribe_TdsTong()
    !    call Subscribe_TdsBackupClamp()
    !    call Subscribe_TdsSwing()
    !    call Subscribe_TdsSpine()
    !    call Subscribe_PowerLed()
    !    call Subscribe_IbopLed()
    !    
    !    call Subscribe_TorqueWrenchLed()
    !    
    !    
    !    call OnSimulationStart%Add(SetDefaultValues)
    !    
    !end subroutine
    
    subroutine SetDefaultValues()
        use OperationScenariosModule
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, TopDrive_DriveType
        use CManifolds, only: RemoveSafetyValve_TripMode, RemoveSafetyValve_KellyMode
        implicit none
        
        call Set_KellyConnection(KELLY_CONNECTION_NOTHING)
        !Get_KellyConnection()  
        !KELLY_CONNECTION_NOTHING    
        !KELLY_CONNECTION_STRING
        !KELLY_CONNECTION_SINGLE
        call Set_ElevatorConnection(ELEVATOR_CONNECTION_NOTHING)
        !Get_ElevatorConnection()
        !ELEVATOR_CONNECTION_NOTHING
        !ELEVATOR_CONNECTION_STRING
        !ELEVATOR_CONNECTION_STAND
        !ELEVATOR_CONNECTION_SINGLE
        !ELEVATOR_LATCH_STRING
        !ELEVATOR_LATCH_SINGLE
        !ELEVATOR_LATCH_STAND
        
        
        
        
        
        
        
        
        call Set_CloseKellyCockLed(.false.)    !Get_CloseKellyCockLed()
        call Set_CloseSafetyValveLed(.false.)  !Get_CloseSafetyValveLed()
        call Set_FillMouseHoleLed(.true.)      !Get_FillMouseHoleLed()
        call Set_IrIBopLed(.false.)            !Get_IrIBopLed()
        !call Set_IrSafetyValveLed(.true.)      !Get_IrSafetyValveLed()
        if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
            call RemoveSafetyValve_TripMode()
            call RemoveSafetyValve_KellyMode()
            call Set_IrSafetyValveLed(.false.)
        else
            call Set_IrSafetyValveLed(.true.)
        endif
        call Set_LatchLed(.false.)             !Get_LatchLed()
        call Set_OpenKellyCockLed(.true.)      !Get_OpenKellyCockLed()
        call Set_OpenSafetyValveLed(.true.)    !Get_OpenSafetyValveLed()
        call Set_SlipsNotification(.false.)    !Get_SlipsNotification()
        call Set_SwingLed(.false.)             !Get_SwingLed()
        call Set_TongNotification(.false.)     !Get_TongNotification()
        call Set_UnlatchLed(.false.)           !Get_UnlatchLed()
        
        
        
        
        
        
        
        call Set_InstallFillupHeadPermission(.false.)  !Get_InstallFillupHeadPermission()
        call Set_InstallMudBucketPermission(.false.)   !Get_InstallMudBucketPermission()
        call Set_IrIbopPermission(.false.)             !Get_IrIbopPermission()
        call Set_IrSafetyValvePermission(.false.)      !Get_IrSafetyValvePermission()
        call Set_RemoveFillupHeadPermission(.false.)   !Get_RemoveFillupHeadPermission()
        call Set_RemoveMudBucketPermission(.false.)    !Get_RemoveMudBucketPermission()
        
        
        
        
        
        
        
        
        
        !call Set_HookHeight(REAL(70.0, 8))   !Get_HookHeight()
        call Set_IbopHeight(3.0)              !Get_IbopHeight()
        !call Set_NearFloorConnection(3.0)    !Get_NearFloorConnection()
        call Set_SafetyValveHeight(3.0)       !Get_SafetyValveHeight()
        call Set_SlackOff(.true.)             !Get_SlackOff()
        !call Set_StandRack(0.0)              !Get_StandRack()
        !call Set_StringPressure(0.0)         !Get_StringPressure()
        !call Set_ZeroStringSpeed(.true.)     !Get_ZeroStringSpeed()
        
        
        
        
        
        
        
        
        !Get_ElevatorConnected()
        !Get_JointConnection()
        !Get_NearMonkeyBoardPosition()
        !Get_NearFloorPosition()
        !Get_SingleSetInMouseHole()
        !Get_SwingCenter()
        
        
        
        
        
        
        call Set_MudBucket(MUD_BUCKET_REMOVE)
        !Get_MudBucket()
        !MUD_BUCKET_INSTALL
        !MUD_BUCKET_REMOVE
        
        call Set_Elevator(ELEVATOR_NEUTRAL)
        ! Get_Elevator()
        !ELEVATOR_NEUTRAL
        !ELEVATOR_LATCH_STRING_BEGIN
        !ELEVATOR_LATCH_STRING_END
        !ELEVATOR_UNLATCH_STRING_BEGIN
        !ELEVATOR_UNLATCH_STRING_END
        !ELEVATOR_LATCH_STAND_BEGIN
        !ELEVATOR_LATCH_STAND_END
        !ELEVATOR_UNLATCH_STAND_BEGIN
        !ELEVATOR_UNLATCH_STAND_END
        !ELEVATOR_LATCH_SINGLE_BEGIN
        !ELEVATOR_LATCH_SINGLE_END
        !ELEVATOR_UNLATCH_SINGLE_BEGIN
        !ELEVATOR_UNLATCH_SINGLE_END
                
        call Set_FillupHead(FILLUP_HEAD_REMOVE)
        !Get_FillupHead()
        !FILLUP_HEAD_INSTALL
        !FILLUP_HEAD_REMOVE
        
        call Set_Ibop(IBOP_REMOVE)
        !Get_Ibop()
        !IBOP_INSTALL
        !IBOP_REMOVE
        
        call Set_Kelly(KELLY_INSTALL)
        !Get_Kelly()
        !KELLY_NEUTRAL
        !KELLY_INSTALL
        !KELLY_REMOVE
        
        call Set_MouseHole(MOUSE_HOLE_FILL)
        !Get_MouseHole()
        !MOUSE_HOLE_NEUTRAL
        !MOUSE_HOLE_FILL
        !MOUSE_HOLE_EMPTY
        
        call Set_OperationCondition(OPERATION_DRILL)
        !Get_OperationCondition()
        !OPERATION_DRILL
        !OPERATION_TRIP
        
        
        call Set_SafetyValve(SAFETY_VALVE_INSTALL)
        !Get_SafetyValve()
        !SAFETY_VALVE_INSTALL
        !SAFETY_VALVE_REMOVE
        if(data%Configuration%Hoisting%DriveType == TopDrive_DriveType) call Set_SafetyValve(SAFETY_VALVE_REMOVE)
        
        call Set_Slips(SLIPS_SET_BEGIN)
        !Get_Slips()
        !SLIPS_NEUTRAL  
        !SLIPS_SET_BEGIN
        !SLIPS_SET_END    
        !SLIPS_UNSET_BEGIN      
        !SLIPS_UNSET_END      
        
        call Set_Swing(SWING_WELL_BEGIN)
        !Get_Swing()
        !SWING_NEUTRAL
        !SWING_MOUSE_HOLE_BEGIN
        !SWING_MOUSE_HOLE_END
        !SWING_RAT_HOLE_BEGIN
        !SWING_RAT_HOLE_END
        !SWING_WELL_BEGIN
        !SWING_WELL_END
        
        call Set_Tong(TONG_NEUTRAL)
        !Get_Tong()
        !TONG_NEUTRAL
        !TONG_BREAKOUT_BEGIN
        !TONG_BREAKOUT_END
        !TONG_MAKEUP_BEGIN
        !TONG_MAKEUP_END
        
        
        call Set_StringUpdate(STRING_UPDATE_NEUTRAL)
        !Get_StringUpdate()
        !STRING_UPDATE_NEUTRAL
        !STRING_UPDATE_ADD_SINGLE
        !STRING_UPDATE_ADD_STAND
        !STRING_UPDATE_REMOVE_SINGLE
        !STRING_UPDATE_REMOVE_STAND
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_NOTHING)
        !Get_TdsElevatorModes()
        !TDS_ELEVATOR_CONNECTION_NOTHING
        !TDS_ELEVATOR_CONNECTION_STRING
        !TDS_ELEVATOR_CONNECTION_SINGLE
        !TDS_ELEVATOR_CONNECTION_STAND
        !TDS_ELEVATOR_LATCH_STRING
        !TDS_ELEVATOR_LATCH_SINGLE
        !TDS_ELEVATOR_LATCH_STAND
        
        
        
        
        
        call Set_TdsConnectionModes(TDS_CONNECTION_NOTHING)
        !Get_TdsConnectionModes()
        !TDS_CONNECTION_NOTHING
        !TDS_CONNECTION_STRING
        !TDS_CONNECTION_SPINE
        
        
        
        
        call Set_SwingTiltPermission(.false.)     !Get_SwingTiltPermission()
        call Set_SwingOffPermission(.false.)      !Get_SwingOffPermission()
        call Set_SwingDrillPermission(.false.)    !Get_SwingDrillPermission()
        call Set_FillupHeadPermission(.false.)    !Get_FillupHeadPermission()
        
        call Set_PowerLed(.false.)
        call Set_IbopLed(.false.)
        
        call Set_TorqueWrenchLed(0)             !Get_TorqueWrenchLed()
        
        
        !TdsPower_REV = 1
        !TdsPower_OFF = 0
        !TdsPower_FWD = -1
        !
        !TdsMu_TORQ = 1
        !TdsMu_SPINE = 0
        !TdsMu_DRILL = -1
        !
        !TdsLinkTilt_TILT = 1
        !TdsLinkTilt_OFF = 0
        !TdsLinkTilt_DRILL = -1

        !TDS IBOP	    TopDriveIbop
        !TDS LinkTilt	TopDriveLinkTiltState
        !TDS MU	        TopDriveDrillTorqueState
        !TDS Power 	    TopDriveTdsPowerState
        
        !TopDriveTorqueWrench   PRESSED=True or UNPRESSED=Falses
        
        !Get_TdsConnectionPossible()
        !Get_TdsStemIn()
        
        !call Set_TdsStemJointHeight(v)  !Get_TdsStemJointHeight()
        
        
        !Get_SwingDrillPermission()
        !Get_SwingOffPermission()
        !Get_SwingTiltPermission()
        !Get_FillupHeadPermission()
        
        call Set_TdsTong(TDS_TONG_BREAKOUT_END)
        !Get_TdsTong()
        !TDS_TONG_BREAKOUT_BEGIN
        !TDS_TONG_BREAKOUT_END
        !TDS_TONG_MAKEUP_BEGIN
        !TDS_TONG_MAKEUP_END
        
        
        call Set_TdsBackupClamp(BACKUP_CLAMP_OFF_END)
        !Get_TdsBackupClamp()
        !BACKUP_CLAMP_OFF_END
        !BACKUP_CLAMP_OFF_BEGIN
        !BACKUP_CLAMP_FW_BEGIN
        !BACKUP_CLAMP_FW_END
        
        
        call Set_TdsSwing(TDS_SWING_OFF_END)
        !Get_TdsSwing()
        !TDS_SWING_NEUTRAL
        !TDS_SWING_OFF_BEGIN
        !TDS_SWING_OFF_END
        !TDS_SWING_DRILL_BEGIN
        !TDS_SWING_DRILL_END
        !TDS_SWING_TILT_BEGIN
        !TDS_SWING_TILT_END
        
        
        
        
        
        call Set_TdsSpine(TDS_SPINE_NEUTRAL) !TDS_SPINE_DISCONNECT_END
        !Get_TdsSpine()
        !TDS_SPINE_NEUTRAL
        !TDS_SPINE_CONNECT_BEGIN
        !TDS_SPINE_CONNECT_END
        !TDS_SPINE_DISCONNECT_BEGIN
        !TDS_SPINE_DISCONNECT_END
        
        
        
        
        
        
        
        
        
        
        
    end subroutine
    
    subroutine SetDefaults_WN()
    !DEC$ ATTRIBUTES DLLEXPORT :: SetDefaults_WN
    !DEC$ ATTRIBUTES ALIAS: 'SetDefaults_WN' :: SetDefaults_WN
        implicit none
        call SetDefaultValues()
    end subroutine
    
end module COperationScenariosSettings