'''
Created on 14-Jun-2024

@author: bthakur
'''

import inspect
import traceback
import random

from ._base_helper import BaseHelper

class EarthHelper(BaseHelper):
    '''
    '''
    
    def __init__(self, **kwargs):
        '''
        '''
        super().__init__(**kwargs)
        pass


    def get_problem_population_density(self, *args, **kwargs):
        sInfo = f"""Processing {self.__class__.__name__}.{inspect.stack()[0][3]}: """
        self._log(sInfo.strip())

        index = kwargs.get('index', None)
        
        try:
            
            if index is None:
                random_choices = list(range(1, 2))
        
                identifier = f"{self.__class__.__name__}{id(self)}.{inspect.stack()[0][3]}.random_choices"
                random_choice = self.get_no_repeat_random_choice(identifier, random_choices)
            else:
                random_choice = index
            
            if random_choice == 1:
                return self._high_population_density_countries_by_continent()
    
            return self._high_population_density_countries_by_continent()
        
        except Exception as e:
            sError = f"""Error {self.__class__.__name__}.{inspect.stack()[0][3]}: {e}
            """
            self._log(sError)
            self._log(traceback.format_exc())
            pass
        
        return None
        pass



    def get_problem_tectonics_plates(self, *args, **kwargs):
        sInfo = f"""Processing {self.__class__.__name__}.{inspect.stack()[0][3]}: """
        self._log(sInfo.strip())

        index = kwargs.get('index', None)
        
        try:
            
            if index is None:
                random_choices = list(range(1, 3))
        
                identifier = f"{self.__class__.__name__}{id(self)}.{inspect.stack()[0][3]}.random_choices"
                random_choice = self.get_no_repeat_random_choice(identifier, random_choices)
            else:
                random_choice = index
            
            if random_choice == 1:
                return self._identify_tectonics_plates()
    
            if random_choice == 2:
                return self._identify_tectonics_formation()
    
            return self._identify_tectonics_plates()
        
        except Exception as e:
            sError = f"""Error {self.__class__.__name__}.{inspect.stack()[0][3]}: {e}
            """
            self._log(sError)
            self._log(traceback.format_exc())
            pass
        
        return None
        pass

    def get_problem_structure_of_earth(self, *args, **kwargs):
        sInfo = f"""Processing {self.__class__.__name__}.{inspect.stack()[0][3]}: """
        self._log(sInfo.strip())

        index = kwargs.get('index', None)
        
        try:
            
            if index is None:
                random_choices = list(range(1, 5))
        
                identifier = f"{self.__class__.__name__}{id(self)}.{inspect.stack()[0][3]}.random_choices"
                random_choice = self.get_no_repeat_random_choice(identifier, random_choices)
            else:
                random_choice = index
            
            if random_choice == 1:
                return self._order_layers_of_earth()
    
            return self._order_layers_of_earth()
        
        except Exception as e:
            sError = f"""Error {self.__class__.__name__}.{inspect.stack()[0][3]}: {e}
            """
            self._log(sError)
            self._log(traceback.format_exc())
            pass
        
        return None
        pass



    def _high_population_density_countries_by_continent(self, **kwargs):
        """
        
        doc_style: xv_doc
        """
        map_helper = self.get_map_helper()
        map_helper.reset_superset_df()

        
        image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Population_density_map_of_the_world.svg/1200px-Population_density_map_of_the_world.svg.png"

        prob_s = f"""
        Refer to Population density map of the earth and 
        Write the name of a few countries from each continent which are densly-populated.
        <br/>
        <div><img src="{image_url}" width = "60%" /></div>
        """
        prob_s = prob_s.replace("\n", " ").strip()


        search_str = f"""
        Singapore, Hong Kong, Bangladesh, India, China, 
        Turkey, Netherlands, Belgium, UK, 
        Mauritius, Rwanda, Burundi, Nigeria,
        Bermuda, Barbados,
        Ecuador , Colombia, Venezuela, 
        Tuvalu, Fiji
        """ 
        
        search = [x.strip() for x in search_str.split(",") if x.strip() ]

        names, svg_str, html_file_path = map_helper.search_and_plot_countries(search, 
                                                                              use_cache = True)
        
        soln_s = f"""
        {svg_str}
        """
        soln_s = soln_s.replace("\n", " ").strip()
        
        answer_s = f"""
        These are major countries, not necessarily the most densly-populated.
        <ul>
            <li>Asia: Singapore, Hong Kong, Bangladesh, India, China </li>
            <li>Europe: Turkey, Netherlands, Belgium, UK</li>
            <li>Africa: Mauritius, Rwanda, Burundi, Nigeria</li>
            <li>North America: Bermuda, Barbados</li>
            <li>South America: Ecuador , Colombia, Venezuela</li>
            <li>Oceania: Tuvalu, Fiji </li>
        <ul>
        """
        answer_s = answer_s.replace("\n", " ").strip()
        
        result = {
            'prob_s': prob_s.strip(), 
            'soln_s': soln_s.strip(), 
            'answer_s': answer_s.strip(), 
            }
        
        return result

    def _identify_tectonics_plates(self, **kwargs):
        """
        
        doc_style: xv_doc
        """
        map_helper = self.get_map_helper()
        map_helper.reset_superset_df()

        
        search_str = f"""
            Asia
            Europe
            Africa
            North America
            South America
            Oceania
            Australia
        """ 
        
        search = [x.strip() for x in search_str.split("\n") if x.strip() ]

        names, svg_str, html_file_path = map_helper.search_and_plot_continents(search, 
                                                                              use_cache = True)

        image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Plates_tect2_en.svg/800px-Plates_tect2_en.svg.png?20220829111721"
        Plate_tectonics_url = "https://simple.wikipedia.org/wiki/Plate_tectonics"
        
        wl_helper = self.get_web_link_helper()
        search_str = 'tectonic plates'
        result = wl_helper.search(search_str)
        links_result = wl_helper.convert_links_to_strs(result)
        
        prob_s = f"""
        Identify tectonic plates
        <br/>
        <br/>
        {svg_str}
        """
        prob_s = prob_s.replace("\n", " ").strip()

        soln_s = f"""
        Read <a target="_blank" href = "{Plate_tectonics_url}" >Wikipedia: Plate tectonics</a>
        
        Also refer to:
        {links_result['anchors_str']}
        """
        
        credit_url = "https://commons.wikimedia.org/wiki/File:Plates_tect2_en.svg"
        
        
        answer_s = f"""
        <object data="{image_url}" width = "60%" > </object>
        Source: <a target="_blank" href="{credit_url}">Wikipedia</a>
        """
        
        result = {
            'prob_s': prob_s.strip(), 
            'soln_s': soln_s.strip(), 
            'answer_s': answer_s.strip(), 
            }
        
        return result

    def _identify_tectonics_formation(self, **kwargs):
        """
        
        doc_style: xv_doc
        """
        map_helper = self.get_map_helper()
        map_helper.reset_superset_df()

        image_url_one = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/EQs_1900-2013_worldseis.png/1200px-EQs_1900-2013_worldseis.png"
        image_url_two = "https://upload.wikimedia.org/wikipedia/commons/5/52/Pacific_Ring_of_Fire.svg"
        
        credit_url_one = "https://en.wikipedia.org/wiki/File:EQs_1900-2013_worldseis.png"
        credit_url_two = "https://en.wikipedia.org/wiki/File:Pacific_Ring_of_Fire.svg"
        
        
        wl_helper = self.get_web_link_helper()
        search_str = 'Ring of Fire'
        result = wl_helper.search(search_str)
        links_result = wl_helper.convert_links_to_strs(result)

        
        prob_s = f"""
        Identify the geo-formation.
        <br/>
        <br/>
        <object data="{image_url_one}" width = "60%" > </object>
        <br/>
        <a target="_blank" href = "{credit_url_one}" >
        Source: Wikipedia
        </a>
        """
        prob_s = prob_s.replace("\n", " ").strip()

        
        soln_s = f"""
        Ring of Fire
        
        <br/>
        <br/>
        
        The Ring of Fire is not a single geological structure. It was created by the 
        subduction of different tectonic plates at convergent boundaries around the Pacific Ocean. 
        
        <br/>
        <br/>
        
        It contains between 750 and 915 active or dormant volcanoes, 
        around two-thirds of the world total. The exact number of 
        volcanoes within the Ring of Fire depends on which regions are included.
        
        <br/>
        <br/>
        
        About 90% of the world's earthquakes,
        including most of its largest, occur within the belt. 
        
        <br/>
        <br/>
        
        <object data="{image_url_two}" width = "60%" > </object>
        
        <br/>
        
        <a target="_blank" href = "{credit_url_two}" >
        Source: Wikipedia: Pacific Ring of Fire
        </a>
        
        Also refer to:
        {links_result['anchors_str']}
        """.replace("\n", " ").strip()
        
        
        answer_s = f"""
        Ring of Fire
        """
        
        result = {
            'prob_s': prob_s.strip(), 
            'soln_s': soln_s.strip(), 
            'answer_s': answer_s.strip(), 
            }
        
        return result

    def _order_layers_of_earth(self, **kwargs):
        """
        
        doc_style: xv_doc
        """

        image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Earth-crust-cutaway-english.svg/800px-Earth-crust-cutaway-english.svg.png"


        ordered_str = f"""
        Exosphere
        Thermosphere
        Mesosphere
        Stratosphere
        Troposphere
        Crust
        Upper Mantle
        Lower Mantle
        Outer Core
        Inner Core
        """
        
        ordered_layers = [x.strip() for x in ordered_str.strip().split("\n")  if x.strip()]
        
        shuffled_layers = random.sample(ordered_layers, len(ordered_layers))
        
        shuffled_layers_str = "\n".join(shuffled_layers)

        prob_s = f"""
        Arrange the followings in the order, starting from the center of the Earth.
        
        {shuffled_layers_str}
        """
        prob_s = prob_s.replace("\n", " ").strip()

        credit_url = "https://simple.wikipedia.org/wiki/Earth#/media/File:Earth-crust-cutaway-english.svg"
        soln_s = f"""
        <object data="{image_url}" width = "60%" > </object>
        Source: <a target="_blank" href="{credit_url}">Wikipedia: Surachit</a>
        """
        
        answer_s = f"""
        {ordered_str}
        """
        
        result = {
            'prob_s': prob_s.strip(), 
            'soln_s': soln_s.strip(), 
            'answer_s': answer_s.strip(), 
            }
        
        return result

    pass
#class MapHelper:
