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

"""
Wrapper to trim and correct sequence data.
"""

import os
import os.path as op
import sys
import logging

from jcvi.formats.base import BaseFile, write_file, must_open
from jcvi.formats.fastq import guessoffset
from jcvi.utils.cbook import depends, human_size
from jcvi.apps.base import OptionParser, ActionDispatcher, download, \
            sh, mkdir, need_update, datadir


class FastQCdata (BaseFile, dict):

    def __init__(self, filename, human=False):
        super(FastQCdata, self).__init__(filename)
        if not op.exists(filename):
            logging.debug("File `{0}` not found.".format(filename))
            # Sample_RF37-1/RF37-1_GATCAG_L008_R2_fastqc =>
            # RF37-1_GATCAG_L008_R2
            self["Filename"] = op.basename(\
                    op.split(filename)[0]).rsplit("_", 1)[0]
            self["Total Sequences"] = self["Sequence length"] = \
                self["Total Bases"] = "na"
            return

        fp = open(filename)
        for row in fp:
            atoms = row.rstrip().split("\t")
            if atoms[0] in ("#", ">"):
                continue
            if len(atoms) != 2:
                continue

            a, b = atoms
            self[a] = b

        ts = self["Total Sequences"]
        sl = self["Sequence length"]
        if "-" in sl:
            a, b = sl.split("-")
            sl = (int(a) + int(b)) / 2
            if a == "30":
                sl = int(b)