"""Unit tests for E2Camera class.

Original author: Mark Zvilius, 20-July-2019
For: The Sexton Corp
"""
import time


def test_info(cam):
    """Test get camera info."""
    print(cam.build_info_command())
    (status,reply) = cam.command_info()
    print('status = %d' % (status))
    if status == 0:
        print('ip: %s, sn: %s' % (reply['eth_ip'], reply['sn']))
    else:
        print(reply)
    print()
    return status


def test_get_session(cam):
    """Test get camera session."""
    print(cam.build_session_command())
    (status,reply) = cam.command_get_session()
    print('get_session: %d' % (status))
    print(reply)
    print()
    return status


def test_quit_session(cam):
    """Test quit camera session."""
    print(cam.build_session_command('quit'))
    (status,reply) = cam.command_quit_session()
    print('quit_session: %d' % (status))
    print(reply)
    print()
    return status


def test_set_iso(cam):
    """Test set ISO."""
    print(cam.build_iso_command('Auto'))
    (status,reply) = cam.command_set_iso('Auto')
    print('iso: %d' % (status))
    print(reply)
    print()
    return status


def test_set_wb_mode(cam):
    """Test set white balance mode."""
    print(cam.build_wb_mode_command('Manual'))
    (status,reply) = cam.command_set_wb_mode('Manual')
    print('wb-mode: %d' % (status))
    print(reply)
    print()
    return status


def test_set_wb(cam):
    """Test set white balance."""
    print(cam.build_wb_command('6500'))
    (status,reply) = cam.command_set_wb('6500')
    print('wb: %d' % (status))
    print(reply)
    print()
    return status


def test_set_shutter(cam):
    """Test set shutter speed."""
    print(cam.build_shutter_command('1/100'))
    (status,reply) = cam.command_set_shutter('1/100')
    print('shutter: %d' % (status))
    print(reply)
    print()
    return status


def test_set_encoder(cam):
    """Test set video encoder."""
    print(cam.build_encoder_command('H.265'))
    (status,reply) = cam.command_set_encoder('H.265')
    print('encoder: %d' % (status))
    print(reply)
    print()
    return status


def test_stream_settings(cam):
    """Test stream settings."""
    settings = {'width':'640', 'height':'480', 'bitrate':'500000', 'fps':'30'}
    print(cam.build_stream_settings_command('stream1', settings))
    (status,reply) = cam.command_stream_settings('stream1', settings)
    print('stream-settings: %d' % (status))
    print(reply)
    print()
    return status


def test_camera_settings(cam):
    """Test camera settings that are persisted."""
    settings = {'mwb':'6500', 'iso':'800', 'shutter_time':'1/100', 'video_encoder':'ProRes%20422%20LT'}
    print(settings)
    (status,reply) = cam.camera_settings(settings)
    print('camera_settings: %d' % (status))
    print(reply)
    print()
    return status


def test_record_start(cam):
    """Test record-start."""
    print(cam.build_record_command('start'))
    (status,reply) = cam.command_record('start')
    print('record-start: %d' % (status))
    print(reply)
    print()
    return status


def test_record_stop(cam):
    """Test record-stop."""
    print(cam.build_record_command('stop'))
    (status,reply) = cam.command_record('stop')
    print('record-stop: %d' % (status))
    print(reply)
    print()
    return status


def test_set_commands(cam):
    test_set_iso(cam)
    test_set_wb_mode(cam)
    test_set_wb(cam)
    test_set_shutter(cam)
    test_set_encoder(cam)
    test_stream_settings(cam)
    test_camera_settings(cam)


def test_record(cam):
    status = test_record_start(cam)
    if status != 0: return

    print('... recording for 5 seconds ...')
    time.sleep(5)
    print()

    test_record_stop(cam)


def run_unit_tests(cam):
    if test_info(cam) != 0: return
    if test_get_session(cam) == 0:
        test_record_stop(cam)   # so that subsequent commands can work
        test_set_commands(cam)
        test_record(cam)
    test_quit_session(cam)
    return

