//go:build windows

package yu_clipboard

import (
	yu_sys "gogs.qqck.cn/s/tools/sys"
)

func Get() string {
	if !yu_sys.IsClipboardFormatAvailable.CallBool(yu_sys.CF_UNICODETEXT) {
		return ""
	}
	if !yu_sys.OpenClipboard.CallBool(0) {
		return ""
	}
	defer yu_sys.CloseClipboard.Call()
	return yu_sys.GetClipboardData.CallUnicodeToUtf8(yu_sys.CF_UNICODETEXT)
	// j_hdata := yu_sys.GetClipboardData.CallUintptr(yu_sys.CF_UNICODETEXT)
	// if j_hdata == 0 {
	// 	return ""
	// }
	// j_data_ptr := yu_sys.GlobalLock.CallUintptr(j_hdata)
	// if j_data_ptr == 0 {
	// 	return ""
	// }
	// defer yu_sys.GlobalUnlock.Call(j_data_ptr)
	// return yu_strings.UnicodePtrToUtf8(j_data_ptr)
}

func Set(s string) bool {
	if !yu_sys.OpenClipboard.CallBool(0) {
		return false
	}
	defer yu_sys.CloseClipboard.Call()
	if !yu_sys.EmptyClipboard.CallBool() {
		return false
	}
	if s == "" {
		return true
	}

	j_unicode_size, _, _ := yu_sys.MultiByteToWideChar.Call(65001, 0, s, len(s), 0, 0)
	if j_unicode_size < 1 {
		return false
	}
	j_unicode_size = j_unicode_size*2 + 2

	j_hdata, _, _ := yu_sys.GlobalAlloc.Call(yu_sys.GMEM_MOVEABLE, j_unicode_size)
	if j_hdata == 0 {
		return false
	}
	defer yu_sys.GlobalFree.Call(j_hdata)

	j_data_ptr, _, _ := yu_sys.GlobalLock.Call(j_hdata)
	if j_data_ptr == 0 {
		return false
	}
	defer yu_sys.GlobalUnlock.Call(j_data_ptr)

	yu_sys.MultiByteToWideChar.Call(65001, 0, s, len(s), j_data_ptr, j_unicode_size)

	return yu_sys.SetClipboardData.CallUintptr(yu_sys.CF_UNICODETEXT, j_hdata) != 0
}
