from aux_functions import network_functions as nf
from aux_functions import helper_functions as helper

#aoc logon comms
def aoc_logon():
        print("entered aoc logon")
        aoc_logon_result = {}
        logon_response = nf.anmail_logon()

        if (not(logon_response == -1)):
                #initial route ID
                route_id = logon_response['route_id']
                takeoff_included = logon_response['takeoff_included']
                land_included = logon_response['land_included']
                land_location = logon_response['land_location']
                land_base = logon_response['land_base']
                waypoints = logon_response['waypoints']
                num_waypoints = logon_response['num_waypoints']
                land_base = logon_response['land_base']
                
                parsed_logon_response = helper.parse_logon_response(logon_response)
                
                if (takeoff_included == "Yes"):
                        aoc_logon_result['takeoff_included'] = True
                else:
                        aoc_logon_result['takeoff_included'] = False
                
                if (land_included == "Yes"):
                        aoc_logon_result['land_included'] = True

                        for idx in range(3):
                                waypoints.append(land_location[idx])

                        waypoints.append(0) #placeholder for land time

                        num_waypoints = num_waypoints + 1

                        #save land information
                        helper.param_to_file("land_base", land_base)

                else:
                        aoc_logon_result['land_included'] = False
                
                #share data
                helper.param_to_file("route_id", route_id)
                aoc_logon_result['route_id'] = route_id
                aoc_logon_result['waypoints'] = waypoints
                aoc_logon_result['num_waypoints'] = num_waypoints
                aoc_logon_result['mission_changed'] = True
                aoc_logon_result['parsed_response'] = parsed_logon_response
        
        print("left aoc logon")
        
        return aoc_logon_result


#aoc logoff comms
def aoc_logoff():
        print("entered aoc logoff")
        nf.anmail_logoff()
        print("exit aoc logoff")


#aoc mission comms
def aoc_mission(drone_obj):
        print("entered aoc comms")

        #continue if there's internet connection
        if (nf.testInternetConnection()):

                #Placeholder for data share
                data2fc = {}

                #retrieve position estimate
                global_posn = drone_obj.recv_match(condition=None, type='GLOBAL_POSITION_INT', blocking=True)    
                posn_estimate = [(global_posn.lat / (10 ** 7)), (global_posn.lon / (10 ** 7)), (global_posn.alt / (10 ** 3))]

                #update tower on position
                data_to_tower = [(global_posn.lat / (10 ** 7)), (global_posn.lon / (10 ** 7)), (global_posn.alt / (10 ** 3)), (global_posn.vx / (10 ** 2)), (global_posn.vy / (10 ** 2)), (global_posn.vz / (10 ** 2))]
                update_response = nf.antower_update(data_to_tower)

                #check for updates to route
                route_id = helper.param_from_file("route_id")
                checkin_response = nf.anmail_checkin(route_id)

                if ((not(checkin_response == 0)) and (not(checkin_response == -1)) and (not(checkin_response == -2))):
                        waypoints = checkin_response['waypoints']
                        num_waypoints = checkin_response['num_waypoints']
                        route_id = checkin_response['route_id']
                        takeoff_included = checkin_response['takeoff_included']
                        land_included = checkin_response['land_included']
                        land_location = checkin_response['land_location']
                        land_base = checkin_response['land_base']
                        
                        parsed_checkin_response = helper.parse_logon_response(checkin_response)
                                                                        
                        if (takeoff_included == "Yes"):
                                data2fc['takeoff_included'] = True
                        else:
                                data2fc['takeoff_included'] = False
                        
                        if (land_included == "Yes"):
                                data2fc['land_included'] = True

                                for idx in range(3):
                                        waypoints.append(land_location[idx])

                                waypoints.append(0) #placeholder for land time

                                num_waypoints = num_waypoints + 1

                                #save land information
                                helper.param_to_file("land_base", land_base)
                        else:
                                data2fc['land_included'] = False

                        #share data
                        data2fc['waypoints'] = waypoints
                        data2fc['num_waypoints'] = num_waypoints
                        helper.param_to_file("route_id", route_id)
                        data2fc['mission_changed'] = True
                        data2fc['parsed_response'] = parsed_checkin_response
                        data2fc['compulsory_land'] = False
                        
                elif (checkin_response == 0):
                        data2fc['mission_changed'] = False
                        data2fc['compulsory_land'] = False
                
                elif ((checkin_response == -2) or (checkin_response == -1)): #compulsory land
                        data2fc['compulsory_land'] = True
                
                data2fc['posn_estimate'] = posn_estimate
                data2fc['traffic_data'] = update_response['traffic_data']

                #save latest aoc data to file
                helper.param_to_file("data2fc", data2fc)
                
        else:
                #retrieve last saved aoc data
                data2fc = helper.param_from_file("data2fc")

        print("leaving aoc comms")
        
        return data2fc
