#!/bin/bash

NUM_OF_CELLULAR=""
FILE_PATH="/usr/sbin/mx-interface-mgmt"
CELL_PREFIX="Cellular"

function test_pass_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[32mPASS\e[0m] $2"
        else
                echo -e "[\e[31mFAIL\e[0m] $2"
        fi
}

function test_fail_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[31mFAIL\e[0m] $2"
        else
                echo -e "[\e[32mPASS\e[0m] $2"
        fi
}

if [[ $# -ne 1 ]]; then
        echo "test_wrapper_cellular <num_of_cellular>"
        exit 1
fi

NUM_OF_CELLULAR=$1

# Test 1: Check if tool is installed
test -e $FILE_PATH
test_pass_result $? "Test 1: Check if tool is installed"

# check if device is support cellular
result=$(bash $FILE_PATH cellular)
if [ "$result" == "Error: Don't support cellular" ]; then
        echo "This device don't support cellular"
        exit 1
fi

# Test 2: Invalid CELLULAR Port format 1
bash $FILE_PATH cellular 00 get_power >/dev/null 2>&1
test_fail_result $? "Test 2: Invalid CELLULAR Port format 1"

# Test 3: Invalid CELLULAR Port format 2
bash $FILE_PATH cellular abc get_power >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid CELLULAR Port format 2"

# Test 4: Invalid CELLULAR Port
bash $FILE_PATH cellular $((NUM_OF_CELLULAR + 1)) get_power >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid CELLULAR Port"

# Test 5: Invalid CELLULAR Port and Power state format 1
bash $FILE_PATH cellular 00 set_power 01 >/dev/null 2>&1
test_fail_result $? "Test 5: Invalid CELLULAR Port and Power state format 1"

# Test 6: Invalid CELLULAR Port and Power state format 2
bash $FILE_PATH cellular abc set_power def >/dev/null 2>&1
test_fail_result $? "Test 6: Invalid CELLULAR Port and Power state format 2"

# Test 7: Invalid CELLULAR Power state format 1
bash $FILE_PATH cellular 0 set_power 01 >/dev/null 2>&1
test_fail_result $? "Test 7: Invalid CELLULAR Power state format 1"

# Test 8: Invalid CELLULAR Power state format 2
bash $FILE_PATH cellular 0 set_state ab >/dev/null 2>&1
test_fail_result $? "Test 8: Invalid CELLULAR Power state format 2"

# Test 9: Get All CELLULAR port Power state
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        result=$(bash $FILE_PATH cellular ${CELL_PREFIX}${i} get_power)
        test_pass_result $(
                [[ "$result" == "off" ]] ||
                        [[ "$result" == "on" ]]
                echo $?
        ) "Test 9: Get CELLULAR port $i Power State"
done

# Test 10: Set All CELLULAR port power ON
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        bash $FILE_PATH cellular ${CELL_PREFIX}${i} set_power on >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 10: Set CELLULAR port $i power on"
                continue
        fi

        result=$(bash $FILE_PATH cellular ${CELL_PREFIX}${i} get_power)
        test_pass_result $(
                [[ "$result" == "on" ]] || [[ "$result" == *"function select"* ]]
                echo $?
        ) "Test 10: Set CELLULAR port $i power ON"
done

# Test 11: Set All CELLULAR port power OFF
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        bash $FILE_PATH cellular ${CELL_PREFIX}${i} set_power off >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 11: Set CELLULAR port $i power off"
                continue
        fi

        result=$(bash $FILE_PATH cellular $i get_power)
        test_pass_result $(
                [[ "$result" == "off" ]] || [[ "$result" == *"function select"* ]]
                echo $?
        ) "Test 11: Set CELLULAR port $i power OFF"
done


# Test 12: Invalid CELLULAR Port and Sim slot format 1
bash $FILE_PATH cellular 00 set_sim_slot 01 >/dev/null 2>&1
test_fail_result $? "Test 12: Invalid CELLULAR Port and Sim slot format 1"

# Test 13: Invalid CELLULAR Port and Sim slot format 2
bash $FILE_PATH cellular abc set_sim_slot def >/dev/null 2>&1
test_fail_result $? "Test 13: Invalid CELLULAR Port and Sim slot format 2"

# Test 14: Invalid CELLULAR Sim slot format 1
bash $FILE_PATH cellular ${CELL_PREFIX}0 set_sim_slot 01 >/dev/null 2>&1
test_fail_result $? "Test 14: Invalid CELLULAR Sim slot format 1"

# Test 15: Invalid CELLULAR Sim slot format 2
bash $FILE_PATH cellular ${CELL_PREFIX}0 set_sim_slot ab >/dev/null 2>&1
test_fail_result $? "Test 15: Invalid CELLULAR Sim slot format 2"

# Test 16: Get All CELLULAR port Sim slot state
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        result=$(bash $FILE_PATH cellular ${CELL_PREFIX}${i} get_sim_slot)
        test_pass_result $(
                [[ "$result" == "1" ]] ||
                        [[ "$result" == "2" ]] ||
                        [[ "$result" == *"not allowed"* ]]
                echo $?
        ) "Test 16: Get CELLULAR port $i Sim slot State"
done

# Test 17: Set All CELLULAR port Sim slot A
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        bash $FILE_PATH cellular ${CELL_PREFIX}${i} set_sim_slot 1 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 17: Set CELLULAR port $i Sim slot A"
                continue
        fi

        result=$(bash $FILE_PATH cellular ${CELL_PREFIX}${i} get_sim_slot)
        test_pass_result $(
                [[ "$result" == "1" ]] ||
                        [[ "$result" == *"not allowed"* ]]
                echo $?
        ) "Test 17: Set CELLULAR port $i Sim slot A"
done

# Test 18: Set All CELLULAR port Sim slot B
for ((i = 1; i <= $NUM_OF_CELLULAR; ++i)); do
        bash $FILE_PATH cellular ${CELL_PREFIX}${i} set_sim_slot 2 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 18: Set CELLULAR port $i Sim slot B"
                continue
        fi

        result=$(bash $FILE_PATH cellular $i get_sim_slot)
        test_pass_result $(
                [[ "$result" == "2" ]] ||
                        [[ "$result" == *"not allowed"* ]]
                echo $?
        ) "Test 18: Set CELLULAR port $i Sim slot B"
done
