#!/usr/local/bin/python3.3

import cgi 
import os
import cgitb
import pymysql
import datetime
import subprocess
import DAPRfunctions as dapr

#dapr.loadConfiguration()

cookie = dapr.get_cookies()
db=dapr.DBConnection()

cgitb.enable()


if cookie:
    fname = cookie["fname"].value
    lname = cookie["lname"].value
    name = " ".join((fname, lname))
    admin = cookie["admin"].value
    uid = cookie["uid"].value
    uname = cookie["uname"].value
    ceid = cookie["ck_eid"].value
    
    dire = "/var/www/dapr_test/bedfiles/"
    
    form = cgi.FieldStorage()
    
    
    eid = form.getvalue("exp")
    if eid:
        cookie["ck_eid"]=eid
        print(cookie)
    else:
        if ceid != "0":
            eid=ceid
        else:
            eid_error_flag=True
            eid_error="there is no experiment set"
    
    
    geneFile = form.getvalue("file")
    #     sid = form.getvalue("study")
    
    if geneFile == None:
        message="there is no file"
        print("Location: upload_v0.2.py")
        print("Content-type: text/html\n\n")
    else:
        #print("Content-type: text/html\n\n")
        #print("there is a file")
        message="There is a file"

    
    def insertBEDfile(bid,name, location, des1, des2, des3, command, uname, cmid, wmid,peaknumber,colnames):
        query1 = """INSERT INTO BEDfile (bid,name, location, description1, description2, description3, 
        upload_date, bedtoolcommand, uploader, cmid, wmid,peaknumber,fieldnames)
                    VALUES (%s, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", %s, %s, %s,"%s")
                    """%(int(bid),name, location, des1, des2, des3, str(datetime.datetime.now())[:-7],
                         command, uname, int(cmid), int(wmid),int(peaknumber),colnames)
        db.runInsert(query1)
    
    def updateBEDAssignment(bid,eid,uid):
        query2 = """INSERT INTO ExperimentBEDfile(eid,bid)
              VALUES (%s,%s);"""%(eid,bid)
        #query3 = """ INSERT INTO ComputationalMethod (cmid, name, bid)
              #VALUES (%s, %s, %s)"""(cmid, cname, bid)
        #query4 = """ INSERT INTO WetlabMethod (bid, wimid, name)
              #VALUES (%s, %s, %s)"""(bid, wimid, cname)
        query5 = """ INSERT INTO UserBEDfile (uid, bid)
              VALUES (%s, %s)"""%(uid, bid)
        
        db.runInsert(query2)
        db.runInsert(query5)
        
    
    fileitem = form['file']
    
    givenname = form.getvalue("givenname")
    des1 = form.getvalue("des1")
    des2 = form.getvalue("des2")
    des3 = form.getvalue("des3")
    command = form.getvalue("command")
    
    cmid = form.getvalue('comp')
    wmid = form.getvalue('wet')
    sid = form.getvalue('study')
    plid = form.getvalue('plt')
    colnames = form.getvalue('BEDcols')
    
    
    if cmid == None:
        cmid = 0
    if wmid == None:
        wmid == 0
    
    
    colnamestr = None
    if colnames:
        colnamestr=",".join([ _.strip() for _ in colnames.strip().split('\n')])
    else:
        colnamestr="NULL"
    
    if des1 == None:
        des1 = "NULL"
    if des2 == None:
        des2 = "NULL"
    if des3 == None:
        des3 = "NULL"
    
    q5 = """select plid from Platform join Study using(plid) where sid="%s" Group by  plid order by date desc;""" %sid
    
    if fileitem.filename:
    
        if givenname == None: 
            (givenname,ext)=os.path.splitext(os.path.basename(fileitem.filename))        
        else:
            if len(givenname)<1:
                (givenname,ext)=os.path.splitext(os.path.basename(fileitem.filename))                
        
        q3="""SHOW TABLE STATUS where Name='BEDfile';"""
        nbid=db.runQueryFetchOne(q3)
        
        nbid = int(nbid[10])
        
        fn_new = givenname +"_"+ str(nbid)



        location = dire + fn_new + ".bed"
        location_temp= dire + fn_new+"_temp.bed"
        location_temp_eol = dire + fn_new + "_tempeol.bed"

        location_sorted = dire + fn_new + "_sorted.bed"
        
        ff=open(dire+"upload_logfile","w")

        
        open(location_temp, 'wb').write(fileitem.file.read()) #TODO: don't load the whole file into memory
        #location_temp_f=open(location_temp)
        
        #command_for_test = "head %s "%(location_temp)
                
        #split into 2 commands
        
        command_for_bedtools_sort = "sort -k 1,1 -k2,2n %s"%(location_temp)
        #command_for_bedtools_sort = "sortBed -i %s"%(location_temp)
        
        #f1=open(location_temp_eol,"wb")
        #subprocess.call(command_for_eol_conversion.split(" "), stdin=location_temp_f,stdout=f1) #can't split by space 
        #f1.close()
        
        #subprocess.call(command_for_test.split(" "), stdout=ff) #can't split by space 
        
        with open(location_sorted,"wb") as f2:
            subprocess.call(command_for_bedtools_sort.split(" "), stdout=f2) #can't split by space 
        f2.close()
        
        command_for_rm = "rm -f %s"%(location_temp_eol)
        subprocess.call(command_for_rm.split(" "), stdout=ff)
        
        #counting lines and adding ID
        #fileitem.file.seek(0)
        sorted_bed = open(location_sorted)
        sorted_bed_w_id = open(location,"w")

        id_prefix = "id_"+str(nbid)+"_"

        if sorted_bed:
            # It's an uploaded file; count lines
            linecount = 0
            while True:
                line = sorted_bed.readline()
                if not line: break
                
                linecount = linecount + 1
                line_spl = line.strip().split("\t")
                if len(line_spl)<4: 
                    #add id                
                    line_spl.append(id_prefix+ str(linecount))
                    sorted_bed_w_id.write("\t".join(line_spl)+"\n")
                else: 
                    sorted_bed_w_id.write(line)
                    
        
        sorted_bed_w_id.close()

        command_for_rm = "rm -f %s"%(location_sorted)
        subprocess.call(command_for_rm.split(" "), stdout=ff)

        ff.close()
        #linecount2=len(fileitem.file.readlines())
        
        #print("Content-type: text/html\n")
        #print("%s %s %s"%(eid,sid,plid))
        
        #TODO: validate that bed file meets constraints of genome
                
        if eid and sid:
            
            insertBEDfile(nbid,fn_new , location, des1, des2, des3, command,uname, cmid, wmid,linecount,colnamestr)
            
            updateBEDAssignment(nbid,eid, uid)
            
            #TODO use function to display success or error messages
            #print("Content-type: text/html\n")
            message="""  File was successfully uploaded, with %s lines Click OK to be directed to list of BED files.  """
            
            #change location on callback of message box
            print("Location: BEDfunctions.py?eid=%s"%eid)
            print("Content-type: text/html\n")

    else:
        #print("Content-type: text/html\n")
        message="""     File failed to upload, please try again     """         
else:
    print("Location: login.py")
    print("Content-type: text/html\n")

