import os
import sys
import argparse

from scai_utils import *

"""
speechROIs = ['H', 'PO', 'PP', 'PT', 'SPL', 'TP',  'SMA', 'aCG', 'aCO', \
              'aFO', 'aINS', 'aSTg', 'adSTs', \
              'aSMg', 'dIFo', 'dMC', 'dSC', 'mdPMC', 'midMC', 'midPMC', 'pCO', \
              'pFO', 'pIFs', 'pINS', 'aSTg', 'pSTg', 'pdPMC', 'pdSTs', \
              'preSMA', 'vIFo', 'vMC', 'vPMC', 'vSC', 'midCG']
"""

from aparc12_oldVer import get_aparc12_cort_rois
speechROIs = get_aparc12_cort_rois("all", bSpeech=True)

#speechROIs = ['midCG']

BIN = "/users/cais/STUT/scripts/aparc12_probtrackx_2.py"
RES_DIR = "/users/cais/STUT/analysis/aparc12_tracts_pt2"

if __name__ == "__main__":
    ap = argparse.ArgumentParser(description="Run aparc12_probtrackx_2.py under seedOnly, caww (corpus-callosum avoidance, ipsilateral WM waypoint) mode on all speech-network ROIs of a hemisphere of a subject")
    ap.add_argument("sID", help="Subject ID")
    ap.add_argument("hemi", help="Hemisphere (e.g., lh or lh,rh)")
    ap.add_argument("--noSurfProj", dest="bNoSurfProj", action="store_true", \
                    help="Do not perform projection to surface")
    ap.add_argument("--redo", dest="bRedo", action="store_true", \
                    help="Force redo all ROIs. If False, then the script will skip the ROIs that have already been completed.")
    
    if bpt2:
        TRACTS_RES_DIR = TRACTS_RES_DIR_PT2
    check_dir(TRACTS_RES_DIR)

    # Check sanity of input arguments
    if not (hemi == "lh" or hemi == "rh" or hemi == "xh"):
        raise Exception, "Unrecognized hemisphere: %s" % hemi

    if len(speechMode) > 6:
        if hemi != speechMode_noThr[-2 :]:
            raise Exception, "Mismatch between hemi=%s and speechMode=%s" \
                             % (hemi, speechMode)

    # Read cortical ROI list
    if speechMode == "speech":
        t_rois = get_aparc12_cort_rois("all", bSpeech=True)
    else:
        t_rois = get_aparc12_cort_rois("all", bSpeech=speechMode)
    t_rois.sort()
    
    h_rois = []
    if hemi == "lh" or hemi == "rh":    
        for t_roi in t_rois:
            h_rois.append(hemi + "_" + t_roi)
    elif hemi == "xh":
        hemis = ["lh", "rh"]
        for t_hemi in hemis:
            for t_roi in t_rois:
                h_rois.append(t_hemi + "_" + t_roi)

    # Preparation: check directories
    args = ap.parse_args()
    if args.bOldVer:
        from aparc12_oldVer import get_aparc12_cort_rois
    else:
        from aparc12 import get_aparc12_cort_rois

    check_dir(FSAV_DIR)
    check_file(APARC12_VOL)
    check_file(APARC12_CTAB)

    # Read color table
    (roi_nums, roi_names) = read_ctab(APARC12_CTAB)

    # Get the set of ROIs to process
    proc_rois_0 = get_aparc12_cort_rois(bSpeech=False)

    # Duplex the rois in to both hemispheres and get their numbers
    proc_roi_names = []
    proc_roi_nums = []

    for (i0, themi) in enumerate(hemis):
        for (i1, troi) in enumerate(proc_rois_0):
            troi_name = themi + "_" + troi
            proc_roi_names.append(troi_name)
        
            if roi_names.count(troi_name) == 0:
                raise Exception, "Cannot find ROI %s in color table %s" \
                                 % (troi_name, APARC12_CTAB)
            
            proc_roi_nums.append(roi_nums[roi_names.index(troi_name)])