#! /usr/bin/env python3


# Modules must contain:
#   definitions()
#   ui_definitions()
#   view_definitions()


app_name = "susm"

STATUS = {
    0: 'Dead',
    1: 'Egg',
    2: 'Infertile egg',
    3: 'Fertile egg',
    4: 'Damaged egg',
    5: 'Died off egg',
    6: 'Dead in shell',
    10: 'Pinkie',
    20: 'Fledgling',
    30: 'Weaned',
    40: 'Moulting',
}
"""
    1 (EGG) > 2 (INFERTILE EGG) > 0 (DEAD)
            > 3 (FETILE EGG) > 4 (DAMAGED EGG)  > 0 (DEAD)
                             > 5 (DIED OFF EGG) > 0 (DEAD)
                             > 6 (DIS)          > 0 (DEAD)
                             > 10 (PINKIE) > 0 (DEAD)
                                           > 20 (FLEDGLING) > 0 (DEAD)
                                                            > 30 (WEANED) > 0 (DEAD)
                                                                          > 40 (MOULTING) > 0 (DEAD)
                                                                                          > 100 (ADULT PLUMAGE)

    Transistions:
        0 #
        1 > 0,2,3,4
        2 > 0,4
        3 > 0,4,5,6,10
        4 > 0
        5 > 0
        6 > 0
    To:
        0 < *
        1 (+ = new)
        2 < 1
        3 < 1
        4 < 1,2,3
        5 < 2
        6 < 2
        10 < 2

    --- < 50: not yet a budgie
    --- 50-100: a sick bird.
    --- 100-150: a normal budgie
    --- 150-200:
    --- 200-250:
"""


def requirements():
    return ["base"]

def definitions(db, scope):
    return {}

def ui_definitions(db, scope):
    return {}

def view_definitions():

    name = 'Breeding Management'
    flow_name = name.upper()
    ref_name = "".join(c.lower() if c.isalnum() else "" for c in name)

    # Data queries.
    queries = {}
    queries["%s.locations" % (ref_name)] = ("result = Location.select(lambda bl: bl.type == Location.BREEDING).order_by(desc(Location.parent), desc(Location.name))", {})
    queries["%s.adults_in_location" % (ref_name)] = ("result = Individual.select(lambda adults: location.id == %(location_id)s and status > 50).order_by(desc(Individual.sex))", {'location_id': None})
    queries["%s.young_in_location" % (ref_name)] = ("result = Individual.select(lambda adults: location.id == %(location_id)s and 0 < status < 50).order_by(desc(Individual.dob))", {'location_id': None})

    # Views
    """
    VIEW
      [TAB*]
        [SECTION*]
          [LINE*]
            (LABEL? FIELD? BUTTON?)+

    Tabs:
        # All identical tabs, depending on the data:
            name, tabs[query,title], sections
        # Specific tabs:
            name, tabs[sections]

    Sections:
        title, lines

    Lines:
        query
    """
    views = {}
    views[flow_name] = {
        "name": name,
        "tabs": {
            "query": "%s.locations" % (ref_name),