package sdlui

import (
	"github.com/veandco/go-sdl2/sdl"
	"opslag.de/schobers/zntg/ui"
)

func eventBase(e sdl.Event) ui.EventBase {
	return ui.EventBase{StampInSeconds: .001 * float64(e.GetTimestamp())}
}

func key(code sdl.Keycode) ui.Key {
	switch code {
	case sdl.K_UNKNOWN:
		return ui.KeyNone
	case sdl.K_RETURN:
		return ui.KeyEnter
	case sdl.K_ESCAPE:
		return ui.KeyEscape
	case sdl.K_BACKSPACE:
		return ui.KeyBackspace
	case sdl.K_TAB:
		return ui.KeyTab
	case sdl.K_SPACE:
		return ui.KeySpace
	// case sdl.K_EXCLAIM:
	// 	return ui.KeyNone
	// case sdl.K_QUOTEDBL:
	// 	return ui.KeyNone
	// case sdl.K_HASH:
	// 	return ui.KeyNone
	// case sdl.K_PERCENT:
	// 	return ui.KeyNone
	// case sdl.K_DOLLAR:
	// 	return ui.KeyNone
	// case sdl.K_AMPERSAND:
	// 	return ui.KeyNone
	case sdl.K_QUOTE:
		return ui.KeyQuote
	// case sdl.K_LEFTPAREN:
	// 	return ui.KeyNone
	// case sdl.K_RIGHTPAREN:
	// 	return ui.KeyNone
	case sdl.K_ASTERISK:
		return ui.KeyPadAsterisk
	case sdl.K_PLUS:
		return ui.KeyPadPlus
	case sdl.K_COMMA:
		return ui.KeyComma
	case sdl.K_MINUS:
		return ui.KeyMinus
	case sdl.K_PERIOD:
		return ui.KeyFullstop
	case sdl.K_SLASH:
		return ui.KeySlash
	case sdl.K_0:
		return ui.Key0
	case sdl.K_1:
		return ui.Key1
	case sdl.K_2:
		return ui.Key2
	case sdl.K_3:
		return ui.Key3
	case sdl.K_4:
		return ui.Key4
	case sdl.K_5:
		return ui.Key5
	case sdl.K_6:
		return ui.Key6
	case sdl.K_7:
		return ui.Key7
	case sdl.K_8:
		return ui.Key8
	case sdl.K_9:
		return ui.Key9
	// case sdl.K_COLON:
	// 	return ui.KeyNone
	case sdl.K_SEMICOLON:
		return ui.KeySemicolon
	// case sdl.K_LESS:
	// 	return ui.KeyNone
	case sdl.K_EQUALS:
		return ui.KeyEquals
	// case sdl.K_GREATER:
	// 	return ui.KeyNone
	// case sdl.K_QUESTION:
	// 	return ui.KeyNone
	// case sdl.K_AT:
	// 	return ui.KeyNone
	case sdl.K_LEFTBRACKET:
		return ui.KeyOpenBrace
	case sdl.K_BACKSLASH:
		return ui.KeyBackslash
	case sdl.K_RIGHTBRACKET:
		return ui.KeyCloseBrace
	// case sdl.K_CARET:
	// 	return ui.KeyNone
	// case sdl.K_UNDERSCORE:
	// 	return ui.KeyNone
	case sdl.K_BACKQUOTE:
		return ui.KeyBacktick
	case sdl.K_a:
		return ui.KeyA
	case sdl.K_b:
		return ui.KeyB
	case sdl.K_c:
		return ui.KeyC
	case sdl.K_d:
		return ui.KeyD
	case sdl.K_e:
		return ui.KeyE
	case sdl.K_f:
		return ui.KeyF
	case sdl.K_g:
		return ui.KeyG
	case sdl.K_h:
		return ui.KeyH
	case sdl.K_i:
		return ui.KeyI
	case sdl.K_j:
		return ui.KeyJ
	case sdl.K_k:
		return ui.KeyK
	case sdl.K_l:
		return ui.KeyL
	case sdl.K_m:
		return ui.KeyM
	case sdl.K_n:
		return ui.KeyN
	case sdl.K_o:
		return ui.KeyO
	case sdl.K_p:
		return ui.KeyP
	case sdl.K_q:
		return ui.KeyQ
	case sdl.K_r:
		return ui.KeyR
	case sdl.K_s:
		return ui.KeyS
	case sdl.K_t:
		return ui.KeyT
	case sdl.K_u:
		return ui.KeyU
	case sdl.K_v:
		return ui.KeyV
	case sdl.K_w:
		return ui.KeyW
	case sdl.K_x:
		return ui.KeyX
	case sdl.K_y:
		return ui.KeyY
	case sdl.K_z:
		return ui.KeyZ
	case sdl.K_CAPSLOCK:
		return ui.KeyCapsLock
	case sdl.K_F1:
		return ui.KeyF1
	case sdl.K_F2:
		return ui.KeyF2
	case sdl.K_F3:
		return ui.KeyF3
	case sdl.K_F4:
		return ui.KeyF4
	case sdl.K_F5:
		return ui.KeyF5
	case sdl.K_F6:
		return ui.KeyF6
	case sdl.K_F7:
		return ui.KeyF7
	case sdl.K_F8:
		return ui.KeyF8
	case sdl.K_F9:
		return ui.KeyF9
	case sdl.K_F10:
		return ui.KeyF10
	case sdl.K_F11:
		return ui.KeyF11
	case sdl.K_F12:
		return ui.KeyF12
	case sdl.K_PRINTSCREEN:
		return ui.KeyPrintScreen
	case sdl.K_SCROLLLOCK:
		return ui.KeyScrollLock
	case sdl.K_PAUSE:
		return ui.KeyPause
	case sdl.K_INSERT:
		return ui.KeyInsert
	case sdl.K_HOME:
		return ui.KeyHome
	case sdl.K_PAGEUP:
		return ui.KeyPageUp
	case sdl.K_DELETE:
		return ui.KeyDelete
	case sdl.K_END:
		return ui.KeyEnd
	case sdl.K_PAGEDOWN:
		return ui.KeyPageDown
	case sdl.K_RIGHT:
		return ui.KeyRight
	case sdl.K_LEFT:
		return ui.KeyLeft
	case sdl.K_DOWN:
		return ui.KeyDown
	case sdl.K_UP:
		return ui.KeyUp
	// case sdl.K_NUMLOCKCLEAR:
	// 	return ui.KeyNone
	case sdl.K_KP_DIVIDE:
		return ui.KeyPadSlash
	case sdl.K_KP_MULTIPLY:
		return ui.KeyPadAsterisk
	case sdl.K_KP_MINUS:
		return ui.KeyPadMinus
	case sdl.K_KP_PLUS:
		return ui.KeyPadPlus
	case sdl.K_KP_ENTER:
		return ui.KeyPadEnter
	case sdl.K_KP_1:
		return ui.KeyPad1
	case sdl.K_KP_2:
		return ui.KeyPad2
	case sdl.K_KP_3:
		return ui.KeyPad3
	case sdl.K_KP_4:
		return ui.KeyPad4
	case sdl.K_KP_5:
		return ui.KeyPad5
	case sdl.K_KP_6:
		return ui.KeyPad6
	case sdl.K_KP_7:
		return ui.KeyPad7
	case sdl.K_KP_8:
		return ui.KeyPad8
	case sdl.K_KP_9:
		return ui.KeyPad9
	case sdl.K_KP_0:
		return ui.KeyPad0
	// case sdl.K_KP_PERIOD:
	// 	return ui.KeyNone
	// case sdl.K_APPLICATION:
	// 	return ui.KeyNone
	// case sdl.K_POWER:
	// 	return ui.KeyNone
	case sdl.K_KP_EQUALS:
		return ui.KeyPadEquals
	// case sdl.K_F13:
	// 	return ui.KeyNone
	// case sdl.K_F14:
	// 	return ui.KeyNone
	// case sdl.K_F15:
	// 	return ui.KeyNone
	// case sdl.K_F16:
	// 	return ui.KeyNone
	// case sdl.K_F17:
	// 	return ui.KeyNone
	// case sdl.K_F18:
	// 	return ui.KeyNone
	// case sdl.K_F19:
	// 	return ui.KeyNone
	// case sdl.K_F20:
	// 	return ui.KeyNone
	// case sdl.K_F21:
	// 	return ui.KeyNone
	// case sdl.K_F22:
	// 	return ui.KeyNone
	// case sdl.K_F23:
	// 	return ui.KeyNone
	// case sdl.K_F24:
	// 	return ui.KeyNone
	// case sdl.K_EXECUTE:
	// 	return ui.KeyNone
	// case sdl.K_HELP:
	// 	return ui.KeyNone
	case sdl.K_MENU:
		return ui.KeyNone
	case sdl.K_SELECT:
		return ui.KeyNone
	case sdl.K_STOP:
		return ui.KeyNone
	case sdl.K_AGAIN:
		return ui.KeyNone
	case sdl.K_UNDO:
		return ui.KeyNone
	case sdl.K_CUT:
		return ui.KeyNone
	case sdl.K_COPY:
		return ui.KeyNone
	case sdl.K_PASTE:
		return ui.KeyNone
	case sdl.K_FIND:
		return ui.KeyNone
	case sdl.K_MUTE:
		return ui.KeyNone
	case sdl.K_VOLUMEUP:
		return ui.KeyVolumeUp
	case sdl.K_VOLUMEDOWN:
		return ui.KeyVolumeDown
	// case sdl.K_KP_COMMA:
	// 	return ui.KeyNone
	// case sdl.K_KP_EQUALSAS400:
	// 	return ui.KeyNone
	// case sdl.K_ALTERASE:
	// 	return ui.KeyNone
	// case sdl.K_SYSREQ:
	// 	return ui.KeyNone
	// case sdl.K_CANCEL:
	// 	return ui.KeyNone
	// case sdl.K_CLEAR:
	// 	return ui.KeyNone
	// case sdl.K_PRIOR:
	// 	return ui.KeyNone
	// case sdl.K_RETURN2:
	// 	return ui.KeyNone
	// case sdl.K_SEPARATOR:
	// 	return ui.KeyNone
	// case sdl.K_OUT:
	// 	return ui.KeyNone
	// case sdl.K_OPER:
	// 	return ui.KeyNone
	// case sdl.K_CLEARAGAIN:
	// 	return ui.KeyNone
	// case sdl.K_CRSEL:
	// 	return ui.KeyNone
	// case sdl.K_EXSEL:
	// 	return ui.KeyNone
	// case sdl.K_KP_00:
	// 	return ui.KeyNone
	// case sdl.K_KP_000:
	// 	return ui.KeyNone
	// case sdl.K_THOUSANDSSEPARATOR:
	// 	return ui.KeyNone
	// case sdl.K_DECIMALSEPARATOR:
	// 	return ui.KeyNone
	// case sdl.K_CURRENCYUNIT:
	// 	return ui.KeyNone
	// case sdl.K_CURRENCYSUBUNIT:
	// 	return ui.KeyNone
	// case sdl.K_KP_LEFTPAREN:
	// 	return ui.KeyNone
	// case sdl.K_KP_RIGHTPAREN:
	// 	return ui.KeyNone
	case sdl.K_KP_LEFTBRACE:
		return ui.KeyOpenBrace // generic equivalent
	case sdl.K_KP_RIGHTBRACE:
		return ui.KeyCloseBrace // generic equivalent
	case sdl.K_KP_TAB:
		return ui.KeyTab // generic equivalent
	case sdl.K_KP_BACKSPACE:
		return ui.KeyBackspace // generic equivalent
	case sdl.K_KP_A:
		return ui.KeyA // generic equivalent
	case sdl.K_KP_B:
		return ui.KeyB // generic equivalent
	case sdl.K_KP_C:
		return ui.KeyC // generic equivalent
	case sdl.K_KP_D:
		return ui.KeyD // generic equivalent
	case sdl.K_KP_E:
		return ui.KeyE // generic equivalent
	case sdl.K_KP_F:
		return ui.KeyF // generic equivalent
	// case sdl.K_KP_XOR:
	// 	return ui.KeyNone
	// case sdl.K_KP_POWER:
	// 	return ui.KeyNone
	// case sdl.K_KP_PERCENT:
	// 	return ui.KeyNone
	// case sdl.K_KP_LESS:
	// 	return ui.KeyNone
	// case sdl.K_KP_GREATER:
	// 	return ui.KeyNone
	// case sdl.K_KP_AMPERSAND:
	// 	return ui.KeyNone
	// case sdl.K_KP_DBLAMPERSAND:
	// 	return ui.KeyNone
	// case sdl.K_KP_VERTICALBAR:
	// 	return ui.KeyNone
	// case sdl.K_KP_DBLVERTICALBAR:
	// 	return ui.KeyNone
	// case sdl.K_KP_COLON:
	// 	return ui.KeyNone
	// case sdl.K_KP_HASH:
	// 	return ui.KeyNone
	case sdl.K_KP_SPACE:
		return ui.KeySpace // generic equivalent
	// case sdl.K_KP_AT:
	// 	return ui.KeyNone
	// case sdl.K_KP_EXCLAM:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMSTORE:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMRECALL:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMCLEAR:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMADD:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMSUBTRACT:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMMULTIPLY:
	// 	return ui.KeyNone
	// case sdl.K_KP_MEMDIVIDE:
	// 	return ui.KeyNone
	// case sdl.K_KP_PLUSMINUS:
	// 	return ui.KeyNone
	// case sdl.K_KP_CLEAR:
	// 	return ui.KeyNone
	// case sdl.K_KP_CLEARENTRY:
	// 	return ui.KeyNone
	// case sdl.K_KP_BINARY:
	// 	return ui.KeyNone
	// case sdl.K_KP_OCTAL:
	// 	return ui.KeyNone
	// case sdl.K_KP_DECIMAL:
	// 	return ui.KeyNone
	// case sdl.K_KP_HEXADECIMAL:
	// 	return ui.KeyNone
	case sdl.K_LCTRL:
		return ui.KeyLeftControl
	case sdl.K_LSHIFT:
		return ui.KeyLeftShift
	case sdl.K_LALT:
		return ui.KeyAlt
	case sdl.K_LGUI:
		return ui.KeyLeftWin
	case sdl.K_RCTRL:
		return ui.KeyRightControl
	case sdl.K_RSHIFT:
		return ui.KeyRightShift
	case sdl.K_RALT:
		return ui.KeyAltGr
	case sdl.K_RGUI:
		return ui.KeyRightWin
	// case sdl.K_MODE:
	// 	return ui.KeyNone
	// case sdl.K_AUDIONEXT:
	// 	return ui.KeyNone
	// case sdl.K_AUDIOPREV:
	// 	return ui.KeyNone
	// case sdl.K_AUDIOSTOP:
	// 	return ui.KeyNone
	// case sdl.K_AUDIOPLAY:
	// 	return ui.KeyNone
	// case sdl.K_AUDIOMUTE:
	// 	return ui.KeyNone
	// case sdl.K_MEDIASELECT:
	// 	return ui.KeyNone
	// case sdl.K_WWW:
	// 	return ui.KeyNone
	// case sdl.K_MAIL:
	// 	return ui.KeyNone
	// case sdl.K_CALCULATOR:
	// 	return ui.KeyNone
	// case sdl.K_COMPUTER:
	// 	return ui.KeyNone
	// case sdl.K_AC_SEARCH:
	// 	return ui.KeyNone
	// case sdl.K_AC_HOME:
	// 	return ui.KeyNone
	// case sdl.K_AC_BACK:
	// 	return ui.KeyNone
	// case sdl.K_AC_FORWARD:
	// 	return ui.KeyNone
	// case sdl.K_AC_STOP:
	// 	return ui.KeyNone
	// case sdl.K_AC_REFRESH:
	// 	return ui.KeyNone
	// case sdl.K_AC_BOOKMARKS:
	// 	return ui.KeyNone
	// case sdl.K_BRIGHTNESSDOWN:
	// 	return ui.KeyNone
	// case sdl.K_BRIGHTNESSUP:
	// 	return ui.KeyNone
	// case sdl.K_DISPLAYSWITCH:
	// 	return ui.KeyNone
	// case sdl.K_KBDILLUMTOGGLE:
	// 	return ui.KeyNone
	// case sdl.K_KBDILLUMDOWN:
	// 	return ui.KeyNone
	// case sdl.K_KBDILLUMUP:
	// 	return ui.KeyNone
	// case sdl.K_EJECT:
	// 	return ui.KeyNone
	// case sdl.K_SLEEP:
	// 	return ui.KeyNone
	default:
		return ui.KeyNone
	}
}

