from stkhelper.scenarioobject import ScenarioObject
from stkhelper.toolbox import TLE_Manager

from comtypes import COMError
from comtypes.gen import STKObjects

class Satellite(ScenarioObject):
    
    """
    
    A satellite object to be placed into the current scenario.
    
    Parameters:
        guardian(stkhelper.scenario.Scenario): Scenario for the satellite to be
            placed into.
        name(str): Name of the satellite.
        sscNumber(str): SSC Number of the satellite to use.
        startTime(str): Start time of the satellite propogation.
        stopTime(str): Stop time of the satellite propogation.
    
    """
    def __init__(self, guardian, name, sscNumber, startTime=None, stopTime=None):
        super().__init__(guardian,name)
        self.sscNumber = sscNumber
        self.name = name
        self.path = "*/Satellite/" + name
    
        self.root = guardian.guardian.root    
        TLE_Manager.GenerateTLE(self.root, str(sscNumber))
        self.tle = TLE_Manager.ParseTLE(str(sscNumber) + ".tle") 
        
        if startTime == None:
            startTime = self.guardian.reference.StartTime
        if stopTime == None:
            stopTime = self.guardian.reference.StopTime
            
        self.reference = self.root.CurrentScenario.Children.New(STKObjects.eSatellite, name)

        command = 'SetState */Satellite/' + self.name + ' TLE "' + \
                self.tle[0] + '" "' + self.tle[1] + \
                '" TimePeriod "' + \
                startTime + '" "' + \
                stopTime + '"'
        try:                                 
            self.root.ExecuteCommand(command)
        except COMError:
            raise RuntimeError("Failure to add satellite. Check formatting of TLE.")
            
    """
    
    Get access to another stkhelper.scenarioObject.ScenarioObject.
    
    Parameters:
        scenarioObject(stkhelper.scenarioObject.ScenarioObject): Object to compute
            access to.
    
    Returns:
        access: List of all access over the scenario time period.
        
    """
    def GetAccess(self,scenarioObject):
        self.root.BeginUpdate()
        access = super().GetAccess(scenarioObject)
        self.root.EndUpdate()
        
        return access
    
    """
    
    Calculates the power generated by solar panels over a given time period.
    
    Parameters:
        startTime(str): Start time of the calculation.
        endTime(str): Stop time of the calculation.
        timestep(int): Time step to be used (in seconds).
        radius(float): Bounding radius used for the simulation. Should be as 
            small as possible without clipping the model.
        outputPath(str): Absolute path of the file to be saved.
    
    """
    def GetPower(self,startTime,endTime,timestep,radius,outputPath):
        command = 'VO %s SolarPanel Visualization Radius On %f' % (self.path,radius)
        self.root.ExecuteCommand(command)
        command = 'VO %s SolarPanel Compute "%s" "%s" %i Power "%s"' % \
                    (self.path,startTime,endTime,timestep,outputPath)
                    
        self.root.ExecuteCommand(command)
        
    """
    
    Sets the model of the satellite in the scenario to a provided file (.dae + .anc
    or .mdl)
    
    Parameters:
        modelFile(str): Absolute path to the model file to be used.
    
    """
    def SetModel(self,modelFile):
        command = 'VO %s Model File "%s"' % (self.path,modelFile)
        self.root.ExecuteCommand(command)
        
    """
    
    Sets the attitude of the satellite. View full list of attitudes at:
    https://help.agi.com/stk/Subsystems/connectCmds/connectCmds.htm#cmd_SetAttitudeProfile.htm
    
    Parameters:
        profile(str): Attitude profile to be used.
    
    
    """
    def SetAttitude(self,profile):
        #TODO account for offset
        command = 'SetAttitude %s Profile %s Offset 0 0 0' % (self.path,profile)
        self.root.ExecuteCommand(command)