#!/usr/bin/python

# Metarace : Cycle Race Abstractions
# Copyright (C) 2012  Nathan Fraser
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Convenience program to test printed report templates
#
#
# Usage: testprint template.ini
# or: 	 testprint path/
#
# If a template file is given, all assets will be loaded from the same
# directory. If a path is given, assets will be loaded from that path,
# but the system template will be used.
#
# Output will be a file called output.pdf in the same path as template
# or in the current working directory.
#
import sys
import os

import metarace
from metarace import printing

def main():
    """Run the test print application."""
    cpath = None
    tfile = None

    # expand configpath on cmd line to realpath _before_ doing chdir
    rdir = '.'	# default is cwd with no template
    if len(sys.argv) > 2:
        print('usage: testprint [path/[template.ini]]\n')
        sys.exit(1)
    elif len(sys.argv) == 2:
        rdir = sys.argv[1]
        if os.path.isfile(rdir):
            tfile = os.path.realpath(rdir)
        if not os.path.isdir(rdir):
            rdir = os.path.dirname(rdir)
    cpath = os.path.realpath(rdir)

    metarace.init()
    os.chdir(cpath)
    r = printing.printrep(template=tfile, path=cpath)
    r.set_provisional(True)
    r.set_pagemarks(True)
    r.strings['title'] = 'Report Title'
    r.strings['subtitle'] = 'Session 1 - Program of Events'

    s = printing.section()
    s.heading = 'Index of Events'
    s.subheading = 'Session 1: 10:30am Friday, 21st August 2012'
    s.colheader = ['Event No.', None, 'Description', None, 'Est. Time', None]
    s.lines.append(['1', '', "U15 Men's Sprint Qualifying", None, '20'])
    s.lines.append(['2', '', "U17 Men's Sprint Qualifying", None, '16'])
    s.lines.append(['3', '', "U15 Women's Time Trial Final",  'Final', '34'])
    s.lines.append(['', '', "Presentation in event Order", None, '10'])
    s.lines.append(['4', '', "U17 Men's Scratch Final", 'Final', '15'])
    s.lines.append(['5', '', "U15 Women's Pursuit Qualifying", None, '60'])
    s.lines.append(['', '', "Lunch Break", None, '20'])
    s.lines.append(['1', '', "U15 Men's Sprint Qualifying", None, '20'])
    s.lines.append(['2', '', "U17 Men's Sprint Qualifying", None, '16'])
    s.lines.append(['3', '', "U15 Women's Time Trial Final",  'Final', '34'])
    s.lines.append(['', '', "Presentation in event Order", None, '10'])
    s.lines.append(['4', '', "U17 Men's Scratch Final", 'Final', '15'])
    s.lines.append(['5', '', "U15 Women's Pursuit Qualifying", None, '60'])
    s.lines.append(['5', '', "U15 Women's Pursuit Qualifying", None, '60'])
    s.lines.append(['', '', "Lunch Break", None, '20'])
    s.lines.append(['1', '', "U15 Men's Sprint Qualifying", None, '20'])
    s.lines.append(['2', '', "U17 Men's Sprint Qualifying", None, '16'])
    s.lines.append(['3', '', "U15 Women's Time Trial Final",  'Final', '34'])
    s.lines.append(['', '', "Presentation in event Order", None, '10'])
    s.lines.append(['4', '', "U17 Men's Scratch Final", 'Final', '15'])
    s.lines.append(['5', '', "U15 Women's Pursuit Qualifying", None, '60'])
    r.add_section(s)


    s = printing.section()
    s.heading = 'Section Title'
    s.subheading = 'Subheading for a Section Title'
    s.colheader = ([None, None, None, 'Cat', 'Time/Gap', 'Bonus', None])
    s.units = 's'
    s.lines.append(['1.', '21', 'First LAST (Team)', 'CATA',
                                                   '1h23:21', '10'])
    s.lines.append(['2.', '5', 'First LAST (Team)', 'CATB',
                                                  '+0', '8'])
    s.lines.append(['3.', '19', 'First LAST (Team)', 'CATC',
                                                  '+1:21', '6'])
    s.lines.append(['4.', '109', 'First LAST (Team)', 'CATD',
                                                   '+1:21', '4'])
    s.lines.append([None, None])
    s.lines.append(['dnf', '29', 'First LAST (Team)', 'CATE', None, None])
    r.add_section(s)

    #s = printing.pagebreak()
    #s.set_threshold(0.3)
    #r.add_section(s)

    s = printing.bullet_text()
    s.heading = ('List of bulleted paragraphs')
    s.subheading = ('Subheading for the bullet list.')
    s.bullet = '*'	# default bullet
    s.lines.append(['43a', '23 Rider NAME (Team) behaved poorly during the stage, and hence were penalised. The 1st offence fined $40 and penalised 30 seconds for motorpacing followed by a team fine of $400 for the second offence.\n(CA Annexure 2.3)'])
    s.lines.append(['43b', '24 Rider NAME (Team) fined $400 for failing to attrend official award ceremony.\n(CA Annexure 2.3.2)'])
    r.add_section(s)

    s = printing.twocol_startlist()
    s.heading = 'Two column mass-start event'
    s.subheading = '10 Laps\tdist/composition\tprogression/medal rule'
    s.timestr = 'Time:'
    s.lines.append([None, '12', 'Rider NAME (Team)', None, None, '8', None])
    s.lines.append([])
    s.lines.append([None, '12', 'Rider NAME (Team)', 'scr', None, '6', None])
    s.lines.append(['1.', '1', 'Rider NAME (Team)', None, None, '6', None])
    s.lines.append(['2.', '103', 'Rider NAME (Team)', '8', None, '6', None])
    s.lines.append([None, None, None, None, None, None])
    s.lines.append([None, None, None, None, None, None])
    s.lines.append(['3.', '14', 'Connor CHRISTIE (Southern Queensland)', '7', None, '4', None])
    s.lines.append(['dnf', '4', 'Johan JOHNSTON (MVD)', '6', None, '4', None])
    s.lines.append(['dns', '3', '', '2', None, '4', None])
    r.add_section(s)

    #s = printing.rmsevent()
    ##s.heading = 'Intermission - 15 Minutes'
    #r.add_section(s)

    #s = printing.rmsevent()
    #s.heading = 'Team Abbreviations'
    #s.lines.append(['MVD', None, 'Mersey Valley Devonport', None, None, None])
    #s.lines.append(['QM1', None, 'Queensland Metro-1', None, None, None])
    #s.lines.append(['QM2', None, 'Queensland Metro-2', None, None, None])
    #r.add_section(s)

    s = printing.section()
    s.heading = ('Example Intermediate')
    s.units = 'pt'
    s.lines.append(['1.', '12', 'Rider NAME (Team)', None, None, '8', None])
    s.lines.append(['2.', '13', 'Rider NAME (Team)', None, None, '6', None])
    s.lines.append(['3.', '14', 'Rider NAME (Team)', None, None, '4', None])
    s.lines.append(['1.', '12', 'Rider NAME (Team)', None, None, '8', None])
    s.lines.append(['3.', '14', 'Rider NAME (Team)', None, None, '4', None])
    s.lines.append(['2.', '13', 'Rider NAME (Team)', None, None, '6', None])
    s.lines.append(['3.', '14', 'Rider NAME (Team)', None, None, '4', None])
    r.add_section(s)

    s = printing.preformat_text()
    s.heading = 'Preformatted Text Section'
    s.subheading = 'Subheading for preformatted text block.'
    s.colheader = '012345678901234567890123'
    s.lines = test_receipt.split('\n')
    r.add_section(s)
 
    ofile = os.path.join(cpath, 'output.pdf')
    with open(ofile, 'wb') as f:
        r.output_pdf(f)
    ofile = os.path.join(cpath, 'output.txt')
    with open(ofile, 'wb') as f:
        r.output_text(f)
    ofile = os.path.join(cpath, 'output.xls')
    with open(ofile, 'wb') as f:
        r.output_xls(f)


