import time
from typing import List, Dict

from django.test import override_settings
from django.urls import path

from selenium.webdriver.common.by import By

from silica import Component
from silica.tests.SilicaBrowserTestCase import SilicaBrowserTestCase
from silica.tests.utils import create_test_view
from silica.urls import urlpatterns as silica_urlpatterns


TestView = create_test_view(f"""{{% silica '{__name__}.Parent' %}}""")

urlpatterns = silica_urlpatterns + [
    path("nestable", TestView.as_view()),
]

class Parent(Component):
    dummy = ''

    def inline_template(self):
        return """
            <div>
                Parent: "{{ dummy }}"
                <input type="test" silica:model="dummy">
                {% silica '"""+__name__+""".Child' %}             
            </div>
        """

class Child(Component):
    dummy = ''

    def dummy_updated(self, value):
        self.js_call('alert', 'hi')

    def inline_template(self):
        return """
            <div>
                Child: "{{ dummy }}"
                <input type="test" silica:model="dummy" id="child_input">             
            </div>
        """


@override_settings(ROOT_URLCONF=__name__)
class NestedTestCase(SilicaBrowserTestCase):

    def test_child_input_doesnt_trigger_parent(self):

        self.selenium.get(self.live_server_url + '/nestable')

        self.assertTrue("child_input" in self.selenium.page_source)

        self.selenium.find_element(By.ID, 'child_input').send_keys('h')
        time.sleep(0.2)

        self.selenium.save_screenshot('screenshot.png')
        # screenshot
        self.assertTrue('Parent: ""' in self.selenium.page_source)
        self.assertTrue('Child: "h"' in self.selenium.page_source)

