#!/usr/bin/env python

from __future__ import absolute_import, print_function

import os
import sys
from lxml import etree

# if PAPARAZZI_HOME not set, then assume the tree containing this
# file is a reasonable substitute
PPRZ_HOME = os.getenv("PAPARAZZI_HOME", os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                                                      '../../..')))
# Class for all settings
class PaparazziACSettings:
    "Paparazzi Settings Class"
    ac_id = 0
    groups = []
    lookup = []
    name_lookup = {}
    # Takes a string file path for settings XML file and
    # returns a settings AC object

    def __init__(self, ac_id):
        self.ac_id = ac_id
        paparazzi_home = PPRZ_HOME
        paparazzi_conf = os.path.join(paparazzi_home, 'conf')
        conf_xml_path = os.path.join(paparazzi_conf, 'conf.xml')
        conf_tree = etree.parse(conf_xml_path)
        # extract aircraft node from conf.xml file
        ac_node = conf_tree.xpath('/conf/aircraft[@ac_id=%i]' % ac_id)
        if (len(ac_node) != 1):
            print("Aircraft ID %i not found." % ac_id)
            sys.exit(1)

        # save AC name for reference
        self.name = ac_node[0].attrib['name']

        # get settings.xml file path from var/aircrafts/<ac_name> directory
        settings_xml_path = os.path.join(paparazzi_home, 'var/aircrafts/' + self.name + '/settings.xml')

        if not os.path.isfile(settings_xml_path):
            print("Could not find %s, did you build it?" % settings_xml_path)
            sys.exit(1)

        index = 0 # keep track of index/id of setting starting at 0
        tree = etree.parse(settings_xml_path)

        for the_tab in tree.xpath("//dl_settings"):
            try:
                if 'NAME' in the_tab.attrib:
                    setting_group_name = the_tab.attrib['NAME']
                else:
                    setting_group_name = the_tab.attrib['name']
            except:
                #print("Could not read name of settings group")
                continue

            #print("parsing setting group:", setting_group_name)
            setting_group = PaparazziSettingsGroup(setting_group_name)

            for the_setting in the_tab.xpath('dl_setting'):
                try:
                    if 'shortname' in the_setting.attrib:
                        name = the_setting.attrib['shortname']
                    elif 'VAR' in the_setting.attrib:
                        name = the_setting.attrib['VAR']
                    else:
                        name = the_setting.attrib['var']
                except:
                    print("Could not get name for setting in group", setting_group)
                    continue

                settings = PaparazziSetting(name)
                settings.index = index

                try:
                    if 'MIN' in the_setting.attrib:
                        settings.min_value = float(the_setting.attrib['MIN'])
                    else:
                        settings.min_value = float(the_setting.attrib['min'])

                    if 'MAX' in the_setting.attrib:
                        settings.max_value = float(the_setting.attrib['MAX'])
                    else:
                        settings.max_value = float(the_setting.attrib['max'])

                    if 'STEP' in the_setting.attrib:
                        settings.step = float(the_setting.attrib['STEP'])
                    else:
                        settings.step = float(the_setting.attrib['step'])
                except:
                    print("Could not get min/max/step for setting", name)
                    continue

                if 'values' in the_setting.attrib:
                    settings.values = the_setting.attrib['values'].split('|')
                    count = int((settings.max_value - settings.min_value + settings.step) / settings.step)
                    if (len(settings.values) != count):
                        print("Warning: possibly wrong number of values (%i) for %s (expected %i)" % (len(settings.values), name, count))

                setting_group.member_list.append(settings)
                self.lookup.append(settings)
                self.name_lookup[name] = settings
                index = index + 1

            self.groups.append(setting_group)

    def GetACName(self):
        return self.name

# Class for named group of settings
class PaparazziSettingsGroup:
    "Paparazzi Setting Group Class"
    name = 0
    member_list = []

    def __init__(self, name):
        self.name = name
        self.member_list = []

# Class for a single paparazzi setting
class PaparazziSetting:
    "Paparazzi Setting Class"
    shortname = ""
    min_value = 0
    max_value = 1
    step = 1
    index = 0
    value = None
    values = None

    def __init__(self, shortname):
        self.shortname = shortname


def test():
    ac_id = 164
    ac_settings = PaparazziACSettings(ac_id)
    for setting_group in ac_settings.groups:
        print(setting_group.name)
    for setting in setting_group.member_list:
        print(" " + setting.shortname)

if __name__ == '__main__':
    test()
