import docker
import time
import logging
from gdal.gdaldockerwrapper import GdalDockerWrapper
from mysqlutils.mysqladapter import MySQLAdapter
from mysqlutils.mysqldockerwrapper import MySqlDockerWrapper
from postgis_docker_wrapper.postgisadapter import PostgisAdapter
from postgis_docker_wrapper.postgisdockerwrapper import PostgisDockerWrapper


def import_mysql():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    docker_client = docker.from_env()
    mysql_docker_wrapper = MySqlDockerWrapper(docker_client)
    mysql_docker_wrapper.start_container()
    gdal_docker_wrapper = GdalDockerWrapper(docker_client)
    mysql_adapter = MySQLAdapter(user="root", password="<PASSWORD>")

    # Recreate schema
    schema_name = "SpatialDatasets"
    if schema_name in mysql_adapter.get_schemas():
        mysql_adapter.execute("DROP SCHEMA SpatialDatasets")
    mysql_adapter.execute("CREATE SCHEMA SpatialDatasets")

    # Import airports
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airports_3857/Airports.shp", "airports_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airports/Airports.shp", "airports"))

    # Import airspaces
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airspace_3857/Class_Airspace.shp", "airspaces_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airspace/Class_Airspace.shp", "airspaces"))

    # Import routes
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "routes_3857/ATS_Route.shp", "routes_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "routes/ATS_Route.shp", "routes"))

def import_postgis():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    docker_client = docker.from_env()
    gdal_docker_wrapper = GdalDockerWrapper(docker_client)
    postgis_docker_wrapper = PostgisDockerWrapper(docker_client)
    postgis_docker_wrapper.start_container()

    # Recreate schema
    schema_name = "spatialdatasets"
    #print(postgis_adapter.get_schemas())
    #if schema_name in postgis_adapter.get_schemas():
    #    print("Deleting old schema")
    #    logger.info(postgis_adapter.execute_nontransaction(f"DROP DATABASE {schema_name}"))

    print("Running createdb")
    # Because ogr2ogr can't do CREATE DATABASE for some reason
    logger.info(postgis_docker_wrapper.inject_command(f"createdb {schema_name} -h 127.0.0.1 -p 5432 -U postgres -w"))
    print("Schema init done")

    postgis_adapter = PostgisAdapter(user="postgres", password="<PASSWORD>", persist = True)

    print("Loading extension")
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_raster;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_topology;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_sfcgal;"))

    # Import airports
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airports_3857/Airports.shp", "airports_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airports/Airports.shp", "airports", schema_name=schema_name))

    # Import airspaces
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airspace_3857/Class_Airspace.shp", "airspaces_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airspace/Class_Airspace.shp", "airspaces", schema_name=schema_name))

    # Import routes
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "routes_3857/ATS_Route.shp", "routes_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "routes/ATS_Route.shp", "routes", schema_name=schema_name))
    logger.info(postgis_adapter.execute(
            f"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name;"))

if __name__ == '__main__':
    import_mysql()
    # import_postgis()

import docker
import time
import logging
from gdal.gdaldockerwrapper import GdalDockerWrapper
from mysqlutils.mysqladapter import MySQLAdapter
from mysqlutils.mysqldockerwrapper import MySqlDockerWrapper
from postgis_docker_wrapper.postgisadapter import PostgisAdapter
from postgis_docker_wrapper.postgisdockerwrapper import PostgisDockerWrapper


def import_mysql():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    docker_client = docker.from_env()
    mysql_docker_wrapper = MySqlDockerWrapper(docker_client)
    mysql_docker_wrapper.start_container()
    gdal_docker_wrapper = GdalDockerWrapper(docker_client)
    mysql_adapter = MySQLAdapter(user="root", password="<PASSWORD>")

    # Recreate schema
    schema_name = "SpatialDatasets"
    if schema_name in mysql_adapter.get_schemas():
        mysql_adapter.execute("DROP SCHEMA SpatialDatasets")
    mysql_adapter.execute("CREATE SCHEMA SpatialDatasets")

    # Import airports
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airports_3857/Airports.shp", "airports_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airports/Airports.shp", "airports"))

    # Import airspaces
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airspace_3857/Class_Airspace.shp", "airspaces_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "airspace/Class_Airspace.shp", "airspaces"))

    # Import routes
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "routes_3857/ATS_Route.shp", "routes_3857"))
    logger.info(gdal_docker_wrapper.import_to_mysql(
        "routes/ATS_Route.shp", "routes"))

def import_postgis():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    docker_client = docker.from_env()
    gdal_docker_wrapper = GdalDockerWrapper(docker_client)
    postgis_docker_wrapper = PostgisDockerWrapper(docker_client)
    postgis_docker_wrapper.start_container()

    # Recreate schema
    schema_name = "spatialdatasets"
    #print(postgis_adapter.get_schemas())
    #if schema_name in postgis_adapter.get_schemas():
    #    print("Deleting old schema")
    #    logger.info(postgis_adapter.execute_nontransaction(f"DROP DATABASE {schema_name}"))

    print("Running createdb")
    # Because ogr2ogr can't do CREATE DATABASE for some reason
    logger.info(postgis_docker_wrapper.inject_command(f"createdb {schema_name} -h 127.0.0.1 -p 5432 -U postgres -w"))
    print("Schema init done")

    postgis_adapter = PostgisAdapter(user="postgres", password="<PASSWORD>", persist = True)

    print("Loading extension")
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_raster;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_topology;"))
    logger.info(postgis_adapter.execute_nontransaction("CREATE EXTENSION IF NOT EXISTS postgis_sfcgal;"))

    # Import airports
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airports_3857/Airports.shp", "airports_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airports/Airports.shp", "airports", schema_name=schema_name))

    # Import airspaces
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airspace_3857/Class_Airspace.shp", "airspaces_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "airspace/Class_Airspace.shp", "airspaces", schema_name=schema_name))

    # Import routes
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "routes_3857/ATS_Route.shp", "routes_3857", schema_name=schema_name))
    logger.info(gdal_docker_wrapper.import_to_postgis(
        "routes/ATS_Route.shp", "routes", schema_name=schema_name))
    logger.info(postgis_adapter.execute(
            f"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name;"))

if __name__ == '__main__':
    import_mysql()
    # import_postgis()

# Recreate schema # Import airports # Import airspaces # Import routes # Recreate schema #print(postgis_adapter.get_schemas()) #if schema_name in postgis_adapter.get_schemas(): #    print("Deleting old schema") #    logger.info(postgis_adapter.execute_nontransaction(f"DROP DATABASE {schema_name}")) # Because ogr2ogr can't do CREATE DATABASE for some reason # Import airports # Import airspaces # Import routes # import_postgis()