# Path: common_utils.py
# def create_dir(output_dir, delete_existing=False):
#     path = Path(output_dir)
#     if path.exists() and delete_existing:
#         shutil.rmtree(output_dir)
#     elif not path.exists():
#         path.mkdir()


import logging
import os
import random
import subprocess
import time
from argparse import ArgumentParser
from jinja2 import Environment, FileSystemLoader
from selenium import webdriver
from slug import slug
from common.workflow import WorkflowBase, run_workflow
from common_utils import create_dir

def parse_args():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument("-p", "--page-url", type=str, required=True, help="PhpBB Forum Page Url")
    return parser.parse_args()


class InitScript(WorkflowBase):
    """Initialise environment"""

    def _init_script(self):
        handlers = [
            logging.StreamHandler(),
        ]

        logging.basicConfig(
            handlers=handlers,
            format="%(asctime)s - %(filename)s:%(lineno)d - %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S",
            level=logging.INFO,
        )
        logging.captureWarnings(capture=True)

    def run(self, context):
        self._init_script()
        template_folder = "sample"
        template_dir = os.path.dirname(os.path.abspath(__file__)) + "/" + template_folder
        jinja_env = Environment(loader=FileSystemLoader(template_dir), trim_blocks=True, autoescape=True)
        context["jinja_env"] = jinja_env
        context["output_folder"] = output_folder = "generated"

# Path: common_utils.py
# def create_dir(output_dir, delete_existing=False):
#     path = Path(output_dir)
#     if path.exists() and delete_existing:
#         shutil.rmtree(output_dir)
#     elif not path.exists():
#         path.mkdir()


# Path: phpbb_thread.py
import logging
import os
import random
import subprocess
import time
from argparse import ArgumentParser
from jinja2 import Environment, FileSystemLoader
from selenium import webdriver
from slug import slug
from common.workflow import WorkflowBase, run_workflow
from common_utils import create_dir


def parse_args():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument("-p", "--page-url", type=str, required=True, help="PhpBB Forum Page Url")
    return parser.parse_args()


class InitScript(WorkflowBase):
    """Initialise environment"""

    def _init_script(self):
        handlers = [
            logging.StreamHandler(),
        ]

        logging.basicConfig(
            handlers=handlers,
            format="%(asctime)s - %(filename)s:%(lineno)d - %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S",
            level=logging.INFO,
        )
        logging.captureWarnings(capture=True)

    def run(self, context):
        self._init_script()
        template_folder = "sample"
        template_dir = os.path.dirname(os.path.abspath(__file__)) + "/" + template_folder
        jinja_env = Environment(loader=FileSystemLoader(template_dir), trim_blocks=True, autoescape=True)
        context["jinja_env"] = jinja_env
        context["output_folder"] = output_folder = "generated"

        create_dir(output_folder, delete_existing=False)