"""WordOps download core classes."""
import os
import urllib.error
import urllib.request

from wo.core.logging import Log


class WODownload():
    """Method to download using urllib"""
    def __init__():
        pass

    def download(self, packages):
        """Download packages, packges must be list in format of
        [url, path, package name]"""
        for package in packages:
            url = package[0]
            filename = package[1]
            pkg_name = package[2]
            try:
                directory = os.path.dirname(filename)
                if not os.path.exists(directory):
                    os.makedirs(directory)
                Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
                req = urllib.request.Request(
                    url, headers={'User-Agent': 'Mozilla/5.0'})
                with urllib.request.urlopen(req) as response, open(filename, 'wb') as out_file:
                    out_file.write(response.read())
                Log.info(self, "{0}".format("[" + Log.ENDC + "Done"
                                            + Log.OKBLUE + "]"))
            except urllib.error.URLError as e:
                Log.debug(self, "[{err}]".format(err=str(e.reason)))
                Log.error(self, "Unable to download file, {0}"
                          .format(filename))
                return False
            except urllib.HTTPError.error as e:
                Log.error(self, "Package download failed. {0}"
                          .format(pkg_name))
                Log.debug(self, "[{err}]".format(err=str(e.reason)))
                return False
            except urllib.ContentTooShortError.error as e:
                Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
                Log.error(self, "Package download failed. The amount of the"
                          " downloaded data is less than "
                          "the expected amount \{0} ".format(pkg_name))
                return False