test_receipt = """Ev 1 Time Trial Final   
141 Shirley AMY(CSL)
145 Elizabeth RANDALL(BB
 0023 C0   9h02:26.1675 
 0028 C1   9h02:30.2009 
 0030 C5   9h02:33.8369 
 0032 C4   9h02:36.4104 
 0034 C2   9h02:41.8021 
 0036 C3   9h02:42.0638 
 0038 C1   9h02:42.8094 
 0040 C5   9h02:45.1814 
 0042 C4   9h02:46.6770 
 0044 C3   9h02:51.3850 
145    1:       25.217 
 0046 C2   9h02:52.2306 
141    1:       26.063 
 0048 C1   9h02:53.2240 
 0050 C5   9h02:54.1941 
 0052 C4   9h02:57.1397 
 0054 C2   9h03:00.6829 
 0056 C1   9h03:01.6253 
 0058 C3   9h03:02.0266 
 0060 C5   9h03:04.9639 
 0062 C4   9h03:05.3963 
 0064 C3   9h03:10.2201 
145    2:       18.835 
145   ST:  9h02:26.1675
145  FIN:  9h03:10.2201
145 TIME:       44.052 
 0066 C2   9h03:11.8482 
141    2:       19.617 
141   ST:  9h02:26.1675
141  FIN:  9h03:11.8482
141 TIME:       45.680 """


if __name__ == '__main__':
    main()
