# -*- coding:utf-8 -*-
"""
Function: add_sp_drive_erase.py moudle. This moudle mainly involves the
 creating the Hard Drive Erase Configuration for
  the Smart Provisioning Service function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2019-2021
"""
import sys

from scripts import common_function


def addspdriveerase_init(parser, parser_list):
    """
    Function Description:initializing the Command for
     creating the Hard Drive Erase Configuration for
      the Smart Provisioning Service.
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    Return Value: subcommand
    """
    sub_parser = parser.add_parser('addspdriveerasetask', help='''
    create Smart Provisioning hard drive erasing task config.''')
    sub_parser.add_argument('-M', dest='mode',
                            type=str, required=False,
                            choices=['Quick', 'Secure'],
                            help='''drive erase mode''')
    sub_parser.add_argument('-S', dest='strategy',
                            type=str, required=False,
                            choices=['Simple', 'Normal', 'Thorough'],
                            help='''drive erase strategy''')
    sub_parser.add_argument('--force', required=False, action='store_true',
                            help='''silent deletion''')

    parser_list['addspdriveerasetask'] = sub_parser

    return 'addspdriveerasetask'


def addspdriveerase(client, args):
    """
    Function Description: create the hard drive erase configuration
    for the Smart Provisioning service.
    Parameter:client redfishClient
    args object:CLI command
    Return Value: resp dict:result of the redfish interface
    """
    # Modify: 2019.11.6 The user selection
    # interaction and forcible execution are added.
    if "--force" not in sys.argv:
        get_tmp()

    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Managers/%s/SPService/SPDriveErase" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] != 200:
        common_function.get_error_info(resp)
        return resp

    payload_dic = get_pyload_dic(args)

    payload = {"DriveErase": payload_dic}
    resp = client.create_resource(url, payload)
    if resp is None:
        return None
    if resp['status_code'] == 201:
        print('Success: successfully completed request.')
    else:
        common_function.get_error_info(resp)
    return resp


def check_parameter(parser, args):
    """
    Function Description:check CLI command
    Parameter:args object:CLI command
    parser object:subcommand ArgumentParser object
    """
    if args.mode is None and args.strategy is None:
        parser.error('Incorrect parameter. -M or -S needs to be set.')
    if args.mode == "Quick" and args.strategy is not None:
        parser.error('Incorrect parameter. -S does not need to be set.')
    if args.mode == "Secure" and args.strategy is None:
        parser.error('Incorrect parameter. -S needs to be set.')


def get_tmp():
    """
    功能描述:是否完成磁盘擦除任务
    参数：None
    返回值：None
    修改：None
    """
    strtemp = input('Confirm whether to create '
                        'a hard drive erasing task.(y/n)? ')
    tmp = strtemp.strip()
    if tmp in ['n', 'N']:
        print("Operation canceled.")
        sys.exit(0)
    elif tmp not in ['y', 'Y']:
        print("Incorrect input. Operation canceled.")
        sys.exit(0)


def get_pyload_dic(args):
    """
    功能描述:初始化 payload_dic
    参数：args (list): parameter list
    返回值：payload_dic (dict)
    修改：None
    """
    payload_dic = {}
    if args.mode:
        payload_dic["EraseMode"] = args.mode
    if args.strategy:
        payload_dic["EraseStrategy"] = args.strategy
    return payload_dic
