/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  fileSystemDriver.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.
 *
 * kvmstoreserver 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 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.  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 serdeHandling

import (
	"bytes"
	"io/ioutil"
	"os"
	"strconv"
	"utils/configMap"

	log "github.com/judwhite/logrjack"
)

//*****************************************************************************
//*
//*      Exported through factory
//*
//*****************************************************************************

// SaveToFileSystem writes the node container to hard disk
func saveToFileSystem(node IHandelSerialization) error {

	if err := ioutil.WriteFile(defineFilePath(node.GetNodeID()),
		node.Marshal(), 0660); err != nil {
		return err
	}

	return nil
}

// deleteFromFileSystem deletes the file which contains the content
// of the node from the hard disk
func deleteFromFileSystem(node IHandelSerialization) error {

	if err := os.Remove(defineFilePath(node.GetNodeID())); err != nil {
		return err
	}

	return nil
}

// loadContainer loads the JSON content of the complete node container
// from the hard disk as byte array and puts all into a reader.
func loadContainerFormDisk(nodeID int) *bytes.Reader {

	fullPath := defineFilePath(nodeID)
	fileContent, loadErr := ioutil.ReadFile(fullPath)
	if loadErr != nil {
		log.Fatalf("Error by loading file from disk -> (%v)",
			fullPath)
	}

	newReader := bytes.NewReader(fileContent)

	return newReader
}

//*****************************************************************************
//*
//*      Private Functions
//*
//*****************************************************************************

// defineFilePath defines the full file path of the serialized
// node container content based on the base path from the config file +
// the node ID
func defineFilePath(i int) string {

	strNodeID := strconv.Itoa(i)
	fullPath := configMap.GetStringOf("storage_target") + "/" + strNodeID + ".json"

	return fullPath
}
