package org

import (
	"errors"

	"github.com/appcelerator/amp/api/rpc/account"
	"github.com/appcelerator/amp/cli"
	"github.com/spf13/cobra"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
)

// NewSwitchCommand returns a new instance of the switch command.
func NewSwitchCommand(c cli.Interface) *cobra.Command {
	return &cobra.Command{
		Use:     "switch ACCOUNT",
		Short:   "Switch account",
		PreRunE: cli.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return switch_(c, args)
		},
	}
}

func switch_(c cli.Interface, args []string) error {
	conn := c.ClientConn()
	client := account.NewAccountClient(conn)
	request := &account.SwitchRequest{
		Account: args[0],
	}
	headers := metadata.MD{}
	_, err := client.Switch(context.Background(), request, grpc.Header(&headers))
	if err != nil {
		if s, ok := status.FromError(err); ok {
			return errors.New(s.Message())
		}
	}
	if err := cli.SaveToken(headers, c.Server()); err != nil {
		if s, ok := status.FromError(err); ok {
			return errors.New(s.Message())
		}
	}
	c.Console().Printf("You are now logged in as: %s\n", args[0])
	return nil
}
