#! /usr/bin/env perl

use strict;
use warnings;

use Getopt::Long;
use JSON::XS;
use File::Slurp;
use WebService::SwigClient; 

GetOptions (
  "api_key=s"     => \(my $api_key),
  "template=s"    => \(my $template),
  "command=s"     => \(my $command = 'render'),
  "service_url=s" => \(my $service_url = 'http://swig.io'),
  "json=s"        => \(my $json = '{}'),
) or die("Error in command line arguments\n");

my $error_handler = sub { my ($error, $curl) = @_; warn $error };

my $client = WebService::SwigClient->new(api_key => $api_key, service_url => $service_url, error_handler => $error_handler);
if ( $command && $command eq 'render') {
  print $client->render($template, JSON::XS::decode_json($json)),"\n";
} elsif ( $command && $command eq 'translate' ) {
  print $client->create_translations({ content => JSON::XS::decode_json($json) }),"\n";
} elsif ( $command && $command eq 'create' ) {
  if ( -e $template ) {
    my $data = read_file($template);
    print $client->create_template($template, { content => $data } ),"\n";
  } else {
    print "That template file appears to not exist!\n";
  }
} else {

print<<EOF;
usage:

swig_client --api_key <api_key> --template foo.html
EOF
}
