from gamera.core import *


class Gamerafeatures:
    names = ('area',
             'aspect_ratio',
             'black_area',
             'compactness',
             'diagonal_projection',
             'moments',
             'ncols_feature',
             'nholes',
             'nholes_extended',
             'nrows_feature',
             'skeleton_features',
             'top_bottom',
             'volume',
             'volume16regions',
             'volume64regions',
             'zernike_moments',
             'height',
             'width')

    def __init__(self, img, enabler=None):
        init_gamera()
        self.img = img
        if enabler == None:
            self.enabler = [[True]] * len(self.names)
            self.calcelements()
            self.enabler = []
            for x in self.elements:
                self.enabler.append([True] * len(x))
            self.calcelements()
        else:
            self.enabler = enabler
            self.calcelements()

    def calcelements(self):
        self.elements = []
        if any(self.enabler[0]):
            self.elements.append(self.rw(self.img.area()))
        else:
            self.elements.append([])
        if any(self.enabler[1]):
            self.elements.append(self.rw(self.img.aspect_ratio()))
        else:
            self.elements.append([])
        if any(self.enabler[2]):
            self.elements.append(self.rw(self.img.black_area()))
        else:
            self.elements.append([])
        if any(self.enabler[3]):
            self.elements.append(self.rw(self.img.compactness()))
        else:
            self.elements.append([])
        if any(self.enabler[4]):
            self.elements.append(self.rw(self.img.diagonal_projection()))
        else:
            self.elements.append([])
        if any(self.enabler[5]):
            self.elements.append(self.rw(self.img.moments()))
        else:
            self.elements.append([])
        if any(self.enabler[6]):
            self.elements.append(self.rw(self.img.ncols_feature()))
        else:
            self.elements.append([])
        if any(self.enabler[7]):
            self.elements.append(self.rw(self.img.nholes()))
        else:
            self.elements.append([])
        if any(self.enabler[8]):
            self.elements.append(self.rw(self.img.nholes_extended()))
        else:
            self.elements.append([])
        if any(self.enabler[9]):
            self.elements.append(self.rw(self.img.nrows_feature()))
        else:
            self.elements.append([])
        if any(self.enabler[10]):
            self.elements.append(self.rw(self.img.skeleton_features()))
        else:
            self.elements.append([])
        if any(self.enabler[11]):
            self.elements.append(self.rw(self.img.top_bottom()))
        else:
            self.elements.append([])
        if any(self.enabler[12]):
            self.elements.append(self.rw(self.img.volume()))
        else:
            self.elements.append([])
        if any(self.enabler[13]):
            self.elements.append(self.rw(self.img.volume16regions()))
        else:
            self.elements.append([])
        if any(self.enabler[14]):
            self.elements.append(self.rw(self.img.volume64regions()))
        else:
            self.elements.append([])
        if any(self.enabler[15]):
            self.elements.append(self.rw(self.img.zernike_moments()))
        else:
            self.elements.append([])
        if any(self.enabler[16]):
            self.elements.append([str(self.img.height)])
        else:
            self.elements.append([])
        if any(self.enabler[17]):
            self.elements.append([str(self.img.width)])
        else:
            self.elements.append([])

    def modifyfeature(self, name, value, pos=None):
        if name in self.names:
            id = self.names.index(name)
            if pos == None:
                for i, e in enumerate(self.enabler[id]):
                    self.enabler[id][i] = value
            else:
                if pos is list or pos is tuple:
                    for i in pos:
                        self.enabler[id][pos] = value
                else:
                    self.enabler[id][pos] = value
        else:
            print "feature not found"

    def disableallfeatures(self):
        for i, e in enumerate(self.enabler):
            for j, f in enumerate(self.enabler[i]):
                self.enabler[i][j] = False

    def rw(self, data):
        return ([str(x) for x in data])

    def getdata(self):
        feature = []
        for i, e in enumerate(self.enabler):
            for j, k in enumerate(e):
                if k:
                    feature.append(self.elements[i][j])
        return feature

    def getnames(self):
        fea = []
        for i, n in enumerate(self.names):
            if any(self.enabler[i]):
                for j, k in enumerate(self.enabler[i]):
                    if k:
                        fea.append(n + '#' + str(j))
        return fea