#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from dracclient.resources import uris
from dracclient import utils
from dracclient import wsman


class SystemConfiguration(object):

    def __init__(self, client):
        """Creates SystemManagement object

        :param client: an instance of WSManClient
        """
        self.client = client

    def list_system_settings(self):
        """List the System configuration settings

        :returns: a dictionary with the System settings using its name as the
                  key. The attributes are either SystemEnumerableAttribute,
                  SystemStringAttribute or SystemIntegerAttribute objects.
        :raises: WSManRequestFailure on request failures
        :raises: WSManInvalidResponse when receiving invalid response
        :raises: DRACOperationFailed on error reported back by the DRAC
                 interface
        """
        result = {}
        namespaces = [(uris.DCIM_SystemEnumeration, SystemEnumerableAttribute),
                      (uris.DCIM_SystemString, SystemStringAttribute),
                      (uris.DCIM_SystemInteger, SystemIntegerAttribute)]
        for (namespace, attr_cls) in namespaces:
            attribs = self._get_config(namespace, attr_cls)
            result.update(attribs)
        return result

    def _get_config(self, resource, attr_cls):
        result = {}

        doc = self.client.enumerate(resource)

        items = doc.find('.//{%s}Items' % wsman.NS_WSMAN)

        if items is not None:
            for item in items:
                attribute = attr_cls.parse(item)
                result[attribute.instance_id] = attribute
        return result


class SystemAttribute(object):
    """Generic System attribute class"""

    def __init__(self, name, instance_id, current_value, pending_value,
                 read_only, fqdd, group_id):
        """Creates SystemAttribute object

        :param name: name of the System attribute
        :param instance_id: InstanceID of the System attribute
        :param current_value: current value of the System attribute
        :param pending_value: pending value of the System attribute, reflecting
                an unprocessed change (eg. config job not completed)
        :param read_only: indicates whether this System attribute can be
                changed
        :param fqdd: Fully Qualified Device Description of the System attribute
        :param group_id: GroupID of System attribute
        """
        self.name = name
        self.instance_id = instance_id
        self.current_value = current_value
        self.pending_value = pending_value
        self.read_only = read_only
        self.fqdd = fqdd
        self.group_id = group_id

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not self.__eq__(other)

    @classmethod
    def parse(cls, namespace, system_attr_xml):
        """Parses XML and creates SystemAttribute object"""

        name = utils.get_wsman_resource_attr(
            system_attr_xml, namespace, 'AttributeName')
        instance_id = utils.get_wsman_resource_attr(