*** Settings ***
Library           AppiumLibrary
Library           Collections
Library           String

*** Keywords ***
Open Application Mobile
    [Arguments]    ${deviceName}    ${platformName}    ${platformVersion}    ${appPackage}    ${appActivity}    ${noReset}=True    ${appiumPort}=4723    ${automationName}=UiAutomator2
    Open Application    http://localhost:${appiumPort}/wd/hub    platformName=Android    deviceName=${deviceName}    appPackage=${appPackage}    appActivity=${appActivity}    noReset=${noReset}    automationName=${automationName}

Wait Until Contains Message
    [Arguments]    ${Element_text}    ${timeout}=10
    FOR    ${I}    IN RANGE    1    10
        ${Socre}    Get Source
        ${Status}    Run Keyword And Ignore Error    Should Contain    ${Socre}    ${Element_text}
        Run Keyword If    "${Status}[0]"=="PASS"    Exit For Loop
    END
    Wait Until Page Contains    ${Element_text}    ${timeout}

Click Element Message
    [Arguments]    ${Element_text}    ${timeout}=10
    Wait Until Contains Message    ${Element_text}    ${timeout}
    Click Element    //*[contains(@text,"${Element_text}")]

Input for Elements Text
    [Arguments]    ${Element_text}    ${Index}    ${Message}
    Wait Until Contains Message    ${Element_text}
    ${List_Webelements}    Get Webelements    //*[contains(@text,"${Element_text}")]
    Insert Into List    ${List_Webelements}    0    ${EMPTY}
    Input Text    ${List_Webelements}[${Index}]    ${Message}
    Hide Keyboard

Swipe By Percent Height
    [Arguments]    ${Start}=1    ${End}=99
    ${height}    Get Window Height
    ${width}    Get Window Width
    ${StartX}    Evaluate    ${width}/2
    ${StartY}    Evaluate    (${height}*${Start})/100
    ${EndX}    Evaluate    ${width}/2
    ${EndY}    Evaluate    (${height}*${End})/100
    @{List}    Create List    ${StartX}    ${StartY}    ${EndX}    ${EndY}    1000
    Swipe    @{List}

Get Message Regular
    [Arguments]    ${Element_text}    ${Pattern}    ${timeout}=10
    Wait Until Contains Message    ${Element_text}    ${timeout}
    ${List_Message}    Get WebElement    //*[contains(@text,"${Element_text}")]
    ${GetText}    Get Text    ${List_Message}
    ${Reg}    Get Regexp Matches    ${GetText}    ${Pattern}
    [Return]    ${Reg}

Click Image Index
    [Arguments]    ${index}
    ${List_Webelements}    Get Webelements    //*[contains(@class,"ImageView")]
    Insert Into List    ${List_Webelements}    0    ${EMPTY}
    Click Element    ${List_Webelements}[${index}]

Click Group Index
    [Arguments]    ${index}
    ${List_Webelements}    Get Webelements    //*[contains(@class,"ViewGroup")]
    Insert Into List    ${List_Webelements}    0    ${EMPTY}
    Click Element    ${List_Webelements}[${index}]

Press Number
    [Arguments]    ${Nunber}=0
    ${Get}    Set Variable    ${Nunber}
    @{SPD}    Split String To Characters    ${Get}
    FOR    ${i}    IN    @{SPD}
        ${num}    Evaluate    7+${i}
        Press Keycode    ${num}
    END

Input for Elements
    [Arguments]    ${Index}    ${Message}
    ${List_Webelements}    Get Webelements    //*[contains(@class,"EditText")]
    Insert Into List    ${List_Webelements}    0    ${EMPTY}
    Input Text    ${List_Webelements}[${Index}]    ${Message}
    Hide Keyboard

Wait Until Contains Element
    [Arguments]    ${attributes}    ${value}    ${timeout}=10
    ${Locator}     Set Variable    //*[contains(${attributes}, "${value}")]
    Wait Until Page Contains Element    ${Locator}    ${timeout}

Get Message
    [Arguments]    ${attributes}    ${value}    ${timeout}=10
    Wait Until Contains Message    ${value}    ${timeout}
    ${List_Message}    Get WebElement    //*[contains(${attributes},"${attributes}")]
    ${GetText}    Get Text    ${List_Message}
    [Return]    ${GetText}

Click Elements
    [Arguments]    ${attributes}    ${value}    ${timeout}=10
    Wait Until Contains Element    ${attributes}    ${value}    ${timeout}
    Click Element    //*[contains(${attributes},"${value}")]

Page Down To Message
    [Arguments]    ${Element_text}    ${Rounds}=20
    FOR    ${index}    IN RANGE    ${Rounds}
        ${Socre}    Get Source
        ${Status}    Run Keyword And Ignore Error    Should Contain    ${Socre}    ${Element_text}
        Run Keyword If    "${Status}[0]"=="PASS"    Exit For Loop    ELSE    Press Keycode    93
    END

Page Up To Message
    [Arguments]    ${Element_text}    ${Rounds}=20
    FOR    ${index}    IN RANGE    ${Rounds}
        ${Socre}    Get Source
        ${Status}    Run Keyword And Ignore Error    Should Contain    ${Socre}    ${Element_text}
        Run Keyword If    "${Status}[0]"=="PASS"    Exit For Loop    ELSE    Press Keycode    92
    END
