# SPDX-License-Identifier: Apache-2.0

from django.conf import settings
import cavedb.docgen_common
import cavedb.docgen_gis_common
import cavedb.utils
from cavedb.docgen_gis_common import get_bulletin_mapserver_mapfile

class MapserverMapfile(cavedb.docgen_common.Common):
    def __init__(self, bulletin):
        cavedb.docgen_common.Common.__init__(self)
        self.bulletin = bulletin
        self.gis_maps = {}


    def gis_map(self, gismap):
        shpdir = cavedb.docgen_gis_common.get_bulletin_shp_directory(self.bulletin.id)

        gis_options = {}
        gis_options['basename'] = gismap.name
        gis_options['path'] = get_bulletin_mapserver_mapfile(self.bulletin.id, gismap.name)
        gis_options['locations_shp'] = '%s/%s' % \
                                       (shpdir, cavedb.docgen_gis_common.LOCATIONS_SHP_LAYER_NAME)
        gis_options['extents_shp'] = '%s/%s' % \
                                     (shpdir, \
                                      cavedb.docgen_gis_common.REGION_EXTENTS_SHP_LAYER_NAME)

        cavedb.docgen_common.create_base_directory(gis_options['path'])

        gis_options['fd'] = open(gis_options['path'], 'w')

        write_mapserver_header(gis_options)

        self.gis_maps[gismap.name] = gis_options


    def gis_layer(self, gislayer):
        for gismap_basename in gislayer.maps.all():
            if gismap_basename.name not in self.gis_maps:
                continue

            write_layer(self.gis_maps[gismap_basename.name], gislayer)


    def gis_lineplot(self, lineplot, lineplot_type, shpfilename):
        if lineplot_type == 'underground':
            for gis_options in list(self.gis_maps.values()):
                write_lineplot(gis_options, lineplot, shpfilename)


    def close(self):
        for gis_options in list(self.gis_maps.values()):
            write_mapserver_footer(gis_options)
            gis_options['fd'].close()

        fonts_list = get_mapserver_fonts_list(self.bulletin.id)
        with open(fonts_list, "w") as output:
            output.write(settings.GIS_FONTS_LIST)


def write_mapserver_header(gis_options):
    # pylint: disable=too-many-statements

    mapfile = gis_options['fd']

    mapfile.write('MAP\n')
    mapfile.write('  IMAGETYPE JPEG\n')
    mapfile.write('  SIZE 2100 2100\n')
    mapfile.write('  IMAGECOLOR 255 255 255\n')
    mapfile.write('  FONTSET "fonts.list"\n')
    mapfile.write('  OUTPUTFORMAT\n')
    mapfile.write('    NAME "aggjpg24"\n')
    mapfile.write('    DRIVER AGG/JPEG\n')
    mapfile.write('    MIMETYPE "image/jpeg"\n')
    mapfile.write('    IMAGEMODE RGB\n')
    mapfile.write('    EXTENSION "jpg"\n')
    mapfile.write('  END\n')
    mapfile.write('  SYMBOL\n')
    mapfile.write('    NAME "point"\n')
    mapfile.write('    TYPE ELLIPSE\n')
    mapfile.write('    POINTS\n')
    mapfile.write('      1 1\n')
    mapfile.write('    END\n')
    mapfile.write('    FILLED TRUE\n')
    mapfile.write('  END\n')
    mapfile.write('  UNITS dd\n')
    mapfile.write('  PROJECTION\n')
    mapfile.write('    "proj=longlat"\n')
    mapfile.write('    "ellps=WGS84"\n')
    mapfile.write('    "datum=WGS84"\n')
    mapfile.write('    "no_defs"\n')
    mapfile.write('  END\n')
    mapfile.write('  LEGEND\n')
    mapfile.write('    KEYSIZE 36 36\n')
    mapfile.write('    LABEL\n')
    mapfile.write('      SIZE 24\n')
    mapfile.write('      TYPE TRUETYPE\n')
    mapfile.write('      FONT opensymbol\n')
    mapfile.write('      ANTIALIAS TRUE\n')
    mapfile.write('      COLOR 0 0 89\n')
    mapfile.write('      OUTLINECOLOR 255 255 255\n')
    mapfile.write('      OUTLINEWIDTH 3\n')
    mapfile.write('      POSITION cc\n')
    mapfile.write('    END\n')
    mapfile.write('    STATUS OFF\n')
    mapfile.write('    POSITION lr\n')
    mapfile.write('    IMAGECOLOR 255 255 255\n')
    mapfile.write('    TRANSPARENT FALSE\n')
    mapfile.write('  END\n')
    mapfile.write('  SCALEBAR\n')
    mapfile.write('    LABEL\n')
    mapfile.write('      SIZE 20\n')
    mapfile.write('      TYPE TRUETYPE\n')
    mapfile.write('      FONT opensymbol\n')
    mapfile.write('      ANTIALIAS TRUE\n')
    mapfile.write('      COLOR 0 0 0\n')
    mapfile.write('    END\n')
    mapfile.write('    STYLE 0\n')
    mapfile.write('    SIZE 600 25\n')
    mapfile.write('    IMAGECOLOR 255 255 255\n')
    mapfile.write('    OUTLINECOLOR 0 0 0\n')
    mapfile.write('    TRANSPARENT FALSE\n')
    mapfile.write('    COLOR 0 0 0\n')
    mapfile.write('    UNITS MILES\n')
    mapfile.write('    INTERVALS 5\n')
    mapfile.write('    STATUS EMBED\n')
    mapfile.write('    POSITION ll\n')
    mapfile.write('  END\n')
    mapfile.write('  INCLUDE "%s"\n' % \
                  (cavedb.docgen_gis_maps.get_mapserver_include(gis_options['basename'])))


def write_layer(gis_options, layer):
    mapfile = gis_options['fd']

    mapfile.write('  LAYER\n')