//go:build windows && (amd64 || arm64 || ppc64 || ppc64le || s390x || mips64 || mips64le)

package yu_win

import (
	"unsafe"
)

func (t Address) Call(args ...any) (r1, r2, err uintptr) {
	if len(args) == 0 {
		return SyscallN(uintptr(t))
	}
	j_args := make([]uintptr, 0, len(args))
	j_bufs := make([][]byte, 0)
	for _, j_arg := range args {
		if j_arg == nil {
			j_args = append(j_args, 0)
			continue
		}
		switch j_val := j_arg.(type) {
		case bool:
			if j_val {
				j_args = append(j_args, 1)
			} else {
				j_args = append(j_args, 0)
			}
		case byte:
			j_args = append(j_args, uintptr(j_val))
		case int8:
			j_args = append(j_args, uintptr(j_val))
		case int16:
			j_args = append(j_args, uintptr(j_val))
		case uint16:
			j_args = append(j_args, uintptr(j_val))
		case int32:
			j_args = append(j_args, uintptr(j_val))
		case uint32:
			j_args = append(j_args, uintptr(j_val))
		case int64:
			j_args = append(j_args, uintptr(j_val))
		case uint64:
			j_args = append(j_args, uintptr(j_val))
		case int:
			j_args = append(j_args, uintptr(j_val))
		case uint:
			j_args = append(j_args, uintptr(j_val))
		case uintptr:
			j_args = append(j_args, j_val)
		case unsafe.Pointer:
			j_args = append(j_args, uintptr(j_val))
		case *bool:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *byte:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *int8:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *int16:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *uint16:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *int32:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *uint32:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *int64:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *uint64:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *int:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *uint:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *uintptr:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case *unsafe.Pointer:
			j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
		case []bool:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []byte:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []int8:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []int16:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []uint16:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []int32:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []uint32:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []int64:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []uint64:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []int:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []uint:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []uintptr:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case []unsafe.Pointer:
			if len(j_val) == 0 {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
		case string:
			if j_val == "" {
				j_args = append(j_args, 0)
				continue
			}
			j_args = append(j_args, (*[2]uintptr)(unsafe.Pointer(&j_val))[0])
		case S:
			if j_val.Utf8 == "" {
				j_args = append(j_args, 0)
				continue
			}
			switch j_val.Code {
			case Gbk:
				j_unicode_len := len(j_val.Utf8) * 2
				j_unicode := make([]byte, j_unicode_len+2) // 结尾 {0,0}
				j_unicode_len = MultiByteToWideChar.CallInt(65001, 0, j_val.Utf8, len(j_val.Utf8), j_unicode, j_unicode_len)

				j_gbk_len := j_unicode_len * 2
				j_bufs = append(j_bufs, make([]byte, j_gbk_len+2)) // 结尾 {0,0}
				j_i := len(j_bufs) - 1
				WideCharToMultiByte.Call(936, 0, j_unicode, j_unicode_len, j_bufs[j_i], j_gbk_len, 0, 0)

				j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
			case Unicode:
				j_utf8_len := len(j_val.Utf8)

				j_unicode_len := j_utf8_len * 2
				j_bufs = append(j_bufs, make([]byte, j_unicode_len+2)) // 结尾 {0,0}
				j_i := len(j_bufs) - 1
				MultiByteToWideChar.Call(65001, 0, j_val.Utf8, j_utf8_len, j_bufs[j_i], j_unicode_len)

				j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
			}
		default:
			panic("不支持的数据类型")
		}
	}
	return SyscallN(uintptr(t), j_args...)
}

func (t Address) CallInt64(args ...any) int64 {
	j_i, _, _ := t.Call(args...)
	return int64(j_i)
}

func (t Address) CallUint64(args ...any) uint64 {
	j_i, _, _ := t.Call(args...)
	return uint64(j_i)
}
