""" PagerMaid initialization. """

from os import getcwd, makedirs
from os.path import exists
from sys import version_info, platform
from yaml import load, FullLoader
from shutil import copyfile
from redis import StrictRedis
from logging import getLogger, INFO, DEBUG, StreamHandler
from distutils2.util import strtobool
from coloredlogs import ColoredFormatter
from telethon import TelegramClient

persistent_vars = {}
module_dir = __path__[0]
working_dir = getcwd()
logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s"
config = None
help_messages = {}
logs = getLogger(__name__)
logging_handler = StreamHandler()
logging_handler.setFormatter(ColoredFormatter(logging_format))
logs.addHandler(logging_handler)
logs.setLevel(INFO)

try:
    config = load(open(r"config.yml"), Loader=FullLoader)
except FileNotFoundError:
    logs.fatal("Configuration file does not exist, generating new configuration file.")
    copyfile(f"{module_dir}/assets/config.gen.yml", "config.yml")
    exit(1)

if strtobool(config['debug']):
    logs.setLevel(DEBUG)
else:
    logs.setLevel(INFO)

if platform == "linux" or platform == "linux2" or platform == "darwin" or platform == "freebsd7" \
        or platform == "freebsd8" or platform == "freebsdN" or platform == "openbsd6":
    logs.info(
        "Detected platform as " + platform + ", proceeding to early load process of PagerMaid."
    )
else:
    logs.error(
        "Your platform " + platform + " is not supported, please start PagerMaid on Linux or *BSD."
    )
    exit(1)

if version_info[0] < 3 or version_info[1] < 6:
    logs.error(
        "Please upgrade your python interpreter to at least version 3.6."
    )
    exit(1)

if not exists(f"{getcwd()}/data"):
    makedirs(f"{getcwd()}/data")

api_key = config['api_key']
api_hash = config['api_hash']
try:
    redis_host = config['redis']['host']
except KeyError:
    redis_host = 'localhost'
try:
    redis_port = config['redis']['port']
except KeyError:
    redis_port = 6379
try:
    redis_db = config['redis']['db']
except KeyError:
    redis_db = 14
if api_key is None or api_hash is None:
    logs.info(
        "Please place a valid configuration file in the working directory."
    )
    exit(1)

bot = TelegramClient("pagermaid", api_key, api_hash, auto_reconnect=True)
redis = StrictRedis(host=redis_host, port=redis_port, db=redis_db)


def redis_status():
    try:
        redis.ping()
        return True
    except BaseException:
        return False


async def log(message):
    logs.info(
        message.replace('`', '\"')
    )
    if not strtobool(config['log']):
        return
    await bot.send_message(
        int(config['log_chatid']),
        message
    )
