from .logger import Fake_Logger
import os
from collections import OrderedDict

class Plugin_Base:
    name = "markdown-plugin"
    desc = "markdown plugin for teedoc"
    defautl_config = {
    }

    def __init__(self, config, doc_src_path, site_config, logger = None):
        '''
            @attention DO NOT overwrite this function, if have to, please call super().__init__() first
            @config a dict object
            @logger teedoc.logger.Logger object
        '''
        self._pid = os.getpid()
        self.on_init(config, doc_src_path, site_config, logger)

    def on_init(self):
        pass
        

    def on_parse_files(self, files, new_config=None):
        '''
            result = {
                "ok": False,
                "msg": "",
                "htmls": {
                    "file1_path": {
                                    "title": "",
                                    "desc": "",
                                    "keywords": [],
                                    "tags": [],
                                    "body": "",
                                    "toc": "", # just empty, toc generated by js but not python
                                    "metadata": {
                                        "title":"",
                                        "keywords": "1, 2, 3",
                                        "desc": "",
                                        "tags": "1, 2, 3",
                                        "id": "",
                                        "class": ", , , ",
                                        "show_source": "true"
                                        },
                                    "raw": content
                                 }
            }
        '''
        # result = {
        #     "ok": False,
        #     "msg": "",
        #     "htmls": OrderedDict()
        # }
        return None
    
    def on_parse_pages(self, pages, new_config):
        return None

    def on_parse_blog(self, pages, new_config):
        return None
    
    def on_add_html_header_items(self):
        return []
    
    def on_add_html_js_items(self):
        return []
    
    def on_add_navbar_items(self, new_config):
        '''
            @return list items(navbar item, e.g. "<a href=></a>")
        '''
        return []
    
    def on_copy_files(self):
        '''
            copy file to out directory, when file changes, it also will be called
            @return dict object, keyword is url, value is file path
                    {
                        "/static/css/theme-default.css": "{}/theme-default.css".format(assets_abs_path)
                    }
                    how to get assets_abs_path see teedoc-plugin-theme-default
        '''
        return {}

    def on_htmls(self, htmls_files, htmls_pages, htmls_blog=None):
        '''
            update htmls, may not all html, just partially
            htmls_files: {
                "/get_started/zh":{
                    "url":{
                                "title": "",
                                "desc": "",
                                "keywords": [],
                                "body": html,
                                "url": "",
                                "raw": ""
                          }
                }
            }
        '''
        return True

    def __del__(self):
        # DO NOT implement this function, use on_end() instead !!!!!! this functioin may be called multi times
        if os.getpid() == self._pid:
            self.on_del()

    def on_del(self):
        pass

