import sys, os
import shutil
import time
from datetime import datetime
from random import shuffle

from dakd.utils import initConf
from dakd.utils import basicLogger
from dakd.utils import create_logger
from dakd.utils import basicLoggerDecorator
from dakd.utils import load_plugin

conf = initConf()
extractLogger = create_logger(conf["logger_extract"])
plugin = load_plugin()


def fetchFiles():
    
    selectedFiles=[]
    finishedFiles=[]

    for line in open(conf["doneFilesList"]):
        try:
            thefile  = os.path.abspath(line.split(":")[0])
            if os.path.exists(thefile):
                finishedFiles.append(thefile)
            
        except:
            print("something went wrong!")
            continue

    print("finished files list:")
    print(finishedFiles)
    print("")


    #print(os.path.abspath('.'))
    for root, dirs, files in os.walk(conf["extract_fetchDir"]):
        #dirs.remove('bootcamp')
        if conf["extract_exclude"] in dirs:
            dirs.remove(conf["extract_exclude"])
        for name in files:
            if os.path.realpath(os.path.join(root,name)) not in finishedFiles:
                print(name)
                #print(dir(name))
                #requirements.append(lvl3([name])) 
                selectedFiles.append(os.path.join(root, name)) 
            
    # randomize list
    shuffle(selectedFiles)
    return selectedFiles

@basicLoggerDecorator(extractLogger)
def prepareFiles(elements, foutput):

    filepath = ""
    lvl2Path = "./lvl2/"
    lvl1Path = "./lvl1/"
    filename = ""
    destination = ""
    outputpath = "./planner/lvl2.txt"
 

    print("this are the files:")
    print(elements)
    print("---------------------------")
   
    for filey in elements:
        print("this is the file:")
        print(filey)
        filepath = filey

        if os.path.exists(filepath):

            # extract filename
            filename = os.path.basename(filepath)
            destination = os.path.join(lvl2Path,filename)

            # move/copy file
            #shutil.copy(filepath,destination)
            
            # write new path to output
            label= os.path.dirname(filepath)
            label = os.path.basename(label)
            foutput.write('{}:{}\n'.format(filepath,label))




@basicLoggerDecorator(extractLogger)
def prepareLvl3OutputPath(newfilepath):

    # prepare paths
    filepath = newfilepath.strip()
    filename = os.path.basename(filepath)

    fileout = os.path.join(conf["lvl3path"],filename)
    auxname = filename.split(".")
    auxname = filename[:filename.index(auxname[-1])]+conf["log_file_extension"]
    logout = os.path.join(conf["lvl3path"],auxname)
    logfile = os.path.join(conf["vagrantpath2"],conf["procmonlog"])

    return logout, fileout



@basicLoggerDecorator(extractLogger)
def runFileTest(original, fin, fout):
	
    for line in fin:

        filepath, label = line.split(":")

        hour = datetime.now().strftime('%H')
        print("hour control!")
        print(hour)
        print(label)
        print(not label.startswith("safe") and hour in ["9","10","11","12","13","14","15","17","18","19","20","21","22","23"])
        if not label.startswith("safe") and hour in ["9","10","11","12","13","14","15","17","18","19","20","21","22","23"]:
            # skip malilcious file during working hours!
            print("skipping!")
            #continue

        # perform work
        plugin.submit(filepath)
        plugin.status()
        plugin.download(conf["lvl3path"])


        # write output file (make sure next level)
        logout, fileout = prepareLvl3OutputPath(filepath)
        fileout = open(fout,'a')
        fileout.write("{}:{}:{}\n".format(filepath,logout,label))
        fileout.close()

        # erase line in fin (avoid processing again)
        thename = ""
        with original.open('r') as input:
            thename = input.name
            with open(thename+".new",'w') as output:
                for line2 in input:
                    if line!=line2:
                        output.write(line2)

        if os.path.exists(thename):
            original.remove()
            shutil.move(thename+".new",thename)

        time.sleep(1)



