from pyclearpass.common import (
    _generate_parameterised_url,
    _remove_empty_keys,
    ClearPassAPILogin,
)

# File Name: api_explorer_logs_v1.py


class ApiLogs(ClearPassAPILogin):
    # Function Section Name:EndpointInfo
    # Function Section Description: Collection of endpoints

    def get_insight_endpoint_mac_by_mac(self, mac=""):
        """
        Operation: Get a single endpoint by MAC address
        HTTP Status Response Codes: 200 OK, 404 Not Found, 406 Not Acceptable, 415 Unsupported Media Type, 200 OK, 404 Not Found, 406 Not Acceptable, 415 Unsupported Media Type
        Required Path Parameter Name: mac, Description: URL parameter mac
        """
        url_path = "/insight/endpoint/mac/{mac}"
        dict_path = {"mac": mac}
        for item in dict_path:
            url_path = url_path.replace("{" + item + "}", dict_path[item])
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    def get_insight_endpoint_ip_by_ip(self, ip=""):
        """
        Operation: Look up endpoints by IP address
        HTTP Status Response Codes: 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity, 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity
        Required Path Parameter Name: ip, Description: IPv4/IPv6 address to match with endpoints
        """
        url_path = "/insight/endpoint/ip/{ip}"
        dict_path = {"ip": ip}
        for item in dict_path:
            url_path = url_path.replace("{" + item + "}", dict_path[item])
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    def get_insight_endpoint_ip_range_by_ip_range(self, ip_range=""):
        """
        Operation: Look up endpoints by IP address range
        HTTP Status Response Codes: 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity, 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity
        Required Path Parameter Name: ip_range, Description: IPv4/IPv6 address range, e.g. 192.168.1.1-255 or 2001:db8:a0b:12cd::a-f
        """
        url_path = "/insight/endpoint/ip-range/{ip_range}"
        dict_path = {"ip_range": ip_range}
        for item in dict_path:
            url_path = url_path.replace("{" + item + "}", dict_path[item])
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    def get_insight_endpoint_time_range_by_from_time_to_time(
        self, from_time="", to_time=""
    ):
        """
        Operation: Look up endpoints by time range
        HTTP Status Response Codes: 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity, 200 OK, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity
        Required Path Parameter Name: from_time, Description: Start time for search, as UNIX timestamp
        Required Path Parameter Name: to_time, Description: End time for search, as UNIX timestamp
        """
        url_path = "/insight/endpoint/time-range/{from_time}/{to_time}"
        dict_path = {"from_time": from_time, "to_time": to_time}
        for item in dict_path:
            url_path = url_path.replace("{" + item + "}", dict_path[item])
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    # Function Section Name:LoginAudit
    # Function Section Description: Get previous login details for an admin user

    def get_login_audit_by_name(self, name=""):
        """
        Operation: Get previous login details for an admin user
        HTTP Status Response Codes: 200 OK, 401 Unauthorized, 403 Forbidden, 404 Not Found, 406 Not Acceptable, 415 Unsupported Media Type, 200 OK, 401 Unauthorized, 403 Forbidden, 404 Not Found, 406 Not Acceptable, 415 Unsupported Media Type
        Required Path Parameter Name: name, Description: User name of admin
        """
        url_path = "/login-audit/{name}"
        dict_path = {"name": name}
        for item in dict_path:
            url_path = url_path.replace("{" + item + "}", dict_path[item])
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    # Function Section Name:SystemEvents
    # Function Section Description: Collection of system events

    def get_system_event(
        self, filter="", sort="", offset="", limit="", calculate_count=""
    ):
        """
        Operation: Get a list of system events
        HTTP Status Response Codes: 200 OK, 401 Unauthorized, 403 Forbidden, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity, 200 OK, 401 Unauthorized, 403 Forbidden, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity
        Optional Query Parameter Name: filter, Description: JSON filter expression specifying the items to return
        Optional Query Parameter Name: sort, Description: Sort ordering for returned items (default +source)
        Optional Query Parameter Name: offset, Description: Zero based offset to start from
        Optional Query Parameter Name: limit, Description: Maximum number of items to return (1 – 1000)
        Optional Query Parameter Name: calculate_count, Description: Whether to calculate the total item count
        """
        url_path = "/system-event"
        dict_query = {
            "filter": filter,
            "sort": sort,
            "offset": offset,
            "limit": limit,
            "calculate_count": calculate_count,
        }
        url_path = _generate_parameterised_url(parameters=dict_query, url=url_path)
        return ClearPassAPILogin._send_request(self, url=url_path, method="get")

    def new_system_event(self, body=({})):
        """
                Operation: Create a new system event
                HTTP Status Response Codes: 201 Created, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity, 201 Created, 406 Not Acceptable, 415 Unsupported Media Type, 422 Unprocessable Entity
                Required Body Parameters (body description)- SystemEvents {source (string): Source,level (string) = ['INFO' or 'ERROR' or 'WARN']: Level,category (string): Category,action (string): Action,description (string): Description,timestamp (string): Timestamp}
                Required Body Parameters (type(dict) body example)- {
          "source": "",
          "level": "",
          "category": "",
          "action": "",
          "description": "",
          "timestamp": ""
        }
        """
        url_path = "/system-event"
        body = _remove_empty_keys(keys=body)
        return ClearPassAPILogin._send_request(
            self, url=url_path, method="post", query=body
        )
