__author__ = 'pav'
from DataNormalizer.utils.helpers import os, getFiles, MkDir, DATA_LOGS, BOT_REPORT
from DataNormalizer.utils.meta import MetaReader
 
 
class Bot():
    """
        Abstract class for image dataset representation & manipulation
        Subclass it or extends it to use it elegant
    """
 
 
    def __init__(self, folder):
        self.dataset = folder
        self.root = os.path.join(DATA_LOGS, folder.split("\\")[-1])
        self.wide_dir = os.path.join(folder, "Wide")
        self.tele_dir = os.path.join(folder, "Tele")
        MkDir(self.root)
 
 
    def __repr__(self):
        return """
            DataLogger Object:
 
            <dataset>: %s
            <log_dir>: %s
        """ % ( self.dataset, self.root)
 
 
    def _BotReport(self, *args, **kwargs):
        sock = open(os.path.join(self.root, "LogBot.txt"), "a")
        sock.write(BOT_REPORT % (
            kwargs["type"],
            kwargs["count"],
            kwargs["gps"],
            kwargs["timestamp"],
        ))
        sock.close()
 
 
    def _logging(self, dir, label):
        time_ch = []
        gps_ch = []
        output = os.path.join(self.root, dir.split("\\")[-1] + ".report")
        files = getFiles(dir)
        socket = open(output, 'w')
        for photo in files:
            a, b, c = MetaReader(photo)
            time_ch.append(c)
            gps_ch.append("%s %s" % (str(a), str(b)))
            socket.write("{}#{}#{}#{}\n".format(photo, a, b, c))
            del photo
        self._BotReport(type=label, count=str(len(gps_ch)), gps=str(len(set(gps_ch))), timestamp=str(len(set(time_ch))))
        socket.close()
 
    def Start(self):
        print "-------- Start Logging Data process ----------"
        self._logging(self.wide_dir, 'wide photos')
        self._logging(self.tele_dir, 'tele photos')
        print "--------- End Logging Data process -----------"
 
    def getImageInfo(self):
        return (
            os.path.join(self.root, "Wide.report"),
            os.path.join(self.root, "Tele.report"),
        )
 
    def getImageSource(self):
        return self.wide_dir, self.tele_dir
 
 
if __name__ == "__main__":
    MkDir(DATA_LOGS)
    obj = Bot("C:\Users\pav\Desktop\Saronikos_15_Spetsopoula")
    obj.Start()
    print obj.getImageSource()