# Path: analysis/pyFinder/pyfinder/core/utils.py
# def string_to_date(string_date):
#     return datetime.datetime(*time.strptime(string_date, "%Y-%m-%dT%H:%M:%S.%fZ")[:7])


import requests
import json
import sys
import logging
from .dfexception import *
from urllib.parse import urljoin
from  requests.compat import urljoin
from .utils import  string_to_date
        except requests.exceptions.ConnectionError as e:
             self.logger.exception("ConnectionError: ")
             raise e
        except:
            self.logger.exception("Unexpected error:")
            raise

    # TODO crate method for checkein if a image must be scanned gain
    def must_scanned(self, name, remote_last_update):#, tag="latest"):
        """
        :param repo_name: the image name with tag.
        :return: True if the image must be scan. False otherwise.
        """
        tag = name.split(":")[1]
        repo = name.split(":")[0]
        must_scanned = True

        res_image_json = self.get_scan_updated(name) # local "last_scan" and "last_update"
        if res_image_json is not None:   # if not empty list, the result is there
            image_json = res_image_json['images'][0]
            try:
                self.logger.info("[" + name + "] local: last scan: " + str(image_json['last_scan']) + "; last update: " + str(image_json[
                    'last_updated']))

                dofinder_last_scan = string_to_date(image_json['last_scan'])
                if image_json['last_updated']:
                    dofinder_last_update = string_to_date(image_json['last_updated'])
                else:
                    dofinder_last_update = dofinder_last_scan   # if is None tha image is not scan again becuse is  equal to last scan


# Path: analysis/pyFinder/pyfinder/core/utils.py
# def string_to_date(string_date):
#     return datetime.datetime(*time.strptime(string_date, "%Y-%m-%dT%H:%M:%S.%fZ")[:7])


# Path: analysis/pyFinder/pyfinder/core/client_images_service.py
import requests
import json
import sys
import logging
from .dfexception import *
from urllib.parse import urljoin
from  requests.compat import urljoin
from .utils import  string_to_date

        except requests.exceptions.ConnectionError as e:
             self.logger.exception("ConnectionError: ")
             raise e
        except:
            self.logger.exception("Unexpected error:")
            raise

    # TODO crate method for checkein if a image must be scanned gain
    def must_scanned(self, name, remote_last_update):#, tag="latest"):
        """
        :param repo_name: the image name with tag.
        :return: True if the image must be scan. False otherwise.
        """
        tag = name.split(":")[1]
        repo = name.split(":")[0]
        must_scanned = True

        res_image_json = self.get_scan_updated(name) # local "last_scan" and "last_update"
        if res_image_json is not None:   # if not empty list, the result is there
            image_json = res_image_json['images'][0]
            try:
                self.logger.info("[" + name + "] local: last scan: " + str(image_json['last_scan']) + "; last update: " + str(image_json[
                    'last_updated']))

                dofinder_last_scan = string_to_date(image_json['last_scan'])
                if image_json['last_updated']:
                    dofinder_last_update = string_to_date(image_json['last_updated'])
                else:
                    dofinder_last_update = dofinder_last_scan   # if is None tha image is not scan again becuse is  equal to last scan


                if string_to_date(remote_last_update) <= dofinder_last_scan or string_to_date(remote_last_update) <= dofinder_last_update: