import logging
import json

from django.core.management.base import BaseCommand
from django.conf import settings

from django.contrib.gis.geos import GeometryCollection, GEOSGeometry
from django.contrib.gis.geos import MultiPolygon
from django.contrib.gis.gdal import DataSource


from geo.models import Country, Region, Province, City


logging.basicConfig(
    filename='logs/loadgisdata.log',
    filemode='w',
    format='%(levelname)s:%(message)s',
    level=logging.INFO,
    )


class Command(BaseCommand):

    help = 'Load GIS data into database'

    def handle(self, *args, **kwargs):
        logging.info('Deleting all instances of Country, Region, Province, City')
        City.objects.all().delete()
        Province.objects.all().delete()
        Region.objects.all().delete()
        Country.objects.all().delete()
        print('Deleted all instances of Country, Region, Province, City')

        ds = DataSource('datasets/gadm36_ESP.gpkg')

        for layer in ds:
            print(f"Layer {layer.name}:\n \
            \t- Layers: {len(layer)}\n \
            \t- Type: {layer.geom_type.name}\n\
            \t- Features: {layer.num_feat}")

        # country instances
        logging.info("loading country instances")
        print('Creating Country instances')
        country_counter = 0
        for feature in ds[0]:
            try:
                # calculate geometry data
                geom = GEOSGeometry(str(feature.geom))
                polygon_list = []
                for polygon in geom:
                    polygon_list.append(polygon)
                geom_geos = MultiPolygon(polygon_list)
                # create instance
                name = feature.get('NAME_0')
                if name == 'Spain':
                    SPAIN = Country.objects.create(name='España',geo=geom_geos)
                Country.objects.create(name=name,geo=geom_geos)
                country_counter += 1
                logging.info(f"Country instance created for: {name}")
            except Exception as e:
                logging.error(f"[{name}][{type(e)}] {str(e)}")

        # region instances
        logging.info("loading region instances")
        print('Creating Region instances')
        region_counter = 0
        for feature in ds[1]:
            try:
                # calculate geometry data
                geom = GEOSGeometry(str(feature.geom))
                polygon_list = []
                for polygon in geom:
                    polygon_list.append(polygon)
                geom_geos = MultiPolygon(polygon_list)
                # get region name
                name = feature.get('NAME_1')
                # create instance
                Region.objects.create(name=name, geo=geom_geos, country=SPAIN)
                region_counter += 1
                logging.info(f"Region instance created for: {name}")
            except Exception as e:
                logging.error(f"[{name}][{type(e)}] {str(e)}")

        # province instances
        logging.info("loading province instances")
        print('Creating Province instances')
        province_counter = 0
        for feature in ds[2]:
            try:
                # calculate geometry data
                geom = GEOSGeometry(str(feature.geom))
                polygon_list = []
                for polygon in geom:
                    polygon_list.append(polygon)
                geom_geos = MultiPolygon(polygon_list)
                # get name and parent
                name = feature.get('NAME_2')
                parent_region = Region.objects.get(name=feature.get('NAME_1'))
                # create instance
                Province.objects.create(name=name, geo=geom_geos, region=parent_region)
                province_counter += 1
                logging.info(f"Province instance created for: {name}")
            except Exception as e:
                logging.error(f"[{name}][{type(e)}] {str(e)}")

        # city instances
        logging.info("loading city instances")
        print('Creating City instances')
        city_counter = 0
        for feature in ds[4]:
            try:
                # calculate geometry data
                geom = GEOSGeometry(str(feature.geom))
                polygon_list = []
                for polygon in geom:
                    polygon_list.append(polygon)
                geom_geos = MultiPolygon(polygon_list)

                # get name and parent
                name = feature.get('NAME_3')
                parent_province = Province.objects.get(name=feature.get('NAME_2'))
                City.objects.create(name=name, geo=geom_geos, province=parent_province)
                city_counter += 1
                logging.debug(f"City instance created for: {name}")
            except Exception as e:
                logging.error(f"[{type(e)}] {str(e)}")

        logging.info(f"Country instances created: {country_counter}")
        logging.info(f"Region instances created: {region_counter}")
        logging.info(f"Province instances created: {province_counter}")
        logging.info(f"City instances created: {city_counter}")
        print(f"Country instances created: {country_counter}")
        print(f"Region instances created: {region_counter}")
        print(f"Province instances created: {province_counter}")
        print(f"City instances created: {city_counter}")
