#!/bin/bash

NUM_OF_LED=""
FILE_PATH="/usr/sbin/mx-interface-mgmt"

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_led <num_of_led>"
        exit 1
fi

NUM_OF_LED=$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 led
result=$(bash $FILE_PATH led)
if [ "$result" == "Error: Don't support led" ]; then
        echo "This device don't support led"
        exit 1
fi

# Test 2: Invalid LED Port format 1
bash $FILE_PATH led 00 get_state >/dev/null 2>&1
test_fail_result $? "Test 2: Invalid LED Port format 1"

# Test 3: Invalid LED Port format 2
bash $FILE_PATH led abc get_state >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid LED Port format 2"

# Test 4: Invalid LED Port
bash $FILE_PATH led $NUM_OF_LED get_state >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid LED Port"

# Test 5: Invalid LED Port and State format 1
bash $FILE_PATH led 00 set_state 01 >/dev/null 2>&1
test_fail_result $? "Test 5: Invalid LED Port and State format 1"

# Test 6: Invalid LED Port and State format 2
bash $FILE_PATH led abc set_state def >/dev/null 2>&1
test_fail_result $? "Test 6: Invalid LED Port and State format 2"

# Test 7: Invalid LED State format 1
bash $FILE_PATH led 0 set_state 01 >/dev/null 2>&1
test_fail_result $? "Test 7: Invalid LED State format 1"

# Test 8: Invalid LED State format 2
bash $FILE_PATH led 0 set_state ab >/dev/null 2>&1
test_fail_result $? "Test 8: Invalid LED State format 2"

# Test 9: Get All LED port State
for ((i = 0; i < $NUM_OF_LED; ++i)); do
        result=$(bash $FILE_PATH led $i get_state)
        test_pass_result $(
                [[ "$result" == "on" ]] ||
                        [[ "$result" == "off" ]]
                echo $?
        ) "Test 9: Get LED port $i State"
done

# Test 10: Set All LED port ON
for ((i = 0; i < $NUM_OF_LED; ++i)); do
        bash $FILE_PATH led $i set_state on >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 10: Set LED port $i on"
                continue
        fi

        result=$(bash $FILE_PATH led $i get_state)
        test_pass_result $(
                [[ "$result" == "on" ]]
                echo $?
        ) "Test 10: Set LED port $i ON"
done

# Test 11: Set All LED port OFF
for ((i = 0; i < $NUM_OF_LED; ++i)); do
        bash $FILE_PATH led $i set_state off >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 11: Set LED port $i off"
                continue
        fi

        result=$(bash $FILE_PATH led $i get_state)
        test_pass_result $(
                [[ "$result" == "off" ]]
                echo $?
        ) "Test 11: Set LED port $i OFF"
done
