package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"gopkg.in/ini.v1"
	"gordenko.dev/dima/qb/client"
)

var (
	metricTypeToName = []string{
		"",
		"cumulative",
		"instant",
	}
)

func main() {
	var (
		iniFileName string
	)

	flag.Usage = func() {
		fmt.Fprint(flag.CommandLine.Output(), helpMessage)
		fmt.Fprint(flag.CommandLine.Output(), configExample)
		fmt.Fprintf(flag.CommandLine.Output(), mainUsage, os.Args[0])
		flag.PrintDefaults()
	}
	flag.StringVar(&iniFileName, "c", "requests.ini", "path to *.ini config file")
	flag.Parse()

	config, err := loadConfig(iniFileName)
	if err != nil {
		log.Fatalln(err)
	}

	conn, err := client.Connect(config.DatabaseAddr)
	if err != nil {
		log.Fatalf("client.Connect(%s): %s\n", config.DatabaseAddr, err)
	} else {
		fmt.Println("Connected to database")
	}

	sendRequests(conn)
}

// CONFIG FILE

const mainUsage = `Usage:
	%s -c path/to/config.ini

`

const helpMessage = `Diploma project. Example requests. Version: 1.0
created by Dmytro Gordenko, 1.e4.kc6@gmail.com
`

const configExample = `
requests.ini example:

databaseAddr    = :12345

`

type Config struct {
	DatabaseAddr string
}

func (s Config) String() string {
	return fmt.Sprintf(`starting options:
databaseAddr = %s
`,
		s.DatabaseAddr)
}

func loadConfig(iniFileName string) (_ Config, err error) {
	file, err := ini.Load(iniFileName)
	if err != nil {
		return
	}

	conf := Config{}
	top := file.Section("")

	conf.DatabaseAddr = top.Key("databaseAddr").String()
	return conf, nil
}
