import re

from . import util


class Stats:
    def __init__(self):
        # Pepare some regex for parsing
        self.re_path_tracing = re.compile(
            ".*(Rendered|Path Tracing Tile) ([0-9]+)/([0-9]+)( Tiles)?" +
            "(, Sample ([0-9]+)\/([0-9]+))?$")
        self.re_total_render_time = re.compile(
            ".*Total render time: ([0-9]+(\.[0-9]+)?)")
        self.re_render_time_no_sync = re.compile(
            ".*Render time \(without synchronization\): ([0-9]+(\.[0-9]+)?)")
        self.re_pipeline_time = re.compile(
            "Time: ([0-9:\.]+) \(Saving: ([0-9:\.]+)\)")
        self.re_cycles_memory = re.compile(
            ".*\| Mem:([0-9.]+[KM]?), Peak:([0-9.]+[KM]?) \|.*")

        # Render time stats.
        self.total_render_time = "N/A"
        self.render_time_no_sync = "N/A"
        self.pipeline_render_time = "N/A"

        # Render memory stats.
        self.device_peak_memory = "N/A"
        self.device_memory_usage = "N/A"

        # Current stats.
        self.current_tiles = 0
        self.total_tiles = 0

    def update(self, line):
        # Current tile progress.
        match = self.re_path_tracing.match(line)
        if match:
            self.current_tiles = int(match.group(2))
            self.total_tiles = int(match.group(3))
        # Total render time.
        match = self.re_total_render_time.match(line)
        if match:
            self.total_render_time = float(match.group(1))
        # Render time without sync.
        match = self.re_render_time_no_sync.match(line)
        if match:
            self.render_time_no_sync = float(match.group(1))
        # Total pipeline time.
        match = self.re_pipeline_time.match(line)
        if match:
            self.pipeline_render_time = \
                util.humanReadableTimeToSeconds(match.group(1))
        # Memory usage.
        match = self.re_cycles_memory.match(line)
        if match:
            mem = util.humanReadableSizeToMegabytes(match.group(1))
            peak = util.humanReadableSizeToMegabytes(match.group(1))
            if self.device_memory_usage == "N/A" or \
               mem > self.device_memory_usage:
                self.device_memory_usage = mem
            if self.device_peak_memory == "N/A" or \
               mem > self.device_peak_memory:
                self.device_peak_memory = mem

    def print(self):
        # TODO(sergey): Check that all stats are available.
        print("Total pipeline render time: {} ({} sec)"
              . format(util.humanReadableTimeDifference(
                           self.pipeline_render_time),
                       self.pipeline_render_time))
        print("Total Cycles render time: {} ({} sec)"
              . format(util.humanReadableTimeDifference(
                           self.total_render_time),
                       self.total_render_time))
        print("Pure Cycles render time (without sync): {} ({} sec)"
              . format(util.humanReadableTimeDifference(
                           self.render_time_no_sync),
                       self.render_time_no_sync))
        print("Cycles memoty usage: {} ({} peak)"
              . format(self.device_memory_usage,
                       self.device_peak_memory))

    def asDict(self):
        return {'total_render_time': self.total_render_time,
                'render_time_no_sync': self.render_time_no_sync,
                'pipeline_render_time': self.pipeline_render_time,
                'device_peak_memory': self.device_peak_memory,
                'device_memory_usage': self.device_memory_usage}
