#!/usr/bin/python
import argparse

from astropy import units as u
from astropy.coordinates import SkyCoord

import astroscope.telescopes.local_telescopes


def main():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-d",
                       help="Port telescope is connected to."
                            "Default = /dev/ttyUSB0")
    group.add_argument("--get_azalt", action="store_true",
                       help="Display the current values of the telescope's "
                            "Aziumth (as) and Altitude (alt) obtained by "
                            "creating a SkyCoord object from astorpy library.")
    group.add_argument("--get_azalt_info", action="store_true",
                       help="Displays the string representaion of the SkyCoord"
                            " Astropy object created with the az and atl"
                            " reported by the telescope.")
    group.add_argument("--get_az_alt", action="store_true",
                       help="Displays the values of az and alt as reported"
                            " by the telescope.")
    group.add_argument("--move_az_by", nargs=1, metavar="degrees",
                       help="Moves the telescope in the Azimuth (az) axis"
                            " by the amount provided in degrees.")
    group.add_argument("--move_alt_by", nargs=1, metavar="degrees",
                       help="Moves the telescope in the Alitude (alt) axis"
                            " by the amount provided in degrees.")
    group.add_argument("--get_location", action="store_true",
                       help=" Displays the latitude and longitude of the "
                            "telescope location.")
    group.add_argument("--get_earth_location", action="store_true",
                       help="Displays EarthLocation Astropy object "
                            "representing the location of the telescope.")
    group.add_argument("--get_model", action="store_true",
                       help=" Returns model of telescope.")
    group.add_argument("--get_radec", action="store_true",
                       help="Displays Right Ascention (ra) and "
                            "Declincation (dec) telescope is pointing to. It "
                            "is obtained by creating a SkyCoord Astropy object"
                            " from the Azimuth, Altitude, and Earth Location "
                            "reported by the telescope.")
    group.add_argument("--get_ra_dec", action="store_true",
                       help="Displays the Right Ascnetion (ra) and "
                            "Declination (dec) reported by the telescope.")
    group.add_argument("--get_time", action="store_true",
                       help="Displays value of Atropy Time object "
                            "created by using the time reported by the"
                            " telescope and the EarthLocation derived from"
                            " latitude and longitude reported by telescope.")
    group.add_argument("--get_tracking_mode", action="store_true",
                       help="Displays the tracking mode value reported"
                            " by the telescope.")
    group.add_argument("--get_version", action="store_true",
                       help="Displays the software version of the"
                            "telescope.")
    group.add_argument("--set_location", nargs=2,
                       metavar=("latitude", "longitude"),
                       help="Sets the latitude and longitude on the "
                            "telescope.")
    group.add_argument("--goto_az_alt", nargs=2, metavar=("az", "alt"),
                       help="Sets the azimuth and altitude of the "
                            "telescope. --goto_az_alt float float")
    group.add_argument("--goto_in_progress", action="store_true",
                       help="reports back weather or not there is an "
                            "operation in progress on the telescope. "
                            "For example, if the telescope is currently"
                            " moving.")
    group.add_argument("--goto_radec", nargs=2, metavar=("Ra", "Dec"),
                       help="Points the telescope to the given degrees of "
                            "Right Ascention (ra) and Declination (dec).")
    group.add_argument("--set_tracking_mode", metavar="tracking_mode",
                       help="Sets the tracking mode of the telescope to the"
                            " value provided.")
    group.add_argument("--set_time", help="Sets the time on the telescope.")
    group.add_argument("--cancel_goto", action="store_true",
                       help="Cancels any 'goto' operation the telescope "
                            "is currently executing.")
    group.add_argument("--cancel_current_operation", action="store_true",
                       help="Cancels any operation the telescope is "
                            "currently executing.")
    group.add_argument("--alignment_complete", action="store_true",
                       help="Reports back weather or not the telescope "
                            "is aligned.")
    group.add_argument("--echo", help="Displays provided text on "
                                      "the telescope's display.")
    group.add_argument("--slew_fixed", nargs=2, metavar=("az_rate", "el_rate"))
    group.add_argument("--slew_var", nargs=2, metavar=("az_rate", "el_rate"))
    group.add_argument("--sync", nargs=2, metavar=("ra", "dec"))

    args = parser.parse_args()

    if args.d:
        device = args.d
    else:
        device = '/dev/ttyUSB0'

    telescope = astroscope.telescopes.local_telescopes.AstropyNexStarSLT130(device)

    if args.get_ra_dec:
        print (telescope.get_ra_dec())
    elif args.get_azalt_info:
        _altaz = telescope.get_azalt()
        print(_altaz)
    elif args.get_azalt:
        _altaz = telescope.get_azalt()
        print("alt: " + telescope.dec_to_degrees(_altaz.alt.deg))
        print(" az: " + telescope.dec_to_degrees(_altaz.az.deg))
    elif args.get_az_alt:
        print (telescope.get_az_alt())
    elif args.get_location:
        _earth_location = telescope.get_earth_location()
        print(
            " lat: " + telescope.dec_to_degrees(_earth_location.latitude.deg))
        print(
            "long: " + telescope.dec_to_degrees(_earth_location.longitude.deg))
    elif args.get_earth_location:
        print(telescope.get_earth_location())
    elif args.get_radec:
        _altaz = telescope.get_azalt()
        _radec = _altaz.transform_to('icrs')
        print(" ra:" + str(_radec.ra.hms))
        print("dec: " + str(_radec.dec.deg))
    elif args.get_tracking_mode:
        print(telescope.get_tracking_mode())
    elif args.get_version:
        print(telescope.get_version())
    elif args.get_time:
        print(telescope.get_time())
    elif args.set_location:
        latitude = args.set_location[0]
        longitude = args.set_location[1]
        telescope.set_location(
            map(int, longitude.split(",")),
            map(int, latitude.split(","))
        )
    elif args.set_time:
        print("broken!!!!!")
    elif args.set_tracking_mode:
        telescope.set_tracking_mode(int(args.set_tracking_mode))
    elif args.goto_az_alt:
        _az = float(args.goto_az_alt[0])
        _alt = float(args.goto_az_alt[1])
        telescope.goto_az_alt(_az, _alt)
    elif args.goto_in_progress:
        if telescope.goto_in_progress():
            print("Yes")
        else:
            print("No")
    elif args.goto_radec:
        _ra = float(args.goto_radec[0])
        _dec = float(args.goto_radec[1])
        _radec = SkyCoord(ra=_ra * u.deg, dec=_dec * u.deg, frame="icrs")
        telescope.goto_radec(_radec)
    elif args.alignment_complete:
        if telescope.alignment_complete():
            print("Yes")
        else:
            print("No")
    elif args.get_model:
        print(telescope.get_model())
    elif args.cancel_goto:
        telescope.cancel_goto()
    elif args.cancel_current_operation:
        telescope.cancel_current_operation()
    elif args.echo:
        telescope.echo(args.echo)
    elif args.slew_fixed:
        _az_rate = float(args.slew_fixed[0])
        _el_rate = float(args.slew_fixed[1])
        telescope.slew_fixed(_az_rate, _el_rate)
    elif args.slew_var:
        _az_rate = float(args.slew_var[0])
        _el_rate = float(args.slew_var[1])
        telescope.slew_var(_az_rate, _el_rate)
    elif args.sync:
        _ra = float(args.sync[0])
        _dec = float(args.sync[1])
        telescope.sync(_ra, _dec)
    elif args.move_alt_by:
        telescope.move_alt_by(args.move_alt_by[0])
    elif args.move_az_by:
        telescope.move_az_by(args.move_az_by[0])

    else:
        print(parser.print_help())


if __name__ == '__main__':
    main()
