# Path: webalchemy/remotedocument.py
# class RemoteDocument:
#     # TODO: remove block altogether...
#     def __init__(self):
#         self.varname = 'document'
#         self.__uid_count = 0
#         self.__code_strings = []
#         self.__block_ixs = []
#         self.__varname_element_dict = {}
#         self.body = Element(self, 'body', '', customvarname='document.body')
#         self.head = Element(self, 'head', '', customvarname='document.head')
#         self.pop_all_code()  # body and head are special: created by static content
#         self.app = SimpleProp(self, 'document', 'app', create=True)
#         self.props = SimpleProp(self, 'document')
#         self.localStorage = SimpleProp(self, 'localStorage')
#         self.sessionStorage = SimpleProp(self, 'localStorage')
#         self.stylesheet = _StyleSheet(self)
#         self.vendor_prefix = None
#         self.jsrpcweakrefs = {}
#         self.window = Window(self)
#         self.KeyCode = KeyCode
# 
#     def parse_elements(self, html):
#         class E:
#             pass
#         e = E()
#         for id in get_element_ids(html):
#             try:
#                 attr_id = id.replace('-', '_').replace(' ', '_')
#                 setattr(e, attr_id, self.getElementById(id))
#             except:
#                 pass
#         return e
# 
#     def set_vendor_prefix(self, vendor_prefix):
#         self.vendor_prefix = vendor_prefix
# 
#     def get_element_from_varname(self, varname) -> Element:
#         return self.__varname_element_dict[varname]
# 
#     def getElementById(self, fromid, app=False):
#         return Element(self, fromid=fromid, app=app)
# 
#     def element(self, typ=None, text=None, app=True, **kwargs):
#         kwargs["app"] = app
#         return Element(self, typ, text, **kwargs)
# 
#     def startinterval(self, ms, exp=None):
#         return Interval(self, ms, exp, level=3)
# 
#     def jsfunction(self, *varargs, body=None, level=3, **kwargs):
#         return JSFunction(self, *varargs, body=body, level=level, **kwargs)
# 
#     def get_new_uid(self):
#         uid = '__v' + str(self.__uid_count)
#         self.__uid_count += 1
#         return uid
# 
#     def begin_block(self):
#         self.__block_ixs.append(len(self.__code_strings))
# 
#     def cancel_block(self):
#         self.__block_ixs.pop()
# 
#     def pop_block(self):
#         ix = self.__block_ixs.pop()
#         code = ''
#         for i in range(len(self.__code_strings) - ix):
#             code = self.__code_strings.pop() + code
#         return code
# 
#     def pop_all_code(self):
#         code = ''.join(self.__code_strings)
#         del self.__code_strings[:]
#         del self.__block_ixs[:]
#         return code
# 
#     def pop_line(self):
#         return self.__code_strings.pop()
# 
#     def inline(self, text, *varargs):
#         """
#         insert a code block (contained in 'text' parameter)
#         """
#         if varargs:
#             text = text.format(*(v.varname for v in varargs))
#         self.__code_strings.append(text)
# 
#     def JS(self, text, encapsulate_strings=True, level=2):
#         self.__code_strings.append(_inline(text, level=level, stringify=self.stringify, rpcweakrefs=self.jsrpcweakrefs, encapsulate_strings=encapsulate_strings))
# 
#     def msg(self, text):
#         self.inline('message("' + text + '");')
# 
#     def stylesheet(self):
#         return _StyleSheet(self)
# 
#     def dict(self):
#         return SimpleProp(self)
# 
#     def stringify(self, val=None, custom_stringify=None, encapsulate_strings=True, pop_line=True, vars=None, translate=False):
#         if type(val) is bool:
#             return 'true' if val else 'false'
#         if hasattr(val, 'varname'):
#             return val.varname
#         if type(val) is str:
#             return '"' + str(val) + '"' if encapsulate_strings else val
#         if callable(val):
#             if translate:
#                 return _vtranslate(dedent(getsource(val)))
#             else:
#                 self.begin_block()
#                 tmp = val(*vars) if vars else val()
#                 if tmp:
#                     self.cancel_block()
#                 else:
#                     tmp = self.pop_block()
#                 if not tmp:
#                     raise Exception('cant find callable '+val.__name__+'. Maybe it needs to get translated?')
#                 if not vars:
#                     return 'function(){' + tmp + '}'
#                 else:
#                     for v in vars:
#                         tmp = tmp.replace('"'+v+'"', v)
#                     return 'function('+','.join(v for v in vars)+'){' + tmp + '}'
#         if val is None:
#             if pop_line:
#                 return self.pop_line()
#             return self.pop_block()
#         if custom_stringify:
#             return custom_stringify(val)
#         return str(val)
# 
#     def new(self, cls):
#         return JSClass(self, cls, level=4)
# 
#     def translate(self, cls):
#         return JSClass(self, cls, level=4, new=False)


import os.path
from bs4 import BeautifulSoup
from .remotedocument import RemoteDocument




def get_main_html_file_path(app):
    if hasattr(app, 'main_html_file_path'):
        return app.main_html_file_path

    main_dir = os.path.realpath(__file__)
    main_dir = os.path.dirname(main_dir)
    return os.path.join(main_dir, 'main.html')

