import numpy as np

def get_raw_data(writebox, spaces=False):

    f = open('trajectory.txt', 'r')
    row = f.readline()

    points = []
    for row in f:
        points.append(row.strip('\n').split(','))
    f.close()

    points = np.array(points, dtype='float')
    points = points[:, :2]

    # need to rotate the points
    theta = np.pi/2.
    R = np.array([[np.cos(theta), -np.sin(theta)],
                  [np.sin(theta), np.cos(theta)]])

    for ii in range(points.shape[0]):
        points[ii] = np.dot(R, points[ii])

    # need to mirror the x values
    for ii in range(points.shape[0]):
        points[ii, 0] *= -1

    # center numbers 
    points[:, 0] -= np.min(points[:,0])
    points[:, 1] -= np.min(points[:,1])

    # normalize 
    # TODO: solve weird scaling for 1 and l, and 9
    points[:, 0] /= max(points[:,0])
    points[:, 1] /= max(points[:,1])

    # center numbers 
    points[:, 0] -= .5 - (max(points[:,0]) - min(points[:, 0])) / 2.0

    points[:,0] *= 5.0 / 6.0 * (writebox[1] - writebox[0]) 
    points[:,1] *= (writebox[3] - writebox[2])

    points[:,0] += writebox[0]
    points[:,1] += writebox[2]

    return points 

def get_sequence(writebox, spaces=False):
    """Returns a sequence 

    writebox list: [min x, max x, min y, max y]
    """

    nans = np.array([np.nan, np.nan])
    nums= nans.copy()

    each_num_width = (writebox[1] - writebox[0])

    num_writebox = [writebox[0], 
                    writebox[0] + each_num_width, 
                    writebox[2], writebox[3]]
    num = get_raw_data(num_writebox)
    nums = np.vstack([nums, num, nans])

    return nums 
