from datetime import datetime

from .base import MarathonResource, MarathonObject


class MarathonTask(MarathonResource):
    """Marathon Task resource.

    :param str app_id: application id
    :param health_check_results: health check results
    :type health_check_results: list[:class:`marathon.models.MarathonHealthCheckResult`] or list[dict]
    :param str host: mesos slave running the task
    :param str id: task id
    :param list[int] ports: allocated ports
    :param staged_at: when this task was staged
    :type staged_at: datetime or str
    :param started_at: when this task was started
    :type started_at: datetime or str
    :param str version: app version with which this task was started
    """

    DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'

    def __init__(self, app_id=None, health_check_results=None, host=None, id=None, ports=None, staged_at=None, started_at=None, version=None):
        self.app_id = app_id
        self.health_check_results = health_check_results or []
        self.health_check_results = [
            hcr if isinstance(hcr, MarathonHealthCheckResult) else MarathonHealthCheckResult().from_json(hcr)
            for hcr in (health_check_results or []) if any(health_check_results)
        ]
        self.host = host
        self.id = id
        self.ports = ports or []
        self.staged_at = staged_at if (staged_at is None or isinstance(staged_at, datetime)) \
            else datetime.strptime(staged_at, self.DATETIME_FORMAT)
        self.started_at = started_at if (started_at is None or isinstance(started_at, datetime)) \
            else datetime.strptime(started_at, self.DATETIME_FORMAT)
        self.version = version

@@ -1676,8 +1676,1409 @@
 version%0A
+%0A%0Aclass MarathonHealthCheckResult(MarathonObject):%0A    %22%22%22Marathon health check result.%0A%0A    See https://mesosphere.github.io/marathon/docs/health-checks.html%0A%0A    :param bool alive: boolean to determine task health%0A    :param int consecutive_failures: number of failed healthchecks in a row%0A    :param str first_success: first time when which healthcheck succeeded%0A    :param str last_failure: last time when which healthcheck failed%0A    :param str last_success: last time when which healthcheck succeeded%0A    :param str task_id: task id%0A    %22%22%22%0A%0A    DATETIME_FORMAT = '%25Y-%25m-%25dT%25H:%25M:%25S.%25fZ'%0A%0A    def __init__(self, alive=None, consecutive_failures=None, first_success=None,%0A                 last_failure=None, last_success=None, task_id=None):%0A        self.alive = alive%0A        self.consecutive_failures = consecutive_failures%0A        self.first_success = first_success if (first_success is None or isinstance(first_success, datetime)) %5C%0A            else datetime.strptime(first_success, self.DATETIME_FORMAT)%0A        self.last_failure = last_failure if (last_failure is None or isinstance(last_failure, datetime)) %5C%0A            else datetime.strptime(last_failure, self.DATETIME_FORMAT)%0A        self.last_success = last_success if (last_success is None or isinstance(last_success, datetime)) %5C%0A            else datetime.strptime(last_success, self.DATETIME_FORMAT)%0A        self.task_id = task_id%0A

define healthcheck result class