import logging

import numpy as np


class Tile(object):

    def __init__(self, index, image, is_missing, bounds, channel, size, margins, *args, **kwargs):

        # identifier
        self.index = index

        # actual image data
        self.image = image
        self.is_missing = is_missing

        # parameters related to the position of the tile within a larger image
        self.bounds = bounds
        self.channel = channel

        # parameters related to the valid portion of the tile
        self.size = size
        self.margins = margins

        logging.info('tile {index} on channel {channel} starts at ({0}, {1})'.format(self.bounds['row']['start'], 
                                                                                     self.bounds['column']['start'], 
                                                                                     index=self.index, 
                                                                                     channel=self.channel))


    def trim_self(self):
        logging.info('trimming tile')
        self.image = self.trim(self.image)

    
    def trim(self, image):
        logging.info('trimming with margins ({row}, {column})'.format(**self.margins))

        return image[self.margins['row']: self.margins['row'] + self.size['row'], 
                     self.margins['column']: self.margins['column'] + self.size['column']]


    def average_tile_is_untrimmed(self, average_tile):
        return average_tile.shape[0] > self.image.shape[0] \
            or average_tile.shape[1] > self.image.shape[1]


    def apply_average_tile(self, average_tile):

        if average_tile is None:
            logging.info('no average tile found for tile with index {index} on channel {channel}'.format(**self.__dict__))
            return self.image

        if self.average_tile_is_untrimmed(average_tile):
            logging.info('trimming average tile')
            average_tile = self.trim(average_tile)

        logging.info('applying flatfield correction to tile with index {index} on channel {channel}'.format(**self.__dict__))
        return np.multiply(self.image, average_tile)

    
    def apply_average_tile_to_self(self, average_tile):
        self.image = self.apply_average_tile(average_tile)


    def get_image_region(self):
        
        row = self.bounds['row']
        col = self.bounds['column']

        return [slice(row['start'], row['end']), 
                slice(col['start'], col['end']), 
                self.channel]


    def get_missing_path(self):

        row = self.bounds['row']
        col = self.bounds['column']
        
        path = [row['start'], col['start'], 
                row['end'], col['start'], 
                row['end'], col['end'], 
                row['start'], col['end']]

        logging.info('missing tile starts at: ({0}, {1})'.format(*path))
        return path


    def initialize_image(self):
        logging.info('initializing tile image to 0')
        self.image = np.zeros((self.size['row'], self.size['column']))
