#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# System modules
import json

# Third-party modules
from astropy.time import Time

# Custom modules
from config.constants import (dateobs, dir_number, dir_type, frame_number,
                              frame_type, imagetyp)
from devices import cab, cam, dom, foc, lamp, light, rot, tel
from utils.logger import log

offset = tel.offset
camall = cam.all
domaz = dom.azimuth
domop = dom.open
basic = cab.basic()

now = Time.now()

#####################
##### Init page #####
#####################

init_panel = {

    "Telescope status": {
        "status": tel.park,
        "cover": tel.cover,
    },

    "Telescope offsets": {
        "zd": offset[0],
        "az": offset[1],
        "rot": rot.position,
    },

    "Dome": {
        "az": dom.azimuth,
        "open": dom.open,
    },

    "Camera": {
        "fan": camall["CoolerPower"],
        "T": camall["CCDTemperature"],
        "amb": camall["AmbientTemperature"],
    },

}

########################
##### Control page #####
########################

control_panel = {

    "Telescope": {
        "ra": basic["radec"][0],
        "dec": basic["radec"][1],
        "alt": basic["altaz"][0],
        "az": basic["altaz"][1],
        "lst": basic["lst"],
        "moving": tel.is_moving,
        "tracking": tel.tracking,
        "cover": tel.cover,

    },

    "Dome": {
        "slaved": dom.slave,
        "az": domaz,
        "open": domop,
        "position": foc.position,
        "lamp": lamp.state,
        "light": light.state,
        "parked": dom.is_parked,
        "moving": dom.is_moving,
    },

    "Camera": {
        "filt": cam.is_moving,
        "status": cam.state,
        "fan": camall["CoolerPower"],
        "T": camall["CCDTemperature"],
        "amb": camall["AmbientTemperature"],
    },

}

dumps = {
    "init": init_panel,
    "control": control_panel
}

print(json.dumps(dumps))

log.debug(f"Total elapsed time: {(Time.now()-now).sec :.2f}s")
