Compile function Clean_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler

Declare Function Database_Services, Error_Services, Logging_Services, Datetime
Declare Function RTI_CreateGUID, Tool_Services, SRP_Json, Date_Services
Declare Subroutine Database_Services, Error_Services, Logging_Services, SRP_Json, Lot_Event_Services

$insert LOGICAL
$Insert CLEAN_EQUATES
$Insert LOT_EQUATES
$Insert LOT_OPERATION_EQUATES
$Insert TOOL_EQUATES
$Insert TOOL_CLASS_EQUATES

Options Stage = 'LWI', 

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service CreateNewCleanRecord(LotId, LotOperationId, UserId)

    ErrorMessage = ''
    CleanRecId = ''
    If RowExists('LOT', LotId) then
        //ToDo check Lot Operation Exists
        //Checks complete, create the CLEAN record
        TransDtm = Datetime()
        CleanRecId = RTI_CreateGUID()
        CleanRec = ''
        CleanRec<CLEAN_LOT_ID$> = LotId
        CleanRec<CLEAN_LOT_OPERATION_ID$> = LotOperationId
        CleanRec<CLEAN_CLEAN_START_DTM$> = TransDtm
        CleanRec<CLEAN_CLEAN_START_USER_ID$> = UserId
        Database_Services('WriteDataRow', 'CLEAN', CleanRecId, CleanRec)
        If Error_Services('NoError') then
            If LotOperationId NE '' then
                LotOperationRec = Database_Services('ReadDataRow', 'LOT_OPERATION', LotOperationId, True$, 0, False$)
                LotOperationRec<LOT_OPERATION_CLEAN_ID$> = CleanRecId
                Database_Services('WriteDataRow', 'LOT_OPERATION', LotOperationId, LotOperationRec)
                If Error_Services('NoError') then
                    Lot_Event_Services('CreateLotEvent', LotId, TransDtm, 'CLEAN_START', 'Created clean record.', '', UserId)
                end else
                    ErrorMessage = Error_Services('GetMessage')
                end
            end
        end else
            ErrorMessage = Error_Services('GetMessage')
        end
    end else
       //Todo add error message 
    end
    
    If ErrorMessage EQ '' then
        Response = CleanRecId
    end else
        // Todo: Add logging
        ErrorMessage = 'Error Creating a new clean record: ' : ErrorMessage
        Error_Services('Add', ErrorMessage)
    end
    
End Service

Service ConvertCleanRecToJson(CleanRecId)
    
    ErrorMessage = ''
    CleanRecJson = ''
    
    If RowExists('CLEAN', CleanRecId) then
        CleanRec = Database_Services('ReadDataRow', 'CLEAN', CleanRecId, True$, 0, False$)
        If Error_Services('NoError') then
            objJson = ''
            If SRP_Json(objJson, 'New', 'Object') then
                SRP_Json(objJson, 'SetValue', 'CleanId', CleanRecId, 'String')
                SRP_Json(objJson, 'SetValue', 'LotId', CleanRec<CLEAN_LOT_ID$>, 'String')
                LegacyLotId = Database_Services('ReadDataColumn', 'LOT', CleanRec<CLEAN_LOT_ID$>, LOT_LEGACY_LOT_ID$)
                SRP_Json(objJson, 'SetValue', 'LegacyLotId', LegacyLotId, 'String')
                SRP_Json(objJson, 'SetValue', 'Recipe', CleanRec<CLEAN_RECIPE$>, 'String')
                SRP_Json(objJson, 'SetValue', 'Tool', CleanRec<CLEAN_TOOL$>, 'String')
                SRP_Json(objJson, 'SetValue', 'StartUser', CleanRec<CLEAN_CLEAN_START_USER_ID$>, 'String')
                StartDtm = Date_Services('ConvertDateTimeToISO8601', CleanRec<CLEAN_CLEAN_START_DTM$>)
                if StartDtm NE '' then
                    SRP_Json(objJson, 'SetValue', 'StartDtm', StartDtm)
                end
                SRP_Json(objJson, 'SetValue', 'StopUser', CleanRec<CLEAN_CLEAN_STOP_USER_ID$>, 'String')
                StopDtm = Date_Services('ConvertDateTimeToISO8601', CleanRec<CLEAN_CLEAN_STOP_DTM$>)
                if StopDtm NE '' then
                   SRP_Json(objJson, 'SetValue', 'StopDtm', StopDtm, 'String') 
                end
                SRP_Json(objJson, 'SetValue', 'LotOperationId', CleanRec<CLEAN_LOT_OPERATION_ID$>, 'String')
                CleanRecJson = SRP_Json(objJson, 'Stringify', 'Styled')
                SRP_Json(objJson, 'Release')
            end else
                ErrorMessage = 'Error creating clean record json'
            end
        end else
            ErrorMessage = Error_Services('GetMessage')
        end
    end else
        ErrorMessage = 'Clean record not found.'
    end
    
    If ErrorMessage EQ '' then
        Response = CleanRecJson
    end else
        Error_Services('Add', 'Error getting clean record : ' : ErrorMessage)
    end
    
