#!/usr/bin/python3
# -*- coding: utf-8 -*-

from gnr.web.gnrbaseclasses import BaseComponent
from gnr.core.gnrdecorator import public_method

class View(BaseComponent):

    def th_struct(self,struct):
        r = struct.view().rows()
        r.fieldcell('trackid')
        r.fieldcell('name')
        r.fieldcell('albumid')
        r.fieldcell('mediatypeid')
        r.fieldcell('genreid')
        r.fieldcell('composer')
        r.fieldcell('milliseconds')
        r.fieldcell('bytes')
        r.fieldcell('unitprice')

    def th_struct_artists(self,struct):
        "View con Artisti"
        r = struct.view().rows()
        r.fieldcell('name')
        r.fieldcell('albumid')
        r.fieldcell('@albumid.@artistid.name', name='Artist')

    def th_order(self):
        return 'trackid'

    def th_query(self):
        return dict(column='name', op='contains', val='')

class ViewArtist(BaseComponent):

    def th_struct(self,struct):
        r = struct.view().rows()
        r.fieldcell('@albumid.@artistid.name', name='Artist')
        r.fieldcell('name', name='Track')
        r.fieldcell('albumid', name='Album')

    def th_order(self):
        return '@albumid.@artistid.name, name'

    def th_query(self):
        return dict(column='name', op='contains', val='', runOnStart=True)
    
    def th_options(self):
        return dict(widget='dialog', readOnly=True)

    #def th_top_toolbarsuperiore(self,top):
    #    top.slotToolbar('*,sections@genreid,*', childname='superiore', _position='<bar')
    
    def th_top_toolbarsuperiore(self,top):
        top.slotToolbar('*,sections@mediatypeid,*', childname='superiore', _position='<bar')

    def th_queryBySample(self):
        return dict(fields=[dict(field='@albumid.@artistid.name', lbl='Artist', width='20em'),
                            dict(field='@genreid.name', lbl='Genre', width='20em')],
                            cols=2, isDefault=True)



class Form(BaseComponent):

    def th_form(self, form):
        bc = form.center.borderContainer()
        top = bc.contentPane(region='top',datapath='.record')
        fb = top.formbuilder(cols=2, border_spacing='4px')
        fb.field('trackid')
        fb.field('name')
        fb.field('albumid')
        fb.field('mediatypeid')
        fb.field('genreid')
        fb.field('composer')
        fb.field('milliseconds')
        fb.field('bytes')
        fb.field('unitprice')
        tc = bc.tabContainer(region='center',margin='2px')
        tab_mu_invoice_items_trackid = tc.contentPane(title='tracks')
        tab_mu_invoice_items_trackid.dialogTableHandler(relation='@mu_invoice_items_trackid')
        tab_mu_playlist_track_trackid = tc.contentPane(title='tracks')
        tab_mu_playlist_track_trackid.dialogTableHandler(relation='@mu_playlist_track_trackid')


    def th_options(self):
        return dict(dialog_height='400px', dialog_width='600px' )

class FormPurchase(BaseComponent):

    def th_form(self, form):
        pane = form.record
        fb = pane.formbuilder(cols=1, border_spacing='4px')
        fb.field('name')
        fb.field('albumid')
        fb.field('mediatypeid')
        fb.field('genreid')
        fb.field('unitprice')
        fb.button('Buy', action='alert("Good choice!")')
        fb.button('Do NOT Buy', action='this.form.abort()')


    def th_options(self):
        return dict(readOnly=True, modal=True)