#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "Tunebook.h"

// Sizes for A4 paper
#define PaperWidth 592
#define PaperHeight 842

typedef enum {
  kSortTunesByOrder,
  kSortTunesByTitle,
  kSortTunesByKey,
  kSortTunesByMeter,
} SortOrder;

@interface TuneFormatter : NSObject {
  BOOL oneTunePerPage;
  BOOL showPageNumbers;
  BOOL landscape;
  int transpose;
  NSString* titleFont;
  NSString* subtitleFont;
  NSString* composerFont;
  NSString* wordsFont;
}

@property (nonatomic) BOOL oneTunePerPage;
@property (nonatomic) BOOL showPageNumbers;
@property (nonatomic) BOOL landscape;
@property (nonatomic) int transpose;

- (NSData *)pdfDataForTune:(NSString *)tune;
- (int)drawTune:(NSString *)tune intoContext:(CGContextRef)context bounds:(CGRect)bounds scale:(float)scale;
@end

@interface Tune : NSObject {
    sqlite3 *database;
    NSInteger primaryKey;
    NSString *title;
    NSString *dots;
    BOOL hydrated;
    BOOL dirty;
    BOOL selected;
    BOOL temporary;
    NSData *data;
    char rating;
    NSString *key;
    char numSharps;
    char timeSigNum;
    char timeSigDen;
    NSInteger order;
#if KEYWORDS
    NSString *keyWords;
#endif
}

@property (assign, nonatomic, readonly) NSInteger primaryKey;
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *dots;
@property (readonly, copy, nonatomic) NSString *meter;
#if KEYWORDS
@property (copy, nonatomic) NSString *keyWords;
#endif

@property (copy, nonatomic) NSString *key;
@property (nonatomic) char numSharps;
@property (nonatomic) char timeSigNum;
@property (nonatomic) char timeSigDen;
@property (nonatomic) char rating;

@property (nonatomic) BOOL selected;
@property (nonatomic) BOOL hydrated;
@property (nonatomic) BOOL temporary;

@property (nonatomic) NSInteger order; // This is a hack: store the sort order of this tune in the displayed tunebook for sorting purposes.

+ (void)finalizeStatements;

- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db;
- (void)insertIntoDatabase:(sqlite3 *)database;

- (void)hydrate;
- (void)dehydrate;
- (void)deleteFromDatabase;

- (BOOL)dirty;
- (BOOL)inDatabase;

- (void)setKeySignature:(char) k sharps:(char)n;
- (void)setTimeSignature:(int)num denominator:(int)denom;

- (NSData *)pdfData;
- (NSData *)midiDataWithGuitarChords:(BOOL)guitarChords;

+ (NSData *)midiDataForTune:(NSString *)tune guitarChords:(BOOL)guitarChords;

+ (void) sortTunes:(NSMutableArray *)tunes by:(SortOrder)sortOrder;

+ (id)tuneWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db;

+ (NSData *)pdfDataForTune:(NSString *)tune;
#if TARGET_IPHONE
+ (UIImage *)thumbnailForTune:(NSString *)tune;
#endif

+ (int) drawTune:(NSString *)tune intoContext:(CGContextRef)context bounds:(CGRect)bounds scale:(float)scale;

+ (NSString *)fixAccents:(NSString *)s; // Convert ABC accents to Unicode
@end
