import re


def parse_show_interface_lag(raw_result):
    """
    Parse the 'show interface' command raw output.

    :param str raw_result: vtysh raw result string.
    :rtype: dict
    :return: The parsed result of the show interface command in a \
        dictionary of the form:

     ::

        {
            'lag_name': 'lag1',
            'aggregated_interfaces': '2 1',
            'agg_key': 1,
            'agg_mode': 'active'
            'ipv4': '10.1.1.1/24',
            'ipv4_secondary': '10.1.1.2/24',
            'ipv6': '2001::1/12',
            'ipv6_secondary': '2001::2/12',
            'speed': 0,
            'speed_unit': 'Mb/s',
            'rx_crc_fcs': 0,
            'rx_dropped': 0,
            'rx_bytes': 0,
            'rx_error': 0,
            'rx_packets': 0,
            'tx_bytes': 0,
            'tx_collisions': 0,
            'tx_dropped': 0,
            'tx_errors': 0,
            'tx_packets': 0,
        }
    """

    show_re = (
        r'\s*Aggregate-name\s(?P<lag_name>\w*)\s*'
        r'Aggregated-interfaces\s\:\s(?P<aggregated_interfaces>(\d\s)*)\s*'
        r'Aggregation-key\s:\s(?P<agg_key>\d*)\s*'
        r'(Aggregate mode\s:\s(?P<agg_mode>\S+))?\s*'
        r'(IPv4\saddress\s(?P<ipv4>\S+))?\s*'
        r'(IPv4\saddress\s(?P<ipv4_secondary>\S+) secondary)?\s*'
        r'(IPv6\saddress\s(?P<ipv6>\S+))?\s*'
        r'(IPv6\saddress\s(?P<ipv6_secondary>\S+) secondary)?\s*'
        r'Speed\s(?P<speed>\d+)\s(?P<speed_unit>\S+)\s*'
        r'(qos trust (?P<qos_trust>\S+))?\s*'
        r'(qos queue-profile (?P<qos_queue_profile>\S+))?\s*'
        r'(qos schedule-profile (?P<qos_schedule_profile>\S+))?\s*'
        r'(qos dscp override (?P<qos_dscp>\S+))?\s*'
        r'RX\s*'
        r'(?P<rx_packets>\d+) input packets\s*'
        r'(?P<rx_bytes>\d+) bytes\s*'
        r'(?P<rx_error>\d+) input error\s*'
        r'(?P<rx_dropped>\d+) dropped\s*'
        r'(?P<rx_crc_fcs>\d+) CRC/FCS\s*'
        r'TX\s*'
        r'(?P<tx_packets>\d+) output packets\s*'
        r'(?P<tx_bytes>\d+) bytes\s*'
        r'(?P<tx_errors>\d+) input error\s*'
        r'(?P<tx_dropped>\d+) dropped\s*'
        r'(?P<tx_collisions>\d+) collision'
    )

    re_result = re.match(show_re, raw_result)
    assert re_result

    result = re_result.groupdict()
    for key, value in result.items():
        if value is not None:
            if value.isdigit():
                result[key] = int(value)
            elif value == 'on':
                result[key] = True
            elif value == 'off':
                result[key] = False
    return result