import requests

from uncloud.cli.helper import make_request, get_otp_parser
from uncloud.common.parser import BaseParser


class HostParser(BaseParser):
    def __init__(self):
        super().__init__('host')

    def create(self, **kwargs):
        p = self.subparser.add_parser('create', parents=[get_otp_parser()], **kwargs)
        p.add_argument('--hostname', required=True)
        p.add_argument('--cpu', required=True, type=int)
        p.add_argument('--ram', required=True)
        p.add_argument('--os-ssd', required=True)
        p.add_argument('--hdd', default=list())

    def list(self, **kwargs):
        self.subparser.add_parser('list', **kwargs)


parser = HostParser()
arg_parser = parser.arg_parser


def main(**kwargs):
    subcommand = kwargs.pop('host_subcommand')
    if not subcommand:
        arg_parser.print_help()
    else:
        request_method = requests.post
        data = None
        if subcommand == 'create':
            kwargs['specs'] = {
                'cpu': kwargs.pop('cpu'),
                'ram': kwargs.pop('ram'),
                'os-ssd': kwargs.pop('os_ssd'),
                'hdd': kwargs.pop('hdd')
            }
            data = kwargs
        elif subcommand == 'list':
            request_method = requests.get

        make_request('host', subcommand, data=data, request_method=request_method)
