package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/ceph/go-ceph/rados"
)

func errChecker(err error) {
	if err != nil {
		log.Fatalf("Error %v happened", err)
	}
}

// request to monitor
func makeMonQuery(cephConn *cephConnection, query map[string]string) []byte {
	monJson, err := json.Marshal(query)
	if err != nil {
		log.Fatalf("Can't marshal json mon query. Error: %v", err)
	}

	monRawAnswer, str, err := cephConn.conn.MonCommand(monJson)
	fmt.Println("MakeMonQuery info: ",str)
	if err != nil {
		log.Fatalf("Failed exec monCommand. Error: %v", err)
	}
	return monRawAnswer
}

// get all available commands and save it to a file
func getAvailableCommands(cephConn *cephConnection) {
	var err error
	var monAnswer map[string]interface{}

	monRawAnswer := makeMonQuery(cephConn, map[string]string{"prefix": "get_command_descriptions"})

	err = json.Unmarshal(monRawAnswer, &monAnswer)
	if err != nil {
		log.Fatalln("Cannot unmarshal")
	}
	prettyView,err := json.MarshalIndent(monAnswer,""," ")
	// fmt.Println(string(prettyView))

//Open a file to write all the commands to
	f, err := os.Create("/tmp/share/commands.txt")
	errChecker(err)
	os.WriteFile("/tmp/share/commands.txt",prettyView,0644)
	defer f.Close()

}

// Prints the output in human readable deserialized form
func OutputFormatter(input []byte) {
	var monAnswer map[string]interface{}
	var err error

	err = json.Unmarshal(input, &monAnswer)
	if err != nil {
		log.Fatalf("Cannot unmarshal %#v",err)
	}
	prettyView,err := json.MarshalIndent(monAnswer,""," ")
	fmt.Println(string(prettyView))
}

func CloseConn(cephConn *cephConnection) {
	fmt.Println("Closing connection to cluster")
	cephConn.conn.Shutdown()
}


func getPools(conn *cephConnection,pool_name string, vars... string) Poolinfo {
	var monAnswer Poolinfo
	var err error
	monRawAnswer := makeMonQuery(conn,map[string]string{"prefix":"osd pool get","pool":pool_name,"var":vars[0],"format":"json"})
	if err = json.Unmarshal(monRawAnswer,&monAnswer); err != nil {
		log.Fatalln("Cannot get pool info %v",err)
	}
	fmt.Println(string(monRawAnswer))
	return monAnswer
}


func main() {

	var connect cephConnection
	var err error
	// var monAnswer map[string]interface{}

// Start connection procedure

	fmt.Println("Creating connection object")

	connect.conn, err = rados.NewConnWithClusterAndUser("ceph", "client.admin")
	if err != nil {
		log.Fatalln("Can't create connection with cluster. Error: %v\n",err)
	}

	if err = connect.conn.ReadConfigFile("/etc/ceph/ceph.conf"); err != nil {
		log.Fatalf("Can't read config file. Error: %v\n", err)
	}

	if err = connect.conn.SetConfigOption("keyring","/etc/ceph/ceph.client.admin.keyring"); err != nil {
		log.Fatalln("Cannot read keyring.Error %v\n",err)
	}

	if err = connect.conn.Connect(); err != nil {
		log.Fatalln("Couldnt connect to cluster. Error is %v\n",err)
	}
	defer CloseConn(&connect)

// end connection procedure

// getAvailableCommands(&connect)
// monRawAnswer := makeMonQuery(&connect, map[string]string{"prefix":"osd pool get","pool":"test","var":"size","format":"json"})
// OutputFormatter(monRawAnswer)
// fmt.Println(string(monRawAnswer))
	fmt.Println(getPools(&connect,"bench","pg_num"))
}

