/**
 * Created by Benny on 14.11.2014.
 */

"use strict";

var TB = TB || {};

TB.Ext = TB.Ext || {};

/**
 * Currently using https://github.com/bpampuch/pdfmake
 */
TB.Ext.PDF = {

    getDocDefinition : function( content ) {
        return {
            header : { text: 'tbuddy.de', margin: [ 10, 2, 2, 2 ] },
            content : content,
            footer : { text: 'Erstellt mit tbuddy.de  ', alignment: 'right', margin: [ 2, 2, 10, 2 ] },
            styles : {
                h1 : {
                    fontSize: 22,
                    bold: true
                }
            }
        };
    },

    downloadAppointment : function( data ) {
        var tableAcceptedBody, docDefinition, filename,
            content = [],
            eventTitle,
            appointment = data.appointment,
            listAccepted = data.listAccepted;

        eventTitle = ( appointment.subject ) ? appointment.subject : appointment.home + ' - ' + appointment.guest;

        // Download filename
        filename = eventTitle;
        filename += ' ' + TB.Utils.getDatetimeStringFromUnixTimestamp( appointment.starttime );
        filename += '.pdf';

        // Generate game header
        content.push( { text: eventTitle, style : 'h1' } );
        content.push( " " );

        if ( appointment.subject ) {
            content.push( {
                table : {
                    headerRows: 0,
                    widths: [ 'auto', '*'],
                    body : [
                        [ 'Datum', TB.Utils.getDatetimeStringFromUnixTimestamp( appointment.starttime ) ],
                        [ 'Kommentar', ( appointment.comment ) ? appointment.comment : '---' ]
                    ]
                }
            });
            // List of player
            content.push( " " );
            content.push( { text: "Teilnehmer", style: 'h1' } );
        } else {
            content.push( {
                table : {
                    headerRows: 0,
                    widths: [ 'auto', '*'],
                    body : [
                        [ 'ID Nummer', TB.GUI.getValue( appointment.id_nr, '---' ) ],
                        [ 'Heim Verein', appointment.home ],
                        [ 'Gast Verein', appointment.guest ],
                        [ 'Datum', TB.Utils.getDatetimeStringFromUnixTimestamp( appointment.starttime ) ],
                        [ 'Kommentar', ( appointment.comment ) ? appointment.comment : '---' ]
                    ]
                }
            });
            // List of player
            content.push( " " );
            content.push( { text: "Buddy", style: 'h1' } );
        }


        // Generate list of attending player
        $.each( listAccepted, function( i, p ) {
            content.push( " " );
            content.push( { text: 'Buddy #' + (i+1) } );
            content.push( {
                table : {
                    headerRows: 0,
                    widths: [ 'auto', '*'],
                    body : [
                        [ 'Vor- und Nachname', TB.GUI.playerName( p ) ],
                        [ 'Passnummer', ( p.player_pass_no ) ? p.player_pass_no : '---' ],
                        [ 'Geburtstdatum', ( p.birthday ) ? TB.Utils.convertSQLDateToDatepickerDate( p.birthday ) : '---' ],
                        [ 'Zusage am', TB.Utils.getDatetimeStringFromUnixTimestamp( p.last_update ) ]
                    ]
                }
            });
        });

        docDefinition = this.getDocDefinition( content );


        // Create and download
        pdfMake.createPdf(docDefinition).download( filename );
    }
};