/*
Copyright © Brian Buller <brian@bullercodeworks.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package helpers

import (
	"github.com/gdamore/tcell"
)

func IsKeyEvent(e tcell.Event) bool {
	_, ok := e.(*tcell.EventKey)
	return ok
}

func IsKey(e tcell.EventKey, opts ...tcell.Key) bool {
	for i := range opts {
		if e.Key() == opts[i] {
			return true
		}
	}
	return false
}

func IsBS(e tcell.EventKey) bool {
	return IsKey(e, tcell.KeyBackspace, tcell.KeyBackspace2)
}

func KeyIsDisplayable(ev tcell.EventKey) bool {
	return KeyIsSpace(ev) || KeyIsAlphaNumeric(ev) || KeyIsSymbol(ev)
}

func KeyIsSpace(ev tcell.EventKey) bool {
	r := ev.Rune()
	return r == ' '
}

func KeyIsAlphaNumeric(ev tcell.EventKey) bool {
	return KeyIsAlpha(ev) || KeyIsNumeric(ev)
}

func KeyIsAlpha(ev tcell.EventKey) bool {
	r := ev.Rune()
	return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
}

// KeyIsNumeric Returns whether the event is a
// numeric Key press
func KeyIsNumeric(ev tcell.EventKey) bool {
	r := ev.Rune()
	return (r >= '0' && r <= '9')
}

// KeyIsSymbol Returns whether the event is a
// symbol Key press
func KeyIsSymbol(ev tcell.EventKey) bool {
	symbols := []rune{
		'!', '@', '#', '$', '%', '^', '&', '*',
		'(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|',
		';', ':', '"', '\'', ',', '<', '.', '>', '/', '?', '`', '~', '\\',
	}
	k := ev.Rune()
	for i := range symbols {
		if k == symbols[i] {
			return true
		}
	}
	return false
}

func HasCtrl(ev *tcell.EventKey) bool { return ev.Modifiers()&tcell.ModCtrl == tcell.ModCtrl }
