package main

import (
	"context"
	"time"
)

func init() {
	setGoDebugEnv()
	checkError(setLogLevels())
	GoF3NodeImpl = &f3Impl{ctx: context.Background()}
}

type f3Impl struct {
	ctx context.Context
}

// The nil checks of the parameters are ommitted because they are passed from Rust code which are not nil.
// The signature pointer types that are generated by rust2go to avoid lifetime issues
func (f3 *f3Impl) run(rpc_endpoint *string, jwt *string, f3_rpc_endpoint *string, initial_power_table *string, bootstrap_epoch *int64, finality *int64, db *string) bool {
	var err error = nil
	const MAX_RETRY int = 5
	nRetry := 0
	for nRetry <= MAX_RETRY {
		err = run(f3.ctx, *rpc_endpoint, *jwt, *f3_rpc_endpoint, *initial_power_table, *bootstrap_epoch, *finality, *db)
		if err != nil {
			nRetry += 1
			logger.Errorf("Unexpected F3 failure, retrying(%d) in 10s... error=%s", nRetry, err)
			time.Sleep(10 * time.Second)
		}
	}
	return err == nil
}

func (f3 *f3Impl) import_snap(f3_rpc_endpoint *string, f3_root *string, snapshot_path *string) string {
	if err := importSnap(f3.ctx, *f3_rpc_endpoint, *f3_root, *snapshot_path); err != nil {
		return err.Error()
	}
	return ""
}
