# XML_Constructor

# Imports
import xml.etree.cElementTree as ET

# Setup XML Processing Instructions
#TODO: Add processing instructions

# Placeholders
Activity_ID = '2015-01-20T13:26:30.000Z' #The StartTime timestamp
Lap_Duration = '419.855' #Total duration of Lap in seconds
Lap_Distance = '1000.0' #Total distance of lap in meters

################################################################################

# TrainingCenterDatabase
TrainingCenterDatabase = ET.Element('TrainingCenterDatabase')
TrainingCenterDatabase.set('xsi:schemaLocation','http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd')
TrainingCenterDatabase.set('xmlns', 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2')
TrainingCenterDatabase.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')

## Activities
Activities = ET.SubElement(TrainingCenterDatabase,'Activities')

### Activity
Activity = ET.SubElement(Activities,'Activity')
Activity.set('Sport','Running') #TODO: Make an option to change sports?
Id = ET.SubElement(Activity,'Id')
Id.text = Activity_ID

#### Lap
#TODO: Work out how to split exercises up into laps (by distance?)
Lap = ET.SubElement(Activity,'Lap')
Lap.set('StartTime',Activity_ID) #TODO: Rename this coincidental variable
TotalTimeSeconds = ET.SubElement(Lap,'TotalTimeSeconds')
TotalTimeSeconds.text = Lap_Duration
DistanceMeters = ET.SubElement(Lap,'DistanceMeters')
DistanceMeters.text = Lap_Distance
Calories = ET.SubElement(Lap,'Calories')
Calories.text = '0' #TODO: Can we nullify or get rid of this?
Intensity = ET.SubElement(Lap,'Intensity')
Intensity.text = 'Active' #TODO: Can we nullify or get rid of this?
TriggerMethod = ET.SubElement(Lap,'TriggerMethod')
TriggerMethod.text = 'Manual' #TODO: How are Laps (or Tracks?) split?

##### Track
#TODO: Loop through data array generated by parsing the Huawei HiTrack file in
#order to properly populate this section
Track = ET.SubElement(Lap,'Track')
Trackpoint = ET.SubElement(Track,'Trackpoint')
Time = ET.SubElement(Trackpoint,'Time')
Time.text = '2015-01-20T13:26:30.000Z'
Position = ET.SubElement(Trackpoint,'Position')
LatitudeDegrees = ET.SubElement(Position,'LatitudeDegrees')
LatitudeDegrees.text = '25.06334876641631'
LongitudeDegrees = ET.SubElement(Position,'LongitudeDegrees')
LongitudeDegrees.text = '121.6330941952765'
AltitudeMeters = ET.SubElement(Trackpoint,'AltitudeMeters')
AltitudeMeters.text = '0.0'
#TODO: Some (all?) Huawei devices don't collect Altitude data, but in that case
#can we call on some open API to estimate it?
DistanceMeters = ET.SubElement(Trackpoint,'DistanceMeters')
DistanceMeters.text = '0.0'
#TODO: Again, some (all?) Huawei devices don't calcualte distance between points
#in the HiTrack file. Do we need this distance, and if so can we calculate it?

#TODO: Work out why this invalidates the tcx file (Reason: character data between child elements not allowed!)
#HeartRateBpm = ET.SubElement(Trackpoint,'HeartRateBpm')
#HeartRateBpm.text = '84'

#### Creator
#TODO: See if we can scrape this data from other files in the .tar
Creator = ET.SubElement(Activity,'Creator')
Creator.set('xsi:type','Device_t')
Name = ET.SubElement(Creator,'Name')
Name.text = 'Huawei Fitness Tracking Device'
UnitId = ET.SubElement(Creator,'UnitId')
UnitId.text = '0000000000'
ProductID = ET.SubElement(Creator,'ProductID')
ProductID.text = '0000'
Version = ET.SubElement(Creator,'Version')
VersionMajor = ET.SubElement(Version,'VersionMajor')
VersionMajor.text = '0'
VersionMinor = ET.SubElement(Version,'VersionMinor')
VersionMinor.text = '0'
BuildMajor = ET.SubElement(Version,'BuildMajor')
BuildMajor.text = '0'
BuildMinor = ET.SubElement(Version,'BuildMinor')
BuildMinor.text = '0'

## Author
Author = ET.SubElement(TrainingCenterDatabase,'Author')
Author.set('xsi:type','Application_t') #TODO: Check this is right
Name = ET.SubElement(Author,'Name')
Name.text = 'Huawei_TCX_Converter 0.0'
Build = ET.SubElement(Author,'Build')
Version = ET.SubElement(Build,'Version')
VersionMajor = ET.SubElement(Version,'VersionMajor')
VersionMajor.text = '0'
VersionMinor = ET.SubElement(Version,'VersionMinor')
VersionMinor.text = '0'
BuildMajor = ET.SubElement(Version,'BuildMajor')
BuildMajor.text = '0'
BuildMinor = ET.SubElement(Version,'BuildMinor')
BuildMinor.text = '0'
LangID = ET.SubElement(Author,'LangID')
LangID.text = 'en' #TODO: Translations? Probably not...
PartNumber = ET.SubElement(Author,'PartNumber')
PartNumber.text = '000-00000-00'

################################################################################
def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i
################################################################################

tree = ET.ElementTree(TrainingCenterDatabase)
indent(TrainingCenterDatabase)
with open('test.tcx', 'wb') as f:
    f.write('<?xml version="1.0" encoding="UTF-8"?>'.encode('utf8'))
    tree.write(f, 'utf-8')
