import numpy as np
from .geometry import path
from astropy import coordinates
from astropy import units as u

csystems = {'galactic':coordinates.Galactic,
            'fk5':coordinates.FK5,
            'fk4':coordinates.FK4,
            'icrs':coordinates.ICRS}

valid_regions = ['line','segment']

def paths_from_regfile(regfile, wcs=None):
    """
    Given a ds9 region file, extract pv diagrams for each:
        group of points [NOT IMPLEMENTED]
        panda [NOT IMPLEMENTED]
        vector [NOT IMPLEMENTED]
        segment [NOT IMPLEMENTED]
        group of lines [NOT IMPLEMENTED]
    """
    import pyregion
    regions = pyregion.open(regfile)
    return paths_from_regions(regions, wcs=wcs)

def paths_from_regions(regions, wcs=None):
    paths = [path_from_region(r, wcs=wcs)
             for r in regions
             if r.name in valid_regions]
    return paths


def path_from_region(region, wcs=None):
    """
    Given a pyregion shape object, extract a pv diagram
    """

    l,b = None,None

    endpoints = []
    otherpars = []

    for x in region.params:
        if 'HMS' in str(type(x)) or 'Number' in str(type(x)) and l is None:
            if region.coord_format == 'celestial':
                l = x.degree
            else:
                l = x.v
        elif 'DMS' in str(type(x)) or 'Number' in str(type(x)):
            b = x.v
            if l is not None and b is not None:
                endpoints.append((l,b))
                l,b = None,None
            else:
                raise ValueError("unmatched l,b")
        else:
            otherpars.append(x)

    lbarr = np.array(endpoints)
    C = csystems[region.coord_format](lbarr[:,0]*u.deg, lbarr[:,1]*u.deg)

    # TODO: add widths for projection

    p = path.WCSPath(C, wcs=wcs)

    return p

def vector_to_path(vector_region):
    """
    Convert a vector region to a path

    # vector(48.944348,-0.36432694,485.647",124.082) vector=1
    """

    #start = coordinatesvector.coord_list
    pass


@@ -260,728 +260,91 @@
 S%7D%0A%0A
-valid_regions = %5B'line','segment'%5D%0A%0Adef paths_from_regfile(regfile, wcs=None):%0A    %22%22%22%0A    Given a ds9 region file, extract pv diagrams for each:%0A        group of points %5BNOT IMPLEMENTED%5D%0A        panda %5BNOT IMPLEMENTED%5D%0A        vector %5BNOT IMPLEMENTED%5D%0A        segment %5BNOT IMPLEMENTED%5D%0A        group of lines %5BNOT IMPLEMENTED%5D%0A    %22%22%22%0A    import pyregion%0A    regions = pyregion.open(regfile)%0A    return paths_from_regions(regions, wcs=wcs)%0A%0Adef paths_from_regions(regions, wcs=None):%0A    paths = %5Bpath_from_region(r, wcs=wcs)%0A             for r in regions%0A             if r.name in valid_regions%5D%0A    return paths%0A%0A%0Adef path_from_region(region, wcs=None):%0A    %22%22%22%0A    Given a pyregion shape object, extract a pv diagram
+def line_to_path(region, wcs=None):%0A    %22%22%22%0A    Convert a line or segment to a path
 %0A   
@@ -1180,16 +1180,26 @@
 r_region
+, wcs=None
 ):%0A    %22
@@ -1319,52 +1319,1249 @@
     
-#start = coordinatesvector.coord_list%0A    pass%0A
+x,y = vector_region.coord_list%5B:2%5D%0A    length = vector_region.coord_list%5B2%5D * u.deg%0A    angle = vector_region.coord_list%5B3%5D * u.deg%0A    %0A    C1 = csystems%5Bvector_region.coord_format%5D(x*u.deg, y*u.deg)%0A    tan = np.tan(angle)%0A    dx,dy = length * tan, length / tan%0A    C2 = csystems%5Bvector_region.coord_format%5D(C1.lonangle + dx, C1.latangle + dy)%0A%0A    C = csystems%5Bvector_region.coord_format%5D(%5BC1.lonangle,C2.lonangle%5D,%0A                                             %5BC1.latangle,C2.latangle%5D)%0A%0A    p = path.WCSPath(C, wcs=wcs)%0A%0A    return p%0A%0Aregion_converters = %7B'line':line_to_path, 'segment':line_to_path,%0A                     'vector':vector_to_path%7D%0A%0Adef paths_from_regfile(regfile, wcs=None):%0A    %22%22%22%0A    Given a ds9 region file, extract pv diagrams for each:%0A        group of points %5BNOT IMPLEMENTED%5D%0A        panda %5BNOT IMPLEMENTED%5D%0A        vector %5BNOT IMPLEMENTED%5D%0A        segment %5BNOT IMPLEMENTED%5D%0A        group of lines %5BNOT IMPLEMENTED%5D%0A    %22%22%22%0A    import pyregion%0A    regions = pyregion.open(regfile)%0A    return paths_from_regions(regions, wcs=wcs)%0A%0Adef paths_from_regions(regions, wcs=None):%0A    paths = %5Bregion_converters%5Br.name%5D(r, wcs=wcs)%0A             for r in regions%0A             if r.name in region_converters%5D%0A    return paths
 %0A

add vector handler, slight refactor