# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

###
# wxRuby3 PGPropArg typemap definitions
###

require_relative '../core/mapping'

module WXRuby3

  module Typemap

    module PGPropArg

      include Typemap::Module

      # provide automagic conversion of simple types to PGPropArg arguments
      define do

        map 'const wxPGPropArgCls&' => 'String,Wx::PG::PGProperty' do

          add_header_code <<~__CODE
            static WxRuby_ID s_pg_property_id("PGProperty");
            __CODE

          map_in temp: 'wxPGPropArgCls temp = (wxPGProperty *)0', code: <<~__CODE
            if (!NIL_P($input))
            {
              if (TYPE($input) == T_STRING)
              {
                temp = wxPGPropArgCls(RSTR_TO_WXSTR($input));
              }
              else if (TYPE($input) == T_DATA)
              {
                VALUE rb_klass = rb_const_get(mWxPG, s_pg_property_id());
                if (rb_obj_is_kind_of($input, rb_klass))
                {
                  swig_type_info* swig_type = wxRuby_GetSwigTypeForClass(rb_klass);
                  wxPGProperty *pgprop = (wxPGProperty *)0;
                  SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&pgprop), swig_type, 0);
                  temp = wxPGPropArgCls(pgprop);
                }
                else
                {
                  VALUE msg = rb_inspect($input);
                  rb_raise(rb_eArgError, "Expected Wx::PGProperty for $argnum but got %s",
                                          StringValuePtr(msg));
                }
              }
            }
            $1 = &temp;
            __CODE
          map_directorin code: <<~__CODE
            if ($1.HasName())
            {
              $input = WXSTR_TO_RSTR($1.GetName());
            }
            else
            {
              $input = wxRuby_WrapWxPGPropertyInRuby($1.GetPtr0());
            }
            __CODE
          map_typecheck precedence: 1, code: <<~__CODE
            VALUE rb_klass = rb_const_get(mWxPG, rb_intern("PGProperty"));
            $1 = NIL_P($input) || (TYPE($input) == T_STRING) || (TYPE($input) == T_DATA && rb_obj_is_kind_of($input, rb_klass));
            __CODE
        end

        # doc-only
        map 'wxPGPropArg' => 'String,Wx::PG::PGProperty', swig: false do
          map_in code: ''
        end

      end

    end # PGPropArg

  end # Typemap

end # WXRuby3
