from colour import Color
from infobox import Infobox
from choroplethcolourscheme import ChoroplethColourScheme
import json
from mapdata import MapData
from coordinates import Coordinates
import requests


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']['colour'] = {}
        self['borders']['colour']['r'] = 0
        self['borders']['colour']['b'] = 0
        self['borders']['colour']['g'] = 0
        self['borders']['stroke-width'] = 3

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

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

    def modify_background_colour(self, new_colour: Color) -> None:
        """
        Modifies the background colour from a Color object.

        Parameters:
            new_colour: Colour that the background will be changed to.

        >>> coords = Coordinates({}, [], zoom=1, translate=[0, 0])
        >>> map_data = MapData({'territory_list':[]})
        >>> svg_map = SVGMap(map_data, coords)
        >>> my_colour = Color(rgb=(0.5, 0, 0))
        >>> svg_map.modify_background_colour(my_colour)
        >>> svg_map['background_colour']['r']
        127
        >>> svg_map['background_colour']['g']
        0
        """
        self['background_colour']['r'] = int(new_colour.get_red() * 255)
        self['background_colour']['g'] = int(new_colour.get_green() * 255)
        self['background_colour']['b'] = int(new_colour.get_blue() * 255)

    def modify_border_colour(self, new_colour: Color) -> None:
        """
        Modifies the border colour from a Color object.

        Parameters:
            new_colour: Colour that the border will be changed to.

        >>> coords = Coordinates({}, [], zoom=1, translate=[0, 0])
        >>> map_data = MapData({'territory_list':[]})
        >>> svg_map = SVGMap(map_data, coords)
        >>> my_colour = Color(rgb=(0, 0.5, 0))
        >>> svg_map.modify_border_colour(my_colour)
        >>> svg_map['borders']['colour']['r']
        0
        >>> svg_map['borders']['colour']['g']
        127
        """
        self['borders']['colour']['r'] = int(new_colour.get_red() * 255)
        self['borders']['colour']['g'] = int(new_colour.get_green() * 255)
        self['borders']['colour']['b'] = int(new_colour.get_green() * 255)

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

    def save_to_file(self, js_filename='autogenerated.js', html_filename='index.html') -> None:
        code = self.return_code()
        file = open(js_filename, 'w')
        file.write(code)
        html_file = open(html_filename, 'w')
        req = requests.get('https://raw.githubusercontent.com/geographieactuelle/fairedescartes/main/index.html')
        html_file.write(req.text)
        file.close()
        html_file.close()
