# coding=utf-8
# Filename: common.py
# pylint: disable=locally-disabled
"""
A collection of commonly used modules.

"""
from __future__ import division, absolute_import, print_function

import numpy as np

from km3pipe import Module
from km3pipe.tools import peak_memory_usage, zenith, azimuth
from km3pipe.dataclasses import KM3DataFrame, KM3Array     # noqa


class Wrap(Module):
    """Wrap a key-val dictionary as a Serialisable.
    """
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.keys = self.get('keys') or None

    def process(self, blob):
        keys = sorted(blob.keys()) if self.keys is None else self.keys
        for key in keys:
            dat = blob[key]
            if dat is None:
                continue
            dt = np.dtype([(f, float) for f in sorted(dat.keys())])
            arr = KM3Array.from_dict(dat, dt)
            blob[key] = arr
        return blob


class Dump(Module):
    """Print the content of the blob.

    Parameters
    ----------
    keys: collection(string), optional [default=None]
        Keys to print. If None, print all keys.
    full: bool, default=False
        Print blob values too, not just the keys?
    """
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.keys = self.get('keys') or None
        self.full = self.get('full') or False

    def process(self, blob):
        keys = sorted(blob.keys()) if self.keys is None else self.keys
        for key in keys:
            print(key + ':')
            if self.full:
                print(blob[key].__repr__())
            print('')
        print('----------------------------------------\n')
        return blob


class Delete(Module):
    """Remove specific keys from the blob.

    Parameters
    ----------
    keys: collection(string), optional
        Keys to remove.
    """
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.keys = self.get('keys') or set()

    def process(self, blob):
        for key in self.keys:
            blob.pop(key, None)
        return blob


class Keep(Module):
    """Keep only specified keys in the blob.

    Parameters
    ----------
    keys: collection(string), optional
        Keys to keep. Everything else is removed.
    """
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.keys = self.get('keys') or set()

    def process(self, blob):
        for key in blob.keys():
            if key not in self.keys:
                blob.pop(key, None)
        return blob


class HitCounter(Module):
    """Prints the number of hits"""
    def process(self, blob):
        try:
            print("Number of hits: {0}".format(len(blob['Hit'])))
        except KeyError:
            pass
        return blob


class BlobIndexer(Module):
    """Puts an incremented index in each blob for the key 'blob_index'"""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.blob_index = 0

    def process(self, blob):
        blob['blob_index'] = self.blob_index
        self.blob_index += 1
        return blob


class StatusBar(Module):
    """Displays the current blob number"""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.every = self.get('every') or 1
        self.blob_index = 0

    def process(self, blob):
        if self.blob_index % self.every == 0:
            print('-'*23 + "[Blob {0:>7}]".format(self.blob_index) + '-'*23)
        self.blob_index += 1
        return blob

    def finish(self):
        print('=' * 60)


class MemoryObserver(Module):
    """Shows the maximum memory usage"""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)

    def process(self, blob):
        memory = peak_memory_usage()
        print("Memory peak usage: {0:.3f} MB".format(memory))


class Cut(Module):
    """Drop an event from the pipe if certain criteria are met.

    This is handled by the ``km3pipe.core.SkipException``.
    """
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.key = self.get('key')
        self.cond = self.get('condition')

    def process(self, blob):
        df = blob[self.key].conv_to(to='pandas')
        ok = df.eval(self.cond).all()
        if not ok:
            return
        blob[self.key] = df
        return blob


class GetAngle(Module):
    """Convert pos(x, y, z) to zenith, azimuth."""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.key = self.get('key')

    def process(self, blob):
        df = blob[self.key].conv_to(to='pandas')
        df['zenith'] = zenith(df[['dir_x', 'dir_y', 'dir_z']])
        df['azimuth'] = azimuth(df[['dir_x', 'dir_y', 'dir_z']])
        blob[self.key] = df
        return blob

@@ -570,16 +570,118 @@
  or None
+%0A        key = self.get('key') or None%0A        if key and not self.keys:%0A            self.keys = %5Bkey%5D
 %0A%0A    de
@@ -1504,16 +1504,118 @@
 or False
+%0A        key = self.get('key') or None%0A        if key and not self.keys:%0A            self.keys = %5Bkey%5D
 %0A%0A    de
@@ -2231,32 +2231,134 @@
 'keys') or set()
+%0A        key = self.get('key') or None%0A        if key and not self.keys:%0A            self.keys = %5Bkey%5D
 %0A%0A    def proces
@@ -2732,32 +2732,32 @@
 it__(**context)%0A
-
         self.key
@@ -2785,16 +2785,118 @@
 or set()
+%0A        key = self.get('key') or None%0A        if key and not self.keys:%0A            self.keys = %5Bkey%5D
 %0A%0A    de

use single key optionally