from os.path import join
from tempfile import mkdtemp
from gzip import open as gzopen

from .processing_pipeline import StudyPreprocessor
from qiita_core.qiita_settings import qiita_config
from qiita_ware.commands import submit_EBI_from_files
from qiita_ware.demux import to_per_sample_ascii
from qiita_ware.util import open_file
from qiita_db.study import Study
from qiita_db.metadata_template import SampleTemplate, PrepTemplate
from qiita_db.data import PreprocessedData, RawData


def preprocessor(study_id, raw_data_id, param_id, param_constructor):
    """Dispatch for preprocessor work"""
    study = Study(study_id)
    raw_data = RawData(raw_data_id)
    params = param_constructor(param_id)

    sp = StudyPreprocessor()
    return sp(study, raw_data, params)


def submit_to_ebi(study_id):
    """Submit a study to EBI"""
    study = Study(study_id)
    st = SampleTemplate(study.sample_template)
    raw_data_id = study.preprocessed_data()[0]
    pt = PrepTemplate(raw_data_id)
    preprocessed_data = PreprocessedData(raw_data_id)
    investigation_type = RawData(raw_data_id).investigation_type

    demux = [path for path, ftype in preprocessed_data.get_filepaths()
             if ftype == 'preprocessed_demux'][0]

    tmp_dir = mkdtemp(prefix=qiita_config.working_dir)
    output_dir = tmp_dir + '_submission'

    samp_fp = join(tmp_dir, 'sample_metadata.txt')
    prep_fp = join(tmp_dir, 'prep_metadata.txt')

    st.to_file(samp_fp)
    pt.to_file(prep_fp)

    with open_file(demux) as demux_fh:
        for samp, iterator in to_per_sample_ascii(demux_fh, list(st)):
            with gzopen(join(tmp_dir, "%s.fastq.gz" % samp), 'w') as fh:
                for record in iterator:
                    fh.write(record)

    return submit_EBI_from_files(study_id, open(samp_fp), open(prep_fp),
                                 tmp_dir, output_dir, investigation_type,
                                 'ADD', True)

@@ -1033,24 +1033,201 @@
 aw_data_id)%0A
+%0A    state = preprocessed_data.submitted_to_insdc_status()%0A    if state in ('submitting', 'success'):%0A        raise ValueError(%22Cannot resubmit! Current state is: %25s%22 %25 state)%0A%0A
     investig
@@ -1918,61 +1918,192 @@
     
+p
 re
-turn submit_EBI_from_files(study_id, open(samp_fp),
+processed_data.update_insdc_status('submitting')%0A    study_acc, submission_acc = submit_EBI_from_files(study_id, open(samp_fp),%0A                                                     
  ope
@@ -2109,24 +2109,33 @@
 en(prep_fp),
+ tmp_dir,
 %0A           
@@ -2160,28 +2160,94 @@
     
-tmp_dir, output_dir,
+                     output_dir,%0A                                                     
  inv
@@ -2300,17 +2300,328 @@
     
+             
-'ADD', True)
+        'MODIFY', True)%0A%0A    if study_acc is None or submission_acc is None:%0A        preprocessed_data.update_insdc_status('failed')%0A    else:%0A        preprocessed_data.update_insdc_status('success', study_acc,%0A                                              submission_acc)%0A%0A    return study_acc, submission_acc
 %0A

update insdc status and accessions