package main

import (
	"fmt"
	mhayaConst "github.com/mhaya/const"
	"github.com/mhaya/game/game_cluster/nodes/adminapi"
	"github.com/mhaya/game/game_cluster/nodes/center/center"
	"github.com/mhaya/game/game_cluster/nodes/db/db"
	"github.com/mhaya/game/game_cluster/nodes/game/game"
	"github.com/mhaya/game/game_cluster/nodes/webadmin"

	//	"github.com/mhaya/database/game_cluster/nodes/gate"
	"github.com/mhaya/game/game_cluster/nodes/master/master"
	"github.com/mhaya/game/game_cluster/nodes/web/web"
	"github.com/urfave/cli/v2"
	"os"
)

func main() {
	app := &cli.App{
		Name:        "database cluster node",
		Description: "database cluster node database",
		Commands: []*cli.Command{
			versionCommand(),
			masterCommand(),
			centerCommand(),
			webCommand(),
			//gateCommand(),
			gameCommand(),
			webadminCommand(),
			adminApiCommand(),
			dbCommand(),
		},
	}

	_ = app.Run(os.Args)
}

func versionCommand() *cli.Command {
	return &cli.Command{
		Name:      "version",
		Aliases:   []string{"ver", "v"},
		Usage:     "view version",
		UsageText: "game cluster node version",
		Action: func(c *cli.Context) error {
			fmt.Println(mhayaConst.Version())
			return nil
		},
	}
}

func masterCommand() *cli.Command {
	return &cli.Command{
		Name:      "master",
		Usage:     "run master node",
		UsageText: "node master --path=./database/config/profile-gc.json --node=m-master",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			master.Run(path, node)
			return nil
		},
	}
}

func centerCommand() *cli.Command {
	return &cli.Command{
		Name:      "center",
		Usage:     "run center node",
		UsageText: "node center --path=./database/config/profile-gc.json --node=m-center",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			center.Run(path, node)
			return nil
		},
	}
}

func webCommand() *cli.Command {
	return &cli.Command{
		Name:      "web",
		Usage:     "run web node",
		UsageText: "node web --path=./database/config/profile-gc.json --node=m-web-1",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			web.Run(path, node)
			return nil
		},
	}
}

func webadminCommand() *cli.Command {
	return &cli.Command{
		Name:      "webadmin",
		Usage:     "run webadmin node",
		UsageText: "node webadmin --path=./database/config/profile-gc.json --node=m-webadmin-1",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			webadmin.Run(path, node)
			return nil
		},
	}
}

func adminApiCommand() *cli.Command {
	return &cli.Command{
		Name:      "adminapi",
		Usage:     "run adminapi node",
		UsageText: "node adminapi --path=./database/config/profile-gc.json --node=m-web-admin-api-1",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			adminapi.Run(path, node)
			return nil
		},
	}
}

/*func gateCommand() *cli.Command {
	return &cli.Command{
		Name:      "gate",
		Usage:     "run gate node",
		UsageText: "node gate --path=./database/config/profile-gc.json --node=m-gate-1",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			gate.Run(path, node)
			return nil
		},
	}
}*/

func gameCommand() *cli.Command {
	return &cli.Command{
		Name:      "game",
		Usage:     "run game node",
		UsageText: "node game --path=./database/config/profile-gc.json --node=10001",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			game.Run(path, node)
			return nil
		},
	}
}

func dbCommand() *cli.Command {
	return &cli.Command{
		Name:      "database",
		Usage:     "run database node",
		UsageText: "node database --path=./database/config/profile-gc.json --node=m-database",
		Flags:     getFlag(),
		Action: func(c *cli.Context) error {
			path, node := getParameters(c)
			db.Run(path, node)
			return nil
		},
	}
}

func getParameters(c *cli.Context) (path, node string) {
	path = c.String("path")
	node = c.String("node")
	return path, node
}

func getFlag() []cli.Flag {
	return []cli.Flag{
		&cli.StringFlag{
			Name:     "path",
			Usage:    "profile config path",
			Required: false,
			Value:    "./database/config/profile-gc.json",
		},
		&cli.StringFlag{
			Name:     "node",
			Usage:    "node id name",
			Required: true,
			Value:    "",
		},
	}
}
