#! /usr/bin/env python
'''Various utilities to load data saved by the POLY new output(s)'''
import sys
import utils
from moving import  TimeInterval
import numpy as np

__metaclass__ = type

# inputs variables	
#dirname= 'G:/0-phdstart/Code/trial/indicatorsNew/'
#extension= '-indicatorsNew.csv'
#indicatorsNames= {1:'Distance',2:'Cosine',3:'collision Course Angle',4:'Velocity Cosine',5:'Velocity Angle',6:'Speed Differential',7:'Collision Probability',8:'Severity Index',9:'TTC'}
''' min Distance case'''
dirname= 'G:/0-phdstart/Code/trial/minDistanceIndicator/'
extension= '-minDistanceInd.csv'
indicatorsNames= {1:'minDistance'}

def loadNewInteractions(videoFilename,interactionType, roaduserNum1,roaduserNum2, selectedIndicators=[]):
    '''Loads interactions from the POLY traffic event format'''
    from events import Interaction 
    from indicators import SeverityIndicator
    #filename= dirname + videoFilename + extension
    filename= dirname + interactionType+ '-' + videoFilename + extension # case of min distance todo: change the saving format to be matched with all outputs
    file = utils.openCheck(filename)
    if (not file):
        return []
    interactions = []
    interactionNum = 0
    data= np.loadtxt(filename)
    indicatorFrameNums= data[:,0]
    inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2) 
    inter.addVideoFilename(videoFilename)
    inter.addInteractionType(interactionType)
    for key in indicatorsNames.keys():
        values= {}
        for i,t in enumerate(indicatorFrameNums):
            values[t] = data[i,key]
        inter.addIndicator(SeverityIndicator(indicatorsNames[key], values))
    if selectedIndicators !=[]:
        values= {}
        for i,t in enumerate(indicatorFrameNums):
            values[t] = [data[i,index] for index in selectedIndicators]
        inter.addIndicator(SeverityIndicator('selectedIndicators', values))	
		
    interactions.append(inter)
    file.close()
    return interactions

