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

# System modules
from astropy.time import Time
import json

# Custom modules
from devices import tel, foc, rot
from devices import dom, lamp, light
from devices import cam, cab
from utils.logger import log

from config.constants import frame_number, dir_number
from config.constants import frame_type, dir_type
from config.constants import imagetyp, dateobs

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")
