#!/usr/bin/env python
import gui.views
import gui.framework
import data.models
import data.nasa
import threading
import time
import data.util

fetch = ['statex','statey','statez','statevx','statevy','statevz','statetime'
        ,'ku1status','vid1source']

class ISSConsole:

  def __init__(self):
  
    self.app      = gui.framework.Application()
    self.state    = data.models.ISSData()
    self.api      = data.nasa.ISSLive(fetch, True)
    
    self.view     = gui.views.MainWindow(self, self.state)
    self.app.main = self.view
    
  def run_api(self):
    self.api.start(self.handle_data)

  def fake_api(self):
    self.state.j2000_pos = [5426.331231,-4521.124543,0.1]
    self.state.j2000_vel = [-7214.331231,-4521.124543,-1234.567]
    self.state.state_time = data.util.gps2dt("11916/06:41:50")
    self.state.state_time_age = 12.4
    self.state.ku1_status = "DISABLED"
    self.state.ku1_source = "Some Camera"
    self.view.update_view_threadsafe()

  def handle_data(self, d):
    #print d
    self.state.j2000_pos[0]   = float(d['statex'])
    self.state.j2000_pos[1]   = float(d['statey'])
    self.state.j2000_pos[2]   = float(d['statez'])
    self.state.j2000_vel[0]   = float(d['statevx'])
    self.state.j2000_vel[1]   = float(d['statevy'])
    self.state.j2000_vel[2]   = float(d['statevz'])
    self.state.state_time     = data.util.gps2dt(d['statetime'])
    self.state.state_time_age = data.util.age(self.state.state_time)
    self.state.ku1_status     = d['ku1status']
    self.state.ku1_source     = data.nasa.JARGON[d['vid1source'].replace(' ','')]

    self.view.update_view_threadsafe()

  def start(self, dev=False):
    if dev:
      api_thread = threading.Thread(target=self.fake_api)
    else:
      api_thread = threading.Thread(target=self.run_api)
      
    api_thread.daemon = True
    api_thread.start()
    self.app.run()

if __name__ == "__main__":
  ISSConsole().start(dev=True)
  #ISSConsole().start()
