/**************************************************************************************
 *
 *     Project:    kvmstoreserver-tools
 *     File name:  jsonObjGenerator.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 generator

import (
	"fmt"
	"io/ioutil"
	"keyNameHandling"
	"log"
	"strconv"
	"utils"
)

//*****************************************************************************
//*
//*      Declarations
//*
//*****************************************************************************

const namePattern string = "System_Name_"

//*****************************************************************************
//*
//*      Structure: JObjGenerator
//*
//*****************************************************************************

// JObjGenerator ...
type JObjGenerator struct {
	objNbr   int
	jObj     []byte
	wPath    string
	fullPath string
	syncer   *utils.ParaController
}

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

func (slf *JObjGenerator) generateJObj() {

	keyName := namePattern + strconv.Itoa(slf.objNbr)
	nodeID, _ := keyNameHandling.NewKeyContext(keyName)

	slf.fullPath = slf.wPath + "/" + strconv.Itoa(nodeID.GetNodeID()) + ".json"

	dataNbr := slf.objNbr
	dataVers := float64(slf.objNbr) + 0.99999999
	dataAct := slf.objNbr%2 == 0
	dataNull := "null"

	slf.jObj = []byte(
		fmt.Sprintf(`{"node_id":%v,"orig_key_name":"%v","data_container":[{"rev_nbr":0,"meta_header":{"remote_addr":"0.0.0.0:65535","created_at":"1970-01-01T00:00:00.3889558+01:00"},"data":{"system_name": "%v","system_number": %v,"system_version": %v,"system_active": %v,"system_null": %v}}]}`,
			nodeID.GetNodeID(), nodeID.GetKeyStrName(), nodeID.GetKeyStrName(), dataNbr, dataVers, dataAct, dataNull),
	)

}

// WriteIntoTarget ...
func (slf *JObjGenerator) WriteIntoTarget() {

	if err := ioutil.WriteFile(slf.fullPath, slf.jObj, 0660); err != nil {
		log.Printf("Write for %v failed: Reason -> [%v]", slf.fullPath, err)
	}

	slf.syncer.Wg.Add(-1)
}

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

// NewWorker ...
func NewWorker(objNbr int, wPath *string, syncer *utils.ParaController) *JObjGenerator {
	newObj := JObjGenerator{
		objNbr: objNbr,
		wPath:  *wPath,
		syncer: syncer,
	}

	newObj.generateJObj()

	return &newObj
}
