package otpdecrypt

import (
	"strings"
	"one-time-pad-utils/pad_definitions"
)

func OTPDecrypt(message string, key string) string {
	message = strings.Replace(message, " ", "", -1)
	messagesplit := strings.Split(message,"")
	var messagesplitint []int

	key = strings.Replace(key, " ", "", -1)
	keysplit := strings.Split(key,"")
	var keysplitint []int

	for _, char := range messagesplit {
		for n, c := range paddefinitions.NumToCharMap{
			if c == strings.ToUpper(char) {
				messagesplitint = append(messagesplitint, n)

			}
		}
	}

	for _, char := range keysplit {
		for n, c := range paddefinitions.NumToCharMap{
			if c == strings.ToUpper(char) {
				keysplitint = append(keysplitint, n)

			}
		}
	}

	if len(keysplitint) < len(messagesplitint) {
		return ("KEY TOO SHORT TO DECRYPT MESSAGE")
	}

	var decryptedmessage []string

	for pos, messagenum := range messagesplitint{
		decryptedmessage = append(decryptedmessage, paddefinitions.NumToCharMap[(((messagenum - keysplitint[pos]) % 36) + 36) % 36]) // crazy modulo for decryption

	}

	return (strings.Join(decryptedmessage, ""))
}