# js files to be injected to main html, in this order:
js_files_to_inject_to_live_app = [
    'js/reconnecting_ws.js',
    'js/cookie.js',
    'js/vendor.js',
    'js/comm.js',
    'js/reconnection_overlay.js',
    'js/classy.js',

# Path: webalchemy/remotedocument.py
# class RemoteDocument:
#     # TODO: remove block altogether...
#     def __init__(self):
#         self.varname = 'document'
#         self.__uid_count = 0
#         self.__code_strings = []
#         self.__block_ixs = []
#         self.__varname_element_dict = {}
#         self.body = Element(self, 'body', '', customvarname='document.body')
#         self.head = Element(self, 'head', '', customvarname='document.head')
#         self.pop_all_code()  # body and head are special: created by static content
#         self.app = SimpleProp(self, 'document', 'app', create=True)
#         self.props = SimpleProp(self, 'document')
#         self.localStorage = SimpleProp(self, 'localStorage')
#         self.sessionStorage = SimpleProp(self, 'localStorage')
#         self.stylesheet = _StyleSheet(self)
#         self.vendor_prefix = None
#         self.jsrpcweakrefs = {}
#         self.window = Window(self)
#         self.KeyCode = KeyCode
# 
#     def parse_elements(self, html):
#         class E:
#             pass
#         e = E()
#         for id in get_element_ids(html):
#             try:
#                 attr_id = id.replace('-', '_').replace(' ', '_')
#                 setattr(e, attr_id, self.getElementById(id))
#             except:
#                 pass
#         return e
# 
#     def set_vendor_prefix(self, vendor_prefix):
#         self.vendor_prefix = vendor_prefix
# 
#     def get_element_from_varname(self, varname) -> Element:
#         return self.__varname_element_dict[varname]
# 
#     def getElementById(self, fromid, app=False):
#         return Element(self, fromid=fromid, app=app)
# 
#     def element(self, typ=None, text=None, app=True, **kwargs):
#         kwargs["app"] = app
#         return Element(self, typ, text, **kwargs)
# 
#     def startinterval(self, ms, exp=None):
#         return Interval(self, ms, exp, level=3)
# 
#     def jsfunction(self, *varargs, body=None, level=3, **kwargs):
#         return JSFunction(self, *varargs, body=body, level=level, **kwargs)
# 
#     def get_new_uid(self):
#         uid = '__v' + str(self.__uid_count)
#         self.__uid_count += 1
#         return uid
# 
#     def begin_block(self):
#         self.__block_ixs.append(len(self.__code_strings))
# 
#     def cancel_block(self):
#         self.__block_ixs.pop()
# 
#     def pop_block(self):
#         ix = self.__block_ixs.pop()
#         code = ''
#         for i in range(len(self.__code_strings) - ix):
#             code = self.__code_strings.pop() + code
#         return code
# 
#     def pop_all_code(self):
#         code = ''.join(self.__code_strings)
#         del self.__code_strings[:]
#         del self.__block_ixs[:]
#         return code
# 
#     def pop_line(self):
#         return self.__code_strings.pop()
# 
#     def inline(self, text, *varargs):
#         """
#         insert a code block (contained in 'text' parameter)
#         """
#         if varargs:
#             text = text.format(*(v.varname for v in varargs))
#         self.__code_strings.append(text)
# 
#     def JS(self, text, encapsulate_strings=True, level=2):
#         self.__code_strings.append(_inline(text, level=level, stringify=self.stringify, rpcweakrefs=self.jsrpcweakrefs, encapsulate_strings=encapsulate_strings))
# 
#     def msg(self, text):
#         self.inline('message("' + text + '");')
# 
#     def stylesheet(self):
#         return _StyleSheet(self)
# 
#     def dict(self):
#         return SimpleProp(self)
# 
#     def stringify(self, val=None, custom_stringify=None, encapsulate_strings=True, pop_line=True, vars=None, translate=False):
#         if type(val) is bool:
#             return 'true' if val else 'false'
#         if hasattr(val, 'varname'):
#             return val.varname
#         if type(val) is str:
#             return '"' + str(val) + '"' if encapsulate_strings else val
#         if callable(val):
#             if translate:
#                 return _vtranslate(dedent(getsource(val)))
#             else:
#                 self.begin_block()
#                 tmp = val(*vars) if vars else val()
#                 if tmp:
#                     self.cancel_block()
#                 else:
#                     tmp = self.pop_block()
#                 if not tmp:
#                     raise Exception('cant find callable '+val.__name__+'. Maybe it needs to get translated?')
#                 if not vars:
#                     return 'function(){' + tmp + '}'
#                 else:
#                     for v in vars:
#                         tmp = tmp.replace('"'+v+'"', v)
#                     return 'function('+','.join(v for v in vars)+'){' + tmp + '}'
#         if val is None:
#             if pop_line:
#                 return self.pop_line()
#             return self.pop_block()
#         if custom_stringify:
#             return custom_stringify(val)
#         return str(val)
# 
#     def new(self, cls):
#         return JSClass(self, cls, level=4)
# 
#     def translate(self, cls):
#         return JSClass(self, cls, level=4, new=False)


# Path: webalchemy/mainhtml.py
import os.path
from bs4 import BeautifulSoup
from .remotedocument import RemoteDocument





def get_main_html_file_path(app):
    if hasattr(app, 'main_html_file_path'):
        return app.main_html_file_path

    main_dir = os.path.realpath(__file__)
    main_dir = os.path.dirname(main_dir)
    return os.path.join(main_dir, 'main.html')

# js files to be injected to main html, in this order:
js_files_to_inject_to_live_app = [
    'js/reconnecting_ws.js',
    'js/cookie.js',
    'js/vendor.js',
    'js/comm.js',
    'js/reconnection_overlay.js',
    'js/classy.js',

    'js/weba.js',