#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Text::QRCode;
use GridFiller;
use GridFiller::Result::Pango;
use feature 'say';
use open ':std',':locale';
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($INFO);

#Log::Log4perl->get_logger('GridFiller')->level($INFO);
#Log::Log4perl->get_logger('GridFiller::Chooser')->level($DEBUG);
#Log::Log4perl->get_logger('GridFiller::Status')->level($DEBUG);
#Log::Log4perl->get_logger('GridFiller::Result')->level($DEBUG);

binmode DATA,':utf8';

my $data = do { local $/="\n__WORDS__\n";my $x=<DATA>;chomp $x;$x };
my @words = <DATA>;chomp @words;

my $qr=Text::QRCode->new(
    level=>'H',
    mode=>'8-bit',
);

my $arr=$qr->plot($data);

my $filler=GridFiller->new({
    words=>\@words,
    grid=> $arr,
});

my $result = GridFiller::Result::Pango->new({
    source_grid => $filler->grid,
});

$filler->result($result);
$filler->status->mode('longest');
$filler->status->length($result->length_closure);

$filler->fill();

$result->save_png('/tmp/qr.png');

say '';
say 'Leftovers:';
say for @{$result->leftover_words};

__DATA__
MECARD:N:Ceccarelli,Gianni;TEL:+447564023056;EMAIL:dakkar@thenautilus.net;URL:http://www.thenautilus.net/contacts/;NICKNAME:dakkar;;
__WORDS__
Lois McMaster Bujold
Kage Baker
David Weber
Robert Heinlein
Douglas Adams
Neal Stephenson
Lewis Carrol
Terry Pratchett
Jorge Luis Borges
Frank Herbert
Alessandro Bergonzoni
Stefano Benni
Jules Verne
Neil Gaiman
H.G. Wells
Isaac Asimov
Arthur C. Clarke
Philip K. Dick
JRR Tolkien
Ed Wood
Tim Burton
Monty Python
Terry Gilliam
Orson Welles
Peter Greenaway
David Cronenberg
Stanley Kubrick
Joss Whedon
Quentin Tarantino
Mel Brooks
宮崎駿
MIYAZAKI Hayao
押井守
OSHII Mamoru
庵野 秀明
ANNO Hideaki
Alan Moore
鬼頭 莫宏
KITOH Mohiro
手塚 治虫
TEZUKA Osamu
ゆうきまさみ
YUKI Masami
浦沢 直樹
URASAWA Naoki
星野 之宣
YUKINOBU Hoshino
安倍 吉俊
yoshitoshi ABe
士郎 正宗
SHIROW Masamune
Amanda Palmer
Marian Call
Jonathan Coulton
The Flaming Lips
The Pillows
Nightwish
Stefano Nosei
Jethro Tull
菅野 よう子
KANNO Yoko
Franco Battiato
Enya
Helloween
Mike Oldfield
Alan Parsons Project
Emerson, Lake & Palmer
Fabrizio De André
Kenji Kawai
David Bowie
Girl Genius
Gunnerkrigg Court
Cyrano de Bergerac
Watership Down
RHPS
Thunderbirds
Doctor Who
The Twilight Zone
Blade Runner
Forbidden Planet
Cowboy Bebop
FLCL
Gundam
Macross
ふしぎの海のナディア
The Secret of Blue Water
涼みやはるひの消失
SUZUMIYA Haruhi
Cutey Honey
電脳コイル
Dennou Coil
灰羽連盟
Haibane Renmei
Star Trek
D&D
Go
Shogi
Perl
