/**************************************************************************************
 *
 *     Project:    kvmstoreserver-tools
 *     File name:  KeyNameToIdConverter.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver-tools.
 *
 * kvmstoreserver-tools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver-tools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver-tools.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package keyNameHandling

// nolint
import (
	"crypto/md5"
	"errors"
	"strconv"
	"strings"
)

//*****************************************************************************
//*
//*      Structure: keyContext
//*
//*****************************************************************************

// KeyContext takes the original key and forms or calculates
// all necessary parameters. This holds it in, so you can access
// it from the outside
type keyContext struct {
	keyNameAsString string
	keyMd5Hash      [16]byte

	// Here is the calculated node ID where the data should be stored
	dataNodeID int
}

/********************* Methods ************************************************/

// defDataNodeID ...
func (slf *keyContext) defDataNodeID() {

	var uniqueIdentifierStr string

	for idx := 0; idx < 6; idx++ {
		uniqueIdentifierStr += strconv.Itoa(int(slf.keyMd5Hash[idx]))
	}

	uniqueIdentifier, _ := strconv.ParseInt(uniqueIdentifierStr, 10, 64)
	slf.dataNodeID = int(uniqueIdentifier)
}

// nolint
// defChkSum calc the checksum from the name (in string form)
func (slf *keyContext) defChkSum() {
	slf.keyMd5Hash = md5.Sum([]byte(slf.keyNameAsString))
}

/********************* Interface: IHandelWithIds ******************************/

// GetKeyStrName returns the plain text key name from the structure
func (slf *keyContext) GetKeyStrName() string { return slf.keyNameAsString }

// GetNodeId returns the calculated Node ID from the structure
func (slf *keyContext) GetNodeID() int { return slf.dataNodeID }

//*****************************************************************************
//*
//*      Public Functions
//*
//*****************************************************************************

// NewKeyContext Returns a reference to the structure, which
// contains all information on the following points:
//
// 	- Original Key Name as string
// 	- MD5 hash of the key name
// 	- The ID of RootTree
// 	- The ID of the node where to save or read data
func NewKeyContext(key string) (IHandelWithIds, error) {

	var kc keyContext

	// A backup to prevent blank input
	secKey := strings.Trim(key, " ")
	if len(secKey) == 0 {
		return &kc, errors.New("Key name is a empty string")
	}

	kc.keyNameAsString = key
	kc.defChkSum()
	kc.defDataNodeID()

	return &kc, nil
}
