module COperationConditionEnum
    use CLog4
    use OperationScenariosModule
    use SimulationVariables
    implicit none
    ! enum, bind(c)
    !     enumerator OPERATION_DRILL
    !     enumerator OPERATION_TRIP
    ! end enum
        
    contains
    
    subroutine Set_OperationCondition(v)
        ! use CKellyEnumVariables
        implicit none
        integer , intent(in) :: v
#ifdef ExcludeExtraChanges
        if(data%State%unitySignals%OperationCondition == v) return
#endif
        data%State%unitySignals%OperationCondition = v
#ifdef deb
	    print*, 'data%State%unitySignals%OperationCondition=', data%State%unitySignals%OperationCondition
#endif
        ! call OnOperationConditionChange%RunAll()
        ! call OnOperationConditionChangeInt%RunAll(data%State%unitySignals%OperationCondition)
    end subroutine
    
    integer function Get_OperationCondition()
        implicit none
        Get_OperationCondition = data%State%unitySignals%OperationCondition
    end function




    subroutine Evaluate_OperationCondition()
        implicit none
        
!        if (DriveType == TopDrive_DriveType) then
!#ifdef OST
!            print*, 'Evaluate_OperationCondition=TopDrive'
!#endif
!        endif
!        
!        
!        
!        
!        
!        
!        
!        
!        if (DriveType == Kelly_DriveType) then
!#ifdef OST
!            print*, 'Evaluate_OperationCondition=Kelly'
!#endif
!        endif
        
    end subroutine
    
    ! subroutine Subscribe_OperationCondition()
    !     use CDrillingConsoleVariables
!@    use ConfigurationVariables
!@    use ConfigurationVariables
    !     implicit none
    !     call OnLatchPipePress%Add(ButtonPress_Latch_OperationCondition)
    !     call OnUnlatchPipePress%Add(ButtonPress_Unlatch_OperationCondition)
    ! end subroutine
    
    subroutine ButtonPress_Latch_OperationCondition()
        ! use ConfigurationVariables
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, Kelly_DriveType
        use CManifolds, only: InstallKellyCock
        implicit none
        
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Latch_OperationCondition=TopDrive'
#endif
        endif
        
        
        
        
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Latch_OperationCondition=Kelly'
#endif

            !OPERATION-CODE=17
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_KellyConnection() == KELLY_CONNECTION_NOTHING .and.&
                Get_Swing() == SWING_RAT_HOLE_END .and.& 
                Get_LatchLed() .and.&
                Get_UnlatchLed() == .false.) then
            
                call Set_OperationCondition(OPERATION_DRILL)
                call Set_LatchLed(.false.)
                call Set_UnlatchLed(.true.)
                call Set_Kelly(KELLY_INSTALL)
                if(data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
                    call InstallKellyCock() !drill mode
                endif
                return
            end if

        endif
        
        
        
        
        
        
        
        
        
    end subroutine
    
    subroutine ButtonPress_Unlatch_OperationCondition()
        use CHoistingVariables
    use SimulationVariables!, only: data%Configuration%Hoisting%DriveType, Kelly_DriveType
        use CManifolds, only: RemoveKellyCock
        implicit none
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Unlatch_OperationCondition=TopDrive'
#endif
        endif
        
        
        
        
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Unlatch_OperationConditions=Kelly'
#endif


            !OPERATION-CODE=18
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_KellyConnection() == KELLY_CONNECTION_NOTHING .and.&
                Get_Swing() == SWING_RAT_HOLE_END .and.& 
                Get_LatchLed() == .false. .and.&
                Get_UnlatchLed()) then
            
                    call Set_Kelly(KELLY_REMOVE)
                    call Set_Swing(SWING_WELL_BEGIN)
                    call Set_OperationCondition(OPERATION_TRIP)
                    call Set_LatchLed(.true.)
                    call Set_UnlatchLed(.false.)
                    call Set_IrSafetyValveLed(.false.)
                    if(data%Configuration%Hoisting%DriveType == Kelly_DriveType) then ! top drive mode
                        call RemoveKellyCock()  !trip mode
                    endif
                    return
            end if



        endif
        
        
        
        
        
        
    end subroutine
    
end module COperationConditionEnum