end service

Service MarkCleanRecComplete(CleanRecId, CleanRecipe, CleanTool, CleanUser)
    
    ErrorMessage = ''
    TransDtm = Datetime()
    
    If RowExists('CLEAN', CleanRecId) then
        CleanRec = Database_Services('ReadDataRow', 'CLEAN', CleanRecId, True$, 0, False$)
        If CleanRec<CLEAN_CLEAN_START_DTM$> NE '' then
            If CleanRec<CLEAN_TOOL$> EQ '' AND CleanRec<CLEAN_RECIPE$> EQ '' then
                if RowExists('LSL_USERS', CleanUser) then
                    If CleanRec<CLEAN_COMPLETE_DTM$> EQ '' then
                        CleanRec<CLEAN_TOOL$> = CleanTool
                        CleanRec<CLEAN_RECIPE$> = CleanRecipe
                        CleanRec<CLEAN_CLEAN_STOP_USER_ID$> = CleanUser
                        CleanRec<CLEAN_CLEAN_STOP_DTM$> = TransDtm
                        Database_Services('WriteDataRow', 'CLEAN', CleanRecId, CleanRec)
                        If Error_Services('NoError') then
                            LotId = CleanRec<CLEAN_LOT_ID$>
                            Lot_Event_Services('CreateLotEvent', LotId, TransDtm, 'CLEAN', 'Clean completed', CleanTool, CleanUser)
                        end else
                            ErrorMessage = Error_Services('GetMessage')
                        end
                    end else
                        ErrorMessage = 'Clean is already signed off.'
                    end 
                end else
                    ErrorMessage = 'Clean tool or clean recipe is missing.'
                end
            end else
                ErrorMessage = 'Clean has already been logged.'
            end
        end else
            ErrorMessage = 'Clean has not been started.'
        end
    end else
        ErrorMessage = 'Clean record not found.'
    end
    
    If ErrorMessage NE '' then
        Error_Services('Add', ErrorMessage)
    end
    
end service

Service ValidateCleanRecord(CleanRecId)
    
    //ErrorMessage = ''
    CleanRecValid = False$
    
*    If CleanRecId NE '' then
*        If RowExists('CLEAN', CleanRecId) then
*            CleanRec = Database_Services('ReadDataRow', 'CLEAN', CleanRecId, True$, 0, False$)
*            SpecTool = CleanRec<CLEAN_TOOL$>
*            ToolUsed = CleanRec<CLEAN_RECIPE$>
*            If ToolUsed EQ SpecTool AND ToolUsed NE '' then
*                SpecRecipe = CleanRec<CLEAN_SPEC_RECIPE$>
*                RecipeUsed = CleanRec<CLEAN_RECIPE$>
*                If RecipeUsed EQ SpecRecipe AND RecipeUsed NE '' then
*                    CleanRecValid = True$
*                end
*            end
*        end else
*            ErrorMessage = 'Clean record not found in CLEAN database.'
*        end
*    end else
*        ErrorMessage = 'Clean ID was null.'
*    end
*    
*    If ErrorMessage EQ '' then
*        Response = CleanRecValid
*    end else
*        // Todo: Add logging
*        ErrorMessage = 'Error validating clean record: ' : ErrorMessage
*        Error_Services('Add', ErrorMessage)
*    end

    Response = CleanRecValid
End Service

Service GetCleanToolOptions(CleanRecId)

    If RowExists('CLEAN', CleanRecId) then
        CleanTools = Database_Services('ReadDataColumn', 'CLEAN', CleanRecId, CLEAN_SPEC_CLEAN_TOOL$, True$, 0, False$)
    end else
        CleanTools = Tool_Services('GetTools', 'AKRION') 
    end
    
    Response = CleanTools
    
end service

Service GetCleanRecipeOptions(CleanRecId)
    
    If RowExists('CLEAN', CleanRecId) then
        Recipes = Database_Services('ReadDataColumn', 'CLEAN', CleanRecId, CLEAN_SPEC_CLEAN_RECIPE$, True$, 0, False$)
    end else
        //Todo: Make this smarter, so as to block out pre-epi recipes
        Recipes = XLATE('TOOL_CLASS','AKRION',TOOL_CLASS_RECIPES$,'X')
    end
    
    Response = Recipes
    
end service
