# -*- coding:utf-8 -*-
"""
Function: set_timezone.py moudle. This moudle mainly involves the
 setting the Time Zone function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
import re

CHECK_ERR = 'argument -Z:the time zone value ranges \
from -12:00 to +14:00 and must be enclosed in \
brackets marks ([]), for example, [-09:00] and [+08:30] '


def settimezone_init(parser, parser_list):
    """
    #=========================================================================
    #   @Description:  settimezone_init
    #
    #   @author:
    #   @Date: 2017-07-28
    #=========================================================================
    """
    sub_parser = parser.add_parser('settimezone', help='''set time zone''')
    sub_parser.add_argument('-Z', dest='DateTimeLocalOffset',
                            type=str, required=True,
                            help='''time zone. the time zone value ranges \
            from -12:00 to +14:00 and must be enclosed in \
            brackets marks ([]), for example, [-09:00]\
            and [+08:30] ''')
    parser_list['settimezone'] = sub_parser

    return 'settimezone'


def check_parameter(parser, args):
    """
    Function Description:check CLI command
    Parameter:args object:CLI command
    parser object:subcommand ArgumentParser object
    """
    args.DateTimeLocalOffset = args.DateTimeLocalOffset[1:-1]
    timezone_type = "(^([-+][0][0-9]:[0-5][0-9])|" + \
                    "([-][1][0-1]:[0-5][0-9])|([+][1][0-3]:" + \
                    "[0-5][0-9])|([-][1][2]:[0][0])|([+][1][4]:[0][0])$)"
    ret = re.match(timezone_type, args.DateTimeLocalOffset)
    if ret is None:
        parser.error(CHECK_ERR)


def settimezone(client, args):
    """
    #=========================================================================
    #   @Description:  settimezone
    #
    #   @author:
    #   @Date: 2017-07-28
    #=========================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/managers/%s/" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] != 200:
        if resp['status_code'] == 404:
            print('Failure: resource was not found')
        return resp

    time_zone = {'DateTimeLocalOffset': args.DateTimeLocalOffset}
    # Property settings
    resp = client.set_resource(url, time_zone)
    if resp is None:
        return None

    if resp['status_code'] == 200:
        print('Success: successfully completed request')
    elif resp['status_code'] == 400:
        if resp['message']['error']['@Message.ExtendedInfo'][0]['MessageId'] \
                == "iBMC.1.0.PropertyModificationNeedPrivilege":
            print('Failure: you do not have the required permissions to '
                  'perform this operation')
            return resp
        print('Failure: %s' %
              resp['message']['error']['@Message.ExtendedInfo'][0]['Message'])

    return resp
