'''
Created on 04-Jul-2021

@author: bthakur
'''

import inspect
import traceback
import random
import os
from pathlib import Path
from enum import Enum     
import re
import pandas as pd

from xv.km.ui import SvgHelper
from xv.util.data import CacheUtil
from xv.util import strip_source_code_tabs, get_function_source_code
import matplotlib.pyplot as plt

from xv.util import HtmlUtil

from ._geo_plotter import GeoPlotter
class MapHelper(GeoPlotter):
    '''
    '''
    
    def __init__(self, **kwargs):
        '''
        '''
        super().__init__(**kwargs)
        pass


    def set_superset_df(self, **kwargs):
        superset_df = kwargs.get('superset_df', None)
        if not superset_df is None:
            self._set_superset_df(superset_df)
            return 'specified custom superset'
        
        
        continent = kwargs.get('continent', None)
        if not continent is None:
            superset_df = self.search_continents([continent])
            self._set_superset_df(superset_df)
            return f"{continent}"

        country = kwargs.get('country', None)
        if not country is None:
            superset_df = self.search_countries([country])
            self._set_superset_df(superset_df)
            return f"{country}"
        
        self.reset_superset_df() #sets to None
        
        return None
        pass

    def search_and_plot(self, searchStrings, **kwargs):
        data = self.search(searchStrings, 
                           superset = self.get_superset_df())
        
        names = []
        for k, df in data.items():
            if df is None:
                continue
            
            names.extend(df['name'].to_list() )
            pass
        
        svg_str = self._plot_search_result(data)
        html_file_path = 'out/search_continents_countries_cities.html'
        
        html_file_path = self._plot_interactive_search_result(data,
                                                              html_file_path = html_file_path
                                                              )
        return names, svg_str, html_file_path
        pass
    
    def search_and_plot_continents(self, searchStrings, **kwargs):
        continents = self.search_continents(searchStrings,
                                            superset = self.get_superset_df())
        
        names = continents['name'].to_list() 
        svg_str = self._plot_name_column(continents)
        
        html_file_path = 'out/search_continents.html'
        data = {
            'continents': continents,
            }

        html_file_path = self._plot_interactive_search_result(data,
                                                              html_file_path = html_file_path
                                                              )
        return names, svg_str, html_file_path
        pass

    def search_and_plot_countries(self, searchStrings, **kwargs):
        countries = self.search_countries(searchStrings,
                                          superset = self.get_superset_df())
        
        names = countries['name'].to_list() 
        svg_str = self._plot_name_column(countries)
        
        html_file_path = 'out/search_countries.html'
        data = {
            'countries': countries,
            }

        html_file_path = self._plot_interactive_search_result(data,
                                                              html_file_path = html_file_path
                                                              )
        return names, svg_str, html_file_path
        pass

    def search_and_plot_cities(self, searchStrings, **kwargs):
        cities = self.search_cities(searchStrings,
                                    superset = self.get_superset_df())
        
        names = cities['name'].to_list() 
        svg_str = self._plot_name_column(cities)
        
        html_file_path = 'out/search_cities.html'
        data = {
            'cities': cities,
            }

        html_file_path = self._plot_interactive_search_result(data,
                                                              html_file_path = html_file_path
                                                              )
        return names, svg_str, html_file_path
        pass

    pass
#class MapHelper:
