#!/usr/bin/python3

import ssl
import json
import xml.etree.ElementTree as ET
from urllib.request import urlopen, Request
from urllib.parse import urljoin
from urllib.error import HTTPError


def get_context(verify):
    ctx = None
    if not verify:
        ctx = ssl.create_default_context()
        ctx.check_hostname = False
        ctx.verify_mode = ssl.CERT_NONE
    return ctx


def get_apikey(configfile="/root/.config/syncthing/config.xml"):
    try:
        tree = ET.parse(configfile)
        root = tree.getroot()
        apikey = root.find("./gui/apikey").text
        return apikey
    except (FileNotFoundError, ET.ParseError, AttributeError) as exc:
        raise f"Exception {exc} occured"

    return None


def get_config(url, verify, apikey):
    fullurl = f"{url}/rest/system/config"
    req = Request(method="GET",
                  url=fullurl)
    req.add_header("X-API-Key", apikey)
    res = urlopen(req, context=get_context(verify))

    ret = json.loads(res.read())
    if res.status == 200:
        return ret

    return None


def set_config(url, verify, apikey, config):
    fullurl = f"{url}/rest/system/config"
    req = Request(method="POST",
                  url=fullurl,
                  data=json.dumps(config).encode())
    req.add_header("Content-Type", "application/json")
    req.add_header("X-API-Key", apikey)
    try:
        res = urlopen(req, context=get_context(verify))
    except HTTPError as err:
        if err.status != 307:
            return None
        req.full_url = urljoin(url, err.headers['Location'])
        res = urlopen(req, context=get_context(verify))
    if res.status == 200:
        return True

    return None


def insync(url, verify, apikey):
    fullurl = f"{url}/rest/system/config/insync"
    req = Request(method="GET",
                  url=fullurl)
    req.add_header("X-API-Key", apikey)
    res = urlopen(req, context=get_context(verify))
    ret = json.loads(res.read())
    if res.status == 200:
        return ret

    return None


def restart(url, verify, apikey):
    fullurl = f"{url}/rest/system/restart"
    req = Request(method="POST",
                  url=fullurl)
    req.add_header("X-API-Key", apikey)
    res = urlopen(req, context=get_context(verify))
    if res.status == 200:
        return {}

    return None