import os
import warnings

import requests
import pymsgbox

def internet_connection():
    try:
        response = requests.get("https://google.com", timeout=5)
        return True
    except requests.ConnectionError:
        return False


def _tk_warning():
    if (
            pymsgbox.confirm(
                "This program breaks Discords TOS.\nPress yes to aknowledge all responsibility\nThis will be the only "
                "time this is asked.",
                buttons=["Yes, I accept", "No"],
            )
            == "No"
    ):
        exit(0)
    _eula_accepted()


def _term_warning():
    if not input("This program breaks Discords TOS.\nPress yes to aknowledge all responsibility\nThis will be the "
                 "only time this is asked. [N/y]").lower().startswith("y"):
        exit(0)
    _eula_accepted()


def _eula_accepted():
    with open(os.path.join(cfg, ".eulaaccepted"), "a") as f:
        pass


CONFIG = {
    "nt": os.path.expanduser("~\\AppData\\Local\\discord-sender"),
    "other": os.path.expanduser("~/.config/discord-sender"),
}
cfg = CONFIG["nt"] if os.name == "nt" else CONFIG["other"]
if not os.path.exists(os.path.join(cfg, ".eulaaccepted")):
    if not os.path.exists(cfg):
        os.mkdir(cfg)
    try:
        _tk_warning()
    except Exception:
        try:
            _term_warning()
        except Exception:
            print("You cannot use this program until you have accepted the eula but there was an error writing the "
                  "config file.\n To continue, please run \"python -m discord_sender --accept-eula\" as "
                  "administrator/root")
    if not internet_connection():
        warnings.warn("The internet is not connected.")
    from . import discord
