#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from func import arg
from func import exe_import
from func import exe_qc
from func import exe_to_bt
from func import exe_correct
from func import exe_mk_sval
from func import exe_buildTree
from func import exe_mergeTree
from func import exe_convert
from func import exe_bc_sort
from func import exe_export
from func import exe_demultiplex
from func import exe_annotate_header

'''
Current available commands
 - streamline
 - import
 - QC
 - correct
 - mk_sval
 - to_bt
 - buildTree
 - mergeTree
 - convert
 - bc_sort
 - export
 - demultiplex
 - annotate_header
'''

# def call_general_help():
#     print("\nCurrent available commands:",flush=True)
#     print("\n".join([" - streamline"," - import"," - qc"," - correct"," - mk_sval"," - to_bt"," - buildTree"," - mergeTree"," - convert"," - bc_sort"," - export"," - demultiplex"," - annotate_header"]),flush=True)

if __name__=="__main__":
    cmd=sys.argv[1]
    if cmd not in ["streamline","import","qc","correct","mk_sval","to_bt","buildTree","mergeTree","convert","bc_sort","export","demultiplex","annotate_header","-h"]:
        print("\nCommand '"+cmd+"' is not found.",flush=True)
        exit()

    # if cmd=="-h":
    #     call_general_help()
    #     exit()
    
    opt=arg.getargs(cmd)

    if cmd == "import":
        print("\nInterstellar: Read segmentation",flush=True)
        s=exe_import.settings_import(opt)
        s.settingGetter()
        baristaImport=exe_import.BARISTA_IMPORT(s)
        baristaImport.importFastq()
        # if s.simple:
        #     print("\nextraction method: Simple extraction\n",flush=True)
        #     baristaImport.simple_extract()
        # else:
        baristaImport.extractComponents()
        baristaImport.exportExtractedComponents()

    elif cmd=="qc":
        print("\nInterstellar: Quality filtering",flush=True)
        s=exe_qc.settings_qc(opt)
        s.settingGetter()
        obj_qc=exe_qc.BARISTA_QC(s)
        obj_qc.qualityCheck()

    elif cmd=="to_bt":
        print("\nInterstellar: Data reshaping for Bartender",flush=True)
        s=exe_to_bt.settings_to_bt(opt)
        s.settingGetter()
        obj_to_bt=exe_to_bt.BARISTA_TO_BT(s)
        obj_to_bt.to_bt()
        obj_to_bt.bartender()

    elif cmd=="correct":
        print("\nInterstellar: Error correction",flush=True)
        s=exe_correct.settings_correct(opt)
        s.settingGetter()
        obj_correct=exe_correct.BARISTA_CORRECT(s)
        obj_correct.importExtractedComponents()
        obj_correct.correct_component()

    elif cmd=="mk_sval":
        print("\nInterstellar: Sequence-to-value conversion",flush=True)
        s=exe_mk_sval.settings_make_s(opt)
        s.settingGetter()
        obj_s=exe_mk_sval.BARISTA_MAKE_S(s)
        obj_s.make_s_seq()
        obj_s.make_s_value()
            
    elif cmd=="buildTree":
        print("\nInterstellar: Value tree construction",flush=True)
        s=exe_buildTree.settings_buildTree(opt)
        s.settingGetter()
        baristaTree=exe_buildTree.BARISTA_BUILDTREE(s)
        baristaTree.buildTree()        

    elif cmd=="mergeTree":
        print("\nInterstellar: Merging value trees",flush=True)
        s=exe_mergeTree.settings_mergetree(opt)
        s.settingGetter()
        baristaMergetree=exe_mergeTree.BARISTA_MERGETREE(s)
        baristaMergetree.mergeTree_v2()

    elif cmd=="convert":
        print("\nInterstellar: Optimized value assignment",flush=True)
        s=exe_convert.settings_convert(opt)
        s.settingGetter()
        baristaConvert=exe_convert.BARISTA_CONVERT(s)
        baristaConvert.convert()

    elif cmd=="bc_sort":
        print("\nInterstellar bc_sort",flush=True)
        barista_bc_sort=exe_bc_sort.BARISTA_BC_SORT(opt)
        barista_bc_sort.bc_sort_settingGetter()
        barista_bc_sort.bc_sort()

    elif cmd=="export":
        print("\nInterstellar export",flush=True)
        s=exe_export.settings_export(opt)
        s.settingGetter()

        baristaExport=exe_export.BARISTA_EXPORT(s)
        baristaExport.generateReferences()
        baristaExport.exportSequence()

    elif cmd=="demultiplex":
        print("\nInterstellar demultiplex",flush=True)
        s=exe_demultiplex.settings_demultiplex(opt)
        s.settingGetter()

        baristaDemulti=exe_demultiplex.BARISTA_DEMULTIPLEX(s)
        baristaDemulti.demultiplex()

    elif cmd=="annotate_header":
        print("\nInterstellar annotate_header",flush=True)
        s=exe_annotate_header.settings_annotate_header(opt)
        s.settingGetter()

        batistaannotate_header=exe_annotate_header.BARISTA_annotate_header(s)
        batistaannotate_header.annotate_header()


    # print("DONE!",flush=True)
