import pycristoforo.geo.shape as shape_py
import logging
import json
import time

__author__ = "Alessandro Negrini"
__copyright__ = "Alessandro Negrini"
__license__ = "mit"

_logger = logging.getLogger(__name__)


def get_shape(key: str):
    """
    This method returns the shape (or shapes) as 'Polygon' or 'MultyPoligon' type
    :param key: country ISO2 code/ISO3 code/name/FIPS code
    :return: the shape as Polygon or Multipolygon
    """
    return shape_py.setup_shape(key)


def get_envelope(shape):
    """
    It returns the envelope built on top the shape passed as input
    :param shape: Polygon or Multipolygon
    :return: shape envelope
    """
    return shape.envelope


def geoloc_generation(shape, num: int, key: str):
    """
    It generates 'points' (ex. 1, 5, 10, 1000) random latitude-longitude pairs (uniform generated).
    :param shape: shape in which you want to fit your geolocations . Polygon and MultiPolygon are the only shapes accepted
    :param num: how many random latitude-longitude pairs do you want to generate
    :param key: country ISO2 code/ISO3 code/name/FIPS code
    :return: list of features (geojson) containing the random points generated by the method
    """
    return shape_py.generate_random_triang_method(shape, num, key)


def geoloc_print(mylist: list, sep: str):
    """
    Method that prints out the list randomly generated geolocation, with separator
    :param mylist: list of geolocations
    :param sep: separator character
    :return: None
    """
    for i, elem in enumerate(mylist):
        if i == len(mylist)-1:
            print(json.dumps(elem))
        else:
            print(json.dumps(elem)+sep)


if __name__ == "__main__":
    start_time = time.time()
    country = get_shape("Italy")
    points = geoloc_generation(country, 100, "Italy")
    geoloc_print(points, ',')
    elapsed_time = time.time() - start_time
    print(elapsed_time)
