import os
import sys
import datetime
import subprocess
import time

class LoC:
    def __init__(self, repoPath, author, date1, date2):
        # init info, won't change after init
        self.workDir = os.getcwd() # keep run path
        self.repoPath = repoPath
        self.nowTimeStr = self.getNowTimeStr();

        # input info
        self.author = author;
        self.startDate = date1
        self.endDate = date2
        self.dateRange = self.getDateRange(self.startDate, self.endDate)

        # output
        self.outDirPrefix = "./static/queryResult/"
        self.outFolder = self.getOutFolderName()
        self.outFolderFullPath = self.outDirPrefix + self.outFolder

        # *.log
        self.logName = self.getLogFileName()
        self.logFullPath = self.outDirPrefix + self.logName
        #os.makedirs(self.outDirPrefix)

        # index.html
        self.resultPage = self.outFolderFullPath + "index.html"

    def setWorkDir(self):
        os.chdir(self.repoPath)

    def resetWorkDir(self):
        os.chdir(self.workDir)

    #TODO: user + range, is the uuid
    def getNowTimeStr(self):
        timeStr = str(datetime.datetime.now()).split(' ');
        return timeStr[0] + timeStr[1].replace(':','-')

    def getOutFolderName(self):
        return self.author + "." + self.startDate + '----' + self.endDate + "/"

    def getLogFileName(self):
        return self.outFolder[0:-1] + ".log"

    def getDateRange(self, date1, date2):
        return "{" + date1 + "}:{" + date2 + "}"

    def getLogCmdStr(self):
        svn = "svn.exe"
        para0 = "log"
        para1 = "-r"
        para2 = "--search"
        dateRange = self.dateRange
        author = self.author
        v_ = "-v"
        xml_ = "--xml"

        cmd = [svn, para0, para1, dateRange, para2, author, v_, xml_]

        return cmd

    def callSvnLog(self):
        logfile = open(self.logFullPath, "w")

        self.setWorkDir()
        cmd = self.getLogCmdStr()
        subprocess.call(cmd, stdout=logfile)

    def getStatCmdStr(self):
        java = "java.exe"
        jar = "-jar"
        jarPath = "statsvn.jar"
        #include = "-include"
        #includeF = "**/*.c:**/*.cpp"
        out = "-output-dir"
        outPath = self.outDirPrefix + self.outFolder
        print '=================='
        print 'outPath:', outPath
        print '=================='
        
        

        cmd = [java, jar, jarPath, out, outPath, self.logFullPath, self.repoPath]

        return cmd

    def callStatSvn(self):
        self.resetWorkDir()
        cmd = self.getStatCmdStr()
        subprocess.call(cmd)

    
    def Run(self):
        self.callSvnLog()
        self.callStatSvn()

        return self.resultPage

    # local Test
    def makeDirAndFile(self, path):
        os.makedirs(path[0:-11])
        f = open(path,"w+")
        content = '<h1>' + path + '<h2>'
        f.write(content);
        f.close()


# local test
def main():
    author = sys.argv[1]
    print author

    date1 = sys.argv[2]
    date2 = sys.argv[3]
    print date1, date2

    REPO_PATH = "E:/GAEA-PUBLIC/GTJ2015/GGJ"

    print LoC(REPO_PATH, author, date1, date2).Run()

def empty():
    print "empty"

if __name__ == '__main__':
    main()
    #empty()
