### ----------------------------------------------------
### -- Gnome::Gtk3
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman
### -- File: t/Image.rakutest
### ----------------------------------------------------
use v6;
use NativeCall;
use Test;

#use Gnome::Cairo;
#use Gnome::Cairo::Enums:api<1>;
use Gnome::Cairo::Types:api<1>;
use Gnome::Cairo::ImageSurface:api<1>;

use Gnome::Gtk3::Enums:api<1>;
use Gnome::Gtk3::Image:api<1>;
#use Gnome::Gtk3::EventBox:api<1>;
use Gnome::Gtk3::Window:api<1>;

use Gnome::Gdk3::Pixbuf:api<1>;

use Gnome::Gio::File:api<1>;
use Gnome::Gio::FileIcon:api<1>;

#use Gnome::N::X:api<1>;
#Gnome::N::debug(:on);

#-------------------------------------------------------------------------------
my Gnome::Gtk3::Image $i;
#-------------------------------------------------------------------------------
subtest 'ISA test', {
  $i .= new;
  isa-ok $i, Gnome::Gtk3::Image, ".new";
  is $i.get-storage-type, GTK_IMAGE_EMPTY, '.get-storage-type() empty';
}

#-------------------------------------------------------------------------------
unless %*ENV<raku_test_all>:exists {
  done-testing;
  exit;
}

#-------------------------------------------------------------------------------
subtest 'Manipulations', {

  $i .= new(:file<t/data/Add.png>);
  isa-ok $i, Gnome::Gtk3::Image, ".new(:filename)";
  is $i.get-storage-type, GTK_IMAGE_PIXBUF, '.get-storage-type() pixbuf';

  $i .= new(:file<desktop>);
  is $i.get-storage-type, GTK_IMAGE_ICON_NAME, '.get-storage-type() icon name';

  $i .= new(:file<t/data/Alexis-Kaufman.gif>);
  is $i.get-storage-type, GTK_IMAGE_ANIMATION, '.get-storage-type() animation';

  $i .= new( :icon-name<media-seek-forward>, :size(GTK_ICON_SIZE_DIALOG));
  is $i.get-storage-type, GTK_IMAGE_ICON_NAME, '.new( :icon, :size)';

  $i .= new;
  $i.set-from-file('/data/Add.png');
  is $i.get-storage-type, GTK_IMAGE_ICON_NAME, '.set-from-file() icon name';

  $i .= new;
  $i.set-from-icon-name( 'help-contents', GTK_ICON_SIZE_DIALOG);
  is $i.get-storage-type, GTK_IMAGE_ICON_NAME, '.set-from-icon-name() icon name';


  my Gnome::Cairo::ImageSurface $is .= new(:png<t/data/Add.png>);
  $i .= new(:surface($is));
  is $i.get-storage-type, GTK_IMAGE_SURFACE, '.new(:surface)';

  $i .= new;
  $i.set-from-surface($is);
  is $i.get-storage-type, GTK_IMAGE_SURFACE, '.set-from-surface() surface';

#  $i.clear;
#  is GtkImageType($i.get-storage-type), GTK_IMAGE_EMPTY, '.clear()';

  $i .= new(:file<t/data/Add.png>);
  my Gnome::Gdk3::Pixbuf $pb .= new(:native-object($i.get-pixbuf));
  is $pb.get-width, 16, '.get-pixbuf()';
  my Gnome::Gtk3::Image $i2 .= new(:pixbuf($pb));
  is $i2.get-storage-type, GTK_IMAGE_PIXBUF, '.new(:pixbuf)';
  $i2.clear;
  is $i2.get-storage-type, GTK_IMAGE_EMPTY, '.clear()';

  $i .= new;
  $i.set-from-pixbuf($pb);
  is $i.get-storage-type, GTK_IMAGE_PIXBUF, '.set-from-pixbuf() pixbuf';



  my Gnome::Gtk3::Window $window .= new;
  $window.add($i);
  $i .= new( :icon-name<help-about>, :size(GTK_ICON_SIZE_DIALOG));
  is $i.get-pixel-size, -1, '.get-pixel-size(): default -1';
  $i.set-pixel-size(1);
  is $i.get-pixel-size, 1, '.set-pixel-size() / .get-pixel-size()';

  my List $info = $i.get-icon-name;
  is-deeply $info, ( 'help-about', GTK_ICON_SIZE_INVALID),
            '.get-icon-name()';

  my Gnome::Gio::File() $file .= new(:path<xt/data/bullseye.jpg>);
  my Gnome::Gio::FileIcon $gicon .= new(:$file);
  my Gnome::Gtk3::Image $i3 .= new(:$gicon);
  my Gnome::Gio::FileIcon() $fi2;
  my GtkIconSize() $size;
  ( $fi2, $size) = $i3.get-gicon;
  is $size, GTK_ICON_SIZE_DIALOG, '.new( :gicon, :size)';

  $file .= new(:path<xt/data/tomj.jpg>);
  $gicon .= new(:$file);
  $i3.set-from-gicon( $gicon, GTK_ICON_SIZE_DIALOG);
  ( $fi2, $size) = $i3.get-gicon;
  $file = $fi2.get-file;
  is $file.get-path, 'xt/data/tomj.jpg'.IO.absolute,
     '.get-gicon() / .set-from-gicon()';

#note $fi2.to-string; # ≡ $file.getpath()
}


#-------------------------------------------------------------------------------
subtest 'Inherit Gnome::Gtk3::Image', {
  class MyClass is Gnome::Gtk3::Image {
    method new ( |c ) {
      self.bless( :GtkImage, |c);
    }

    submethod BUILD ( *%options ) {

    }
  }

  my MyClass $mgc .= new;
  isa-ok $mgc, Gnome::Gtk3::Image, '.new()';
}

#-------------------------------------------------------------------------------
subtest 'Properties ...', {
  use Gnome::GObject::Value:api<1>;
  use Gnome::GObject::Type:api<1>;

  #my Gnome::Gtk3::Image $i .= new;

  sub test-property (
    $type, Str $prop, Str $routine, $value,
    Bool :$approx = False, Bool :$is-local = False
  ) {
    my Gnome::GObject::Value $gv .= new(:init($type));
    $i.get-property( $prop, $gv);
    my $gv-value = $gv."$routine"();
    if $approx {
      is-approx $gv-value, $value,
        "property $prop, value: " ~ $gv-value;
    }

    # dependency on local settings might result in different values
    elsif $is-local {
      if $gv-value ~~ /$value/ {
        like $gv-value, /$value/, "property $prop, value: " ~ $gv-value;
      }

      else {
        ok 1, "property $prop, value: " ~ $gv-value;
      }
    }

    else {
      is $gv-value, $value,
        "property $prop, value: " ~ $gv-value;
    }
    $gv.clear-object;
  }

  # example calls
  $i .= new(:file<t/data/Add.png>);
  test-property( G_TYPE_STRING, 'file', 'get-string', 't/data/Add.png');
  $i .= new( :icon-name<help-about>, :size(GTK_ICON_SIZE_DIALOG));
  test-property( G_TYPE_STRING, 'icon-name', 'get-string', 'help-about');
  test-property(
    G_TYPE_ENUM, 'icon-size', 'get-enum', GTK_ICON_SIZE_DIALOG.value
  );
 }

#-------------------------------------------------------------------------------
done-testing;
=finish

#-------------------------------------------------------------------------------
subtest 'Themes ...', {
}


