#!/usr/bin/python
import sys
import psycopg2
import psycopg2.extras
sys.path.append("/home/keithg/allen/allensd")
import allensdk.core.json_utilities as json


def prep_json(spec_id):
    jin = {}

    try:
        conn_string = "host='limsdb2' dbname='lims2' user='atlasreader' password='atlasro'"
        conn = psycopg2.connect(conn_string)
        cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    except:
        print "unable to connect"
        raise

    ####################################################################
    # get polygons outlining layers in 20x image
    layer_sql = """
        select st.acronym, poly.path, wkf.storage_directory, wkf.filename, ims.id from image_series ims
        join sub_images si on si.image_series_id = ims.id
        join avg_graphic_objects layer on layer.sub_image_id = si.id
        join avg_graphic_objects poly on poly.parent_id = layer.id
        join avg_group_labels layert on layert.id = layer.group_label_id
        left join structures st on st.id = poly.structure_id
        join specimens hemisl on hemisl.id = ims.specimen_id
        join specimens cell on cell.parent_id = hemisl.id
        join neuron_reconstructions nr on nr.specimen_id = cell.id
        join well_known_files wkf on wkf.attachable_id = nr.id
        JOIN well_known_file_types wkft on wkft.id = wkf.well_known_file_type_id
        where nr.superseded is false
        and layert.name = 'Default'
        AND wkft.name = '3DNeuronReconstruction'
        and cell.id = %d 
        order by 1
    """
    cursor.execute(layer_sql % spec_id)
    #print layer_sql % spec_id
    poly_info = cursor.fetchall()
    if len(poly_info) == 0:
        print("Error -- cannot no polygon data for Specimen %d" % spec_id)
        sys.exit(1)
    poly = []
    for entry in poly_info:
        label = entry[0]
        path = entry[1]
        block = {}
        block["path"] = path
        block["label"] = label
        poly.append(block)
        ## break down string path into two numeric arrays
        #path_array = np.array(path.split(','))
        #path_x = np.array(path_array[0::2], dtype=float)
        #path_y = np.array(path_array[1::2], dtype=float)
        #block["path_array"] = path_array
        #block["path_x"] = path_x
        #block["path_y"] = path_y
        #poly[label] = block
    jin["layers"] = poly
    jin["storage_directory"] = poly_info[0][2]
    jin["swc_file"] = poly_info[0][3]

    # reconstruction ID
    # steal this from file name
    fname = jin["swc_file"].split("_m.swc")[0]
    reconstruction_id = int(fname[-9:])
    jin["reconstruction_id"] = reconstruction_id
    jin["resolution"] = 0.363


    # it appears that we need to restrict image query to use this ims_id
    ims_id = poly_info[0][4]

    ####################################################################
    # get soma outline
    soma_sql = """
        SELECT poly.path
        from image_series ims
        join sub_images si on si.image_series_id = ims.id 
        join avg_graphic_objects layer on layer.sub_image_id = si.id
        join avg_graphic_objects poly on poly.parent_id = layer.id
        join avg_group_labels layert on layert.id = layer.group_label_id
        left JOIN images im ON im.id=si.image_id
        left JOIN scans sc ON sc.image_id=im.id
        JOIN avg_group_labels agl ON layer.group_label_id=agl.id
        left join structures st on st.id = poly.structure_id
        join specimens hemisl on hemisl.id = ims.specimen_id
        join specimens cell on cell.parent_id = hemisl.id
        join neuron_reconstructions nr on nr.specimen_id = cell.id
        join well_known_files wkf on wkf.attachable_id = nr.id
        JOIN well_known_file_types wkft ON wkft.id = wkf.well_known_file_type_id AND wkft.name = '3DNeuronReconstruction'
        where nr.superseded is false
        and agl.name = 'Soma'
        and cell.id = %d
    """
    #print soma_sql % spec_id
    cursor.execute(soma_sql % spec_id)
    soma_res = cursor.fetchall()
    som = {}
    som["label"] = "Soma"
    som["path"] = soma_res[0]
    jin["soma"] = som

    ####################################################################
    # get 20x image
    img_sql = """
        SELECT ss.storage_directory, im.jp2 from image_series ims
        join sub_images si on si.image_series_id = ims.id 
        left JOIN images im ON im.id=si.image_id
        left JOIN specimens cell on ims.specimen_id = cell.id
        join slides ss on ss.id = im.slide_id
        where cell.id = %d
    """

    # get 20x image
    img_sql = """
        SELECT ss.storage_directory, im.jp2
        from image_series ims
        join sub_images si on si.image_series_id = ims.id 
        left JOIN images im ON im.id=si.image_id
        join slides ss on ss.id = im.slide_id
        and ims.id = %s
    """
    try:
        cursor.execute(img_sql % ims_id)
        #cursor.execute(img_sql % spec_id)
        img_res = cursor.fetchall()
        img_path = img_res[0][0] + img_res[0][1]
        #img_path = "%s-20x.jpeg" % str(spec_id)
    except:
        print "Error fetching path to 20x image from database"
        print img_sql % spec_id
        raise

    img = {}
    #img["img_res"] = img_res
    img["img_path"] = img_path
    jin["20x"] = img

    return jin

if __name__ == "__main__":
    spec_id = 490387590
    jin = prep_json(spec_id)
    print("Test mode: creating input.json for specimen.id=%d" % spec_id)
    json.write("input.json", jin)

