from __future__ import absolute_import, division, print_function, unicode_literals

from getpass import getpass
import socket

from six.moves import input
from six.moves.configparser import NoOptionError

from cloak.serverapi.server import Server

from ._base import BaseCommand, CommandError


class Command(BaseCommand):
    brief = "Register this server to your Cloak team"
    description = """
        Register this server to your Cloak team. You must be an administrator
        of a team and have permission to add new servers. You should only need
        to do this once.
    """
    epilog = "Any options not provided will be prompted for."

    def add_arguments(self, parser, group):
        group.add_argument('-e', '--email', help="Email address of your Cloak account.")
        group.add_argument('-p', '--password', help="Your Cloak password.")
        group.add_argument('-t', '--target', help="The target to associate this server with. Get this from your team dashboard.")
        group.add_argument('-n', '--name', default=socket.getfqdn(), help="The name of this server. [%(default)s]")

    def handle(self, config, email, password, target, name, **options):
        try:
            config.get('serverapi', 'server_id')
        except NoOptionError:
            pass
        else:
            raise CommandError("This server is already registered. If you've unregistered it from your team dashboard, you can delete {}".format(options['config_path']))

        if email is None:
            email = input("Enter your Cloak email: ")
        if password is None:
            password = getpass("Enter your Cloak password: ")
        if target is None:
            target = input("Enter the target identifier (from the team dashboard): ")

        server = Server.register(email, password, target, name)

        config.set('serverapi', 'server_id', server.server_id)
        config.set('serverapi', 'auth_token', server.auth_token)

        print("This server has been registered. The next step is to request a certificate.", file=self.stdout)

@@ -354,29 +354,34 @@
 ver to your 
-Cloak
+Encrypt.me
  team%22%0A    d
@@ -434,21 +434,26 @@
 to your 
-Cloak
+Encrypt.me
  team. Y
@@ -465,16 +465,24 @@
 st be an
+%0A       
  adminis
@@ -487,24 +487,16 @@
 istrator
-%0A       
  of a te
@@ -541,16 +541,24 @@
 ers. You
+%0A       
  should 
@@ -566,24 +566,16 @@
 nly need
-%0A       
  to do t
@@ -732,20 +732,18 @@
 t('-
-e
+k
 ', '--
-email
+key
 ', h
@@ -751,250 +751,43 @@
 lp=%22
-Email address of your Cloak account.%22)%0A        group.add_argument('-p', '--password', help=%22Your Cloak password.%22)%0A        group.add_argument('-t', '--target', help=%22The target to associate this server with. Get this from your team dashboard.
+Slot registration authorization key
 %22)%0A 
@@ -931,39 +931,19 @@
 config, 
-email, password, target
+key
 , name, 
@@ -1259,21 +1259,23 @@
      if 
-email
+reg_key
  is None
@@ -1288,21 +1288,23 @@
         
-email
+reg_key
  = input
@@ -1320,223 +1320,59 @@
 our 
-Cloak email: %22)%0A        if password is None:%0A            password = getpass(%22Enter your Cloak password: %22)%0A        if target is None:%0A            target = input(%22Enter the target identifier (from the team dashboard)
+Encrypt.me private end-point slot authorization key
 : %22)
@@ -1410,31 +1410,15 @@
 ter(
-email, password, target
+reg_key
 , na

support for new secret auth token for server registration