from infobox import Infobox
from choroplethcolourscheme import ChoroplethColourScheme
import json
from mapdata import MapData
from coordinates import Coordinates


class SVGMap(dict):
    def __init__(self, data: MapData, coords: Coordinates) -> None:
        super().__init__()
        self['map'] = coords
        self['choropleth'] = ChoroplethColourScheme()
        self['layers'] = []
        self['infobox'] = Infobox()
        self['download'] = False

        # borders
        self['borders'] = {}
        self['borders']['colors'] = {}
        self['borders']['colors']['r'] = 0
        self['borders']['colors']['b'] = 0
        self['borders']['colors']['g'] = 0
        self['borders']['stroke-width'] = 3

        # background
        self['background_color'] = {}
        self['background_color']['r'] = 0
        self['background_color']['b'] = 0
        self['background_color']['g'] = 0

        # Supports two inputs: Hovering and clicking
        self['inputs'] = {}
        self['inputs']['onHover'] = ''
        self['inputs']['onMouseOut'] = ''
        self['inputs']['onClick'] = ''
        self['data'] = data

    def return_code(self) -> str:
        return 'var data = ' + json.dumps(self) + ';'

    def save_to_file(self, filename='autogenerated.js') -> None:
        code = self.return_code()
        file = open(filename, 'w')
        file.write(code)
        file.close()
