"""
Here goes a brief description of the clustering subpackage.
"""

import os
import shutil
import sys
from phybers.clustering.hclust import mainHClust
from phybers.clustering.ffclust import mainFFClust

pathname = os.path.dirname(__file__)
sys.path.insert(0,os.path.join(pathname,'hclust'))

def hclust(fiber_input,work_dir,MaxDistance_Threshold,var,PartDistance_Threshold):
    """!Documentation for the hclust function. The inputs for hclust are:
    <ol>
        <li>**fiber_input**: Tractography data file.</li>
        <li>**work_dir**: Directory to store all the results generated by the algorithm.</li>
        <li>**MaxDistance_Threshold**: Maximum distance threshold (in mm), default 40mm.</li>
        <li>**var**: A similarity scale (in mm), default 60mm.</li>
        <li>**PartDistance_Threshold**: Partition threshold (in mm), default 40mm.</li>
    </ol>
    """
    pass
    if os.path.exists(work_dir):
      shutil.rmtree(work_dir)
    
    os.makedirs(work_dir)
    
    #final_bundles_dir = work_dir + '/FinalBundles'
    final_bundles_dir = os.path.join(work_dir, 'FinalBundles')

    os.makedirs(final_bundles_dir, exist_ok=True)

    #final_bundles21p_dir = work_dir + '/FinalBundles21p'
    final_bundles21p_dir = os.path.join(work_dir, 'FinalBundles21p')    
    os.makedirs(final_bundles21p_dir, exist_ok=True)
    
    #final_centroids_dir = work_dir + '/FinalCentroids'    
    final_centroids_dir = os.path.join(work_dir, 'FinalCentroids') 
    os.makedirs(final_centroids_dir, exist_ok=True)
    
    #outfile_dir= work_dir + '/outfile'
    outfile_dir= os.path.join(work_dir, 'outfile')
    os.makedirs(outfile_dir, exist_ok=True)
    
    
    mainHClust.hierarchical(fiber_input,outfile_dir,str(MaxDistance_Threshold))
    mainHClust.particional_hierarchical(fiber_input,outfile_dir, PartDistance_Threshold,var,final_bundles_dir)
    
def ffclust(infile, output_directory, thr_assign=6, thr_join=6):
    """!Documentation for the FFclust function.
    The inputs for FFClust are:
    
    <ol>
        <li>**infile**: File with input tractography dataset.</li>
        <li>**output_directory**: Directory to store all the results generated by the algorithm.</li>
        <li>**thr_assign**: Maximum distance threshold for the cluster reassignment in mm, default: 6.</li>
        <li>**thr_join**: Maximum distance threshold for the cluster merge in mm, default: 6.</li>
    </ol>
    """
    mainFFClust.fastfiber(infile, output_directory, thr_assign, thr_join)
