"""Testing Zcam E2 communication.

This is the detailed info.
"""
__author__ = "Mark Zvilius"

import sys
import requests
import time
from datetime import datetime

SLAVE_CAMERA_IP = "10.6.10.162"
MASTER_CAMERA_IP = "10.6.10.161"

ISO_VALUES = [
    '500', '640', '800', '1000', '1250', '1600', '2000', '2500', '3200', '4000',
    '5000', '6400', '8000', '10000', '12800', '16000', '20000', '25600', '32000',
    '40000', '51200', '64000', '80000', '102400']

def camera_query(ip, query, payload=None, hide=False):
    """ Send HTTP GET to camera and receive reply.
    
    ip -- camera ip address
    query -- query
    payload -- key-value pairs for parameters
    """

    url = "http://%s/%s" % (ip, query)
    response = requests.get(url, params=payload)
    if not hide:
        print("%s --> status %d" % (response.url, response.status_code))
    body = response.json()
    return body

def query_and_print(ip, query, payload=None):
    print()
    body = camera_query(ip, query, payload)
    print(body)

def set_and_print(ip, key, val, timing=False):
    if timing:
        t0 = datetime.now()
    query_and_print(ip, 'ctrl/set', {key: val})
    if timing:
        elapsed = datetime.now() - t0
        secs = elapsed.total_seconds()
        print(f'-- elapsed: {secs:.2f} secs')

def get_and_print(ip, key):
    query_and_print(ip, 'ctrl/get', {'k': key})

def wait_for_setting(ip, key, val):
    for i in range(100):
        body = camera_query(ip, 'ctrl/get', {'k': key}, hide=True)
        if body['value'] == val:
            break
        time.sleep(0.1)
    got = body['value']
    print(f'after {i} got {got}')

def print_camera_info(body):
    print_kv(body,'model')
    print_kv(body,'number')
    print_kv(body,'sw')
    print_kv(body,'hw')
    print_kv(body,'mac')
    print_kv(body,'eth_ip')
    print_kv(body,'sn')
    print()

def print_kv(body,key):
    print(f'{key}={body[key]}')

if (__name__ == "__main__"):
    print("hello world")
    master = MASTER_CAMERA_IP
    slave = SLAVE_CAMERA_IP
    write = False
    if (len(sys.argv) > 1):
        write = (sys.argv[1] == 'w')

    body = camera_query(slave, "info")
    print_camera_info(body)
    # i=0
    # while 1:
    #     i = i+1
    #     print(i)
    #     camera_query(ip, "info")
    #     print()
    #     time.sleep(1)

    if write:
        get_and_print(master, 'focus')
        get_and_print(master, 'caf')
        get_and_print(master, 'restore_lens_pos')
        get_and_print(master, 'lens_focus_pos')

        query_and_print(master, 'ctrl/session')

        set_and_print(master, 'focus', 'AF')
        set_and_print(master, 'caf', 'Off')
        set_and_print(master, 'restore_lens_pos', 'Enable')
        set_and_print(master, 'lens_focus_pos', 300)

        query_and_print(master, 'ctrl/session', {'action': 'quit'})

    get_and_print(master, 'focus')
    get_and_print(master, 'caf')
    get_and_print(master, 'restore_lens_pos')
    get_and_print(master, 'lens_focus_pos')

    print()
    print("goodbye world")
