###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.examples module of the BEAT platform.         #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

import numpy as np
from collections import namedtuple
from beat.backend.python.database import View as BaseView


#----------------------------------------------------------


class View(BaseView):
    """Outputs:
        - out: "{{ system_user.username }}/integer/1"

    --------------- --------------- --------------- ---------------
    |     out     | |     out     | |     out     | |     out     |
    --------------- --------------- --------------- ---------------
    """

    def index(self, root_folder, parameters):
        Entry = namedtuple('Entry', ['out'])

        return [
            Entry(42),
        ]


    def get(self, output, index):
        obj = self.objs[index]

        if output == 'out':
            return {
                'value': np.int32(obj.out)
            }


#----------------------------------------------------------


class View2(BaseView):
    """Outputs:
        - out: "{{ system_user.username }}/integer/1"

    --------------- --------------- --------------- ---------------
    |     out     | |     out     | |     out     | |     out     |
    --------------- --------------- --------------- ---------------
    """

    def index(self, root_folder, parameters):
        Entry = namedtuple('Entry', ['out'])

        return [
            Entry(53),
        ]


    def get(self, output, index):
        obj = self.objs[index]

        if output == 'out':
            return {
                'value': np.int32(obj.out)
            }


#----------------------------------------------------------


class LargeView(BaseView):

    """Outputs:
        - out: "{{ system_user.username }}/integer/1"

    --------------- --------------- --------------- ---------------
    |     out     | |     out     | |     out     | |     out     |
    --------------- --------------- --------------- ---------------
    """

    def index(self, root_folder, parameters):
        Entry = namedtuple('Entry', ['out'])

        return [
            Entry(0),
            Entry(1),
            Entry(2),
            Entry(3),
            Entry(4),
        ]


    def get(self, output, index):
        obj = self.objs[index]

        if output == 'out':
            return {
                'value': np.int32(obj.out)
            }


#----------------------------------------------------------


class View10(BaseView):

    """Outputs:
        - out: "{{ system_user.username }}/integer/1"

    --------------- --------------- --------------- ---------------
    |     out     | |     out     | |     out     | |     out     |
    --------------- --------------- --------------- ---------------
    """

    def index(self, root_folder, parameters):
        Entry = namedtuple('Entry', ['out'])

        return [
            Entry(0),
            Entry(1),
            Entry(2),
            Entry(3),
            Entry(4),
            Entry(5),
            Entry(6),
            Entry(7),
            Entry(8),
            Entry(9),
        ]


    def get(self, output, index):
        obj = self.objs[index]

        if output == 'out':
            return {
                'value': np.int32(obj.out)
            }


#----------------------------------------------------------


class ViewDuo(BaseView):

    """Outputs:
        - a: "{{ system_user.username }}/integer/1"
        - b: "{{ system_user.username }}/integer/1"

    ------------------------------- -------------------------------
    |              a              | |              a              |
    ------------------------------- -------------------------------
    --------------- --------------- --------------- ---------------
    |      b      | |      b      | |      b      | |      b      |
    --------------- --------------- --------------- ---------------
    """

    def index(self, root_folder, parameters):
        Entry = namedtuple('Entry', ['a', 'b'])

        return [
            Entry(0, 0),
            Entry(0, 1),
            Entry(0, 2),
            Entry(10, 3),
            Entry(10, 4),
            Entry(10, 5),
            Entry(20, 6),
            Entry(20, 7),
            Entry(20, 8),
        ]


    def get(self, output, index):
        obj = self.objs[index]

        if output == 'a':
            return {
                'value': np.int32(obj.a)
            }

        elif output == 'b':
            return {
                'value': np.int32(obj.b)
            }


#----------------------------------------------------------


def setup_tests():
    pass


#----------------------------------------------------------


# Test the behavior of the views (on fake data)
if __name__ == '__main__':

    setup_tests()

    from beat.backend.python.database import DatabaseTester

    DatabaseTester('View', View,
        [
            'out',
        ],
        parameters=dict(
        ),
    )

    DatabaseTester('View2', View2,
        [
            'out',
        ],
        parameters=dict(
        ),
    )

    DatabaseTester('LargeView', LargeView,
        [
            'out',
        ],
        parameters=dict(
        ),
    )