! Target-side assertions: no file writes, network or native UI calls.
program fortran_api
  use gaspardos_gui
  implicit none
  type(GO64_FSInfo), target :: info
  type(GO64_WindowOptions), target :: options
  type(GO64_Drawing) :: drawing
  type(GO64_Blit), target :: blit
  type(GO64_Event), target :: event
  type(GO64_SocketAddress), target :: address
  type(GO64_StartInfo) :: startup
  type(c_ptr) :: ui, code, framebuffer, mesh, raster, input
  integer(c_int32_t), pointer :: pixels(:)
  character(kind=c_char,len=4), target :: label='abc' // c_null_char
  character(kind=c_char,len=1), target :: letter='X'
  character(kind=c_char), target :: output(32)
  integer(c_int64_t) :: r
  integer :: failures
  interface
    function probe(info, options, blit, event, handle) bind(C,name='gof_test_abi') result(r)
      import
      type(c_ptr), value :: info, options, blit, event
      integer(c_int64_t), value :: handle
      integer(c_int64_t) :: r
    end function
  end interface

  failures=0
  call check(c_sizeof(info)==304, 'FSInfo size')
  call check(c_sizeof(options)==84, 'WindowOptions size')
  call check(c_sizeof(drawing)==148, 'Drawing size')
  call check(c_sizeof(blit)==32, 'Blit size')
  call check(c_sizeof(event)==32, 'Event size')
  call check(c_sizeof(address)==8, 'SocketAddress size')
  call check(c_sizeof(startup)==24, 'StartInfo size')
  info%id=int(z'100000001',c_int64_t); info%bytes=123456789
  info%name(128)=321; info%name_length=128; info%is_directory=1
  options%width=640; options%title(64)='Z'
  blit%stride=320; blit%pixels=c_loc(options)
  event%type=100; event%modifiers=77
  r=probe(c_loc(info),c_loc(options),c_loc(blit),c_loc(event),int(z'100000002',c_int64_t))
  call check(r==0 .and. info%bytes==987654321 .and. event%key==42, 'C/Fortran layout and 64-bit values')
  r=gof_ipv4(127,0,0,1,19002,c_loc(address))
  call check(r==0 .and. address%family==2 .and. address%address(1)==127, 'IPv4')
  r=gof_ipv4(256,0,0,1,80,c_loc(address))
  call check(r==-22, 'IPv4 validation')
  ui=gof_widgets_new(320,240)
  call check(c_associated(ui), 'widgets allocation')
  r=gof_widgets_add(ui,2,7,10,10,120,28,c_loc(label(1:1)),0)
  call check(r==0, 'textfield add')
  r=gof_widgets_add(ui,1,7,10,50,120,28,c_loc(label(1:1)),0)
  call check(r==-17, 'duplicate widget ID')
  event%type=100; r=gof_widgets_event(ui,c_loc(event))
  event%type=8; event%key=9; r=gof_widgets_event(ui,c_loc(event))
  event%key=ichar('X'); r=gof_widgets_event(ui,c_loc(event))
  r=gof_widgets_value(ui,7,c_loc(output),32_c_int64_t)
  call check(r==4 .and. output(4)=='X' .and. output(5)==c_null_char, 'widget keyboard dispatch')
  r=gof_widgets_delete(ui)
  code=gof_code_new()
  r=gof_code_load(code,c_loc(letter),1_c_int64_t,c_loc(output),32_c_int64_t)
  call check(r==1, 'CodeBuffer load')
  r=gof_code_place(code,0,1)
  r=gof_code_key(code,ichar('Y'))
  call check(r==1, 'CodeBuffer edit')
  r=gof_code_text(code,0,0,c_loc(output),32_c_int64_t)
  call check(r==2 .and. output(1)=='X' .and. output(2)=='Y', 'CodeBuffer serialization')
  r=gof_code_undo(code)
  call check(r==1, 'CodeBuffer undo')
  r=gof_code_delete(code)
  input=gof_input_new(c_loc(label(1:1)))
  r=gof_input_key(input,ichar('D'))
  r=gof_input_value(input,c_loc(output),2_c_int64_t)
  call check(r==4 .and. output(1)=='a' .and. output(2)==c_null_char, 'string truncation and full length')
  r=gof_input_delete(input)
  framebuffer=gof_framebuffer_new(32,24)
  call check(c_associated(framebuffer), 'framebuffer allocation')
  r=gof_framebuffer_pixel(framebuffer,2,3,12345)
  call c_f_pointer(gof_framebuffer_pixels(framebuffer),pixels,(/32*24/))
  call check(pixels(1+2+3*32)==12345, 'pixel storage order')
  raster=gof_raster_new(framebuffer)
  mesh=gof_shape(1,2.0_c_float,0.0_c_float,0.0_c_float,16,8)
  call check(c_associated(raster) .and. c_associated(mesh), '3D allocation')
  r=gof_raster_clear(raster,0)
  r=gof_raster_draw(raster,mesh,0.0_c_float,0.0_c_float,0.0_c_float, &
                    0.0_c_float,0.0_c_float,4.0_c_float,0,0)
  call check(r==0 .and. any(pixels/=0), '3D rendering')
  r=gof_raster_delete(raster)
  r=gof_mesh_delete(mesh)
  nullify(pixels)
  r=gof_framebuffer_delete(framebuffer)
  r=gof_widgets_dirty(c_null_ptr)
  call check(r==-22, 'NULL object validation')
  if(failures/=0) stop 1
  print *, 'PASS Fortran ABI, widgets, editing, buffers and 3D'
contains
  subroutine check(condition, message)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: message
    if(.not.condition) then
      failures=failures+1
      print *, 'FAIL ',message
    end if
  end subroutine
end program fortran_api