func keyModifiers(mod uint16) ui.KeyModifier {
	var modifiers ui.KeyModifier
	if mod&uint16(sdl.KMOD_ALT|sdl.KMOD_LALT) != 0 {
		modifiers |= ui.KeyModifierAlt
	}
	if mod&uint16(sdl.KMOD_CTRL|sdl.KMOD_LCTRL) != 0 {
		modifiers |= ui.KeyModifierControl
	}
	if mod&uint16(sdl.KMOD_SHIFT|sdl.KMOD_LSHIFT) != 0 {
		modifiers |= ui.KeyModifierShift
	}
	if mod&uint16(sdl.KMOD_GUI|sdl.KMOD_LGUI) != 0 {
		modifiers |= ui.KeyModifierOSCommand
	}
	return modifiers
}

func mouseButton(b uint8) ui.MouseButton {
	switch b {
	case sdl.BUTTON_LEFT:
		return ui.MouseButtonLeft
	case sdl.BUTTON_MIDDLE:
		return ui.MouseButtonMiddle
	case sdl.BUTTON_RIGHT:
		return ui.MouseButtonRight
	}
	return ui.MouseButtonLeft
}

func mouseEvent(e sdl.Event, x, y int32) ui.MouseEvent {
	return ui.MouseEvent{
		X:         float32(x),
		Y:         float32(y),
		EventBase: eventBase(e),
	}
